Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Order of Execution of SQL

KarkiFeb 23 2014 — edited Feb 24 2014

Hi Friends,

Referring post https://community.oracle.com/thread/3520158

If the order of processing a SQL statement is

FROM -> WHERE -> GROUP BY -> HAVING -> SELECT -> ORDER BY

Then why do I get errors in different order:

Check on it.

A). All fields are wrong. FROM has been checked first.

SQL> SELECT EAEED

  2  FROM DSFS

  3  WHERE DFSDF >3

  4  GROUP BY EDDEW

  5  HAVING EDEW = 5

  6  ORDER BY ERE;

FROM DSFS

     *

ERROR at line 2:

ORA-00942: table or view does not exist

B). Correcting FROM clause identifier.   [ORDER BY has been checked prior to WHERE]

SQL> SELECT EAEED

  2  FROM EMP

  3  WHERE DFSDF >3

  4  GROUP BY EDDEW

  5  HAVING EDEW = 5

  6  ORDER BY ERE;

ORDER BY ERE

         *

ERROR at line 6:

ORA-00904: "ERE": invalid identifier

C). Correcting ORDER BY clause.  [HAVING has been checked prior to GROUP BY]

SQL> SELECT EAEED

  2  FROM EMP

  3  WHERE DFSDF >3

  4  GROUP BY EDDEW

  5  HAVING EDEW = 5

  6  ORDER BY ENAME;

HAVING EDEW = 5

       *

ERROR at line 5:

ORA-00904: "EDEW": invalid identifier

D). Removing HAVING clause.

SELECT EAEED

FROM EMP

WHERE DFSDF >3

GROUP BY EDDEW

-- HAVING EDEW = 5

ORDER BY ENAME;

E). Correcting Group by

SQL> SELECT EAEED

  2  FROM EMP

  3  WHERE DFSDF > 3

  4  GROUP BY EMPNO

  5  -- HAVING EDEW = 5

  6  ORDER BY ENAME;

WHERE DFSDF > 3

      *

ERROR at line 3:

ORA-00904: "DFSDF": invalid identifier

F). Correcting WHERE

SQL> SELECT EAEED

  2  FROM EMP

  3  WHERE deptno <> 10

  4  GROUP BY EMPNO

  5  -- HAVING EDEW = 5

  6  ORDER BY ENAME;

SELECT EAEED

       *

ERROR at line 1:

ORA-00904: "EAEED": invalid identifier

G). Correcting SELECT

SELECT EMPNO

FROM EMP

WHERE deptno <> 10

GROUP BY EMPNO

HAVING count(*) > 1

ORDER BY Empno;

     EMPNO

----------

      7369

      7499

      7521

      7566

      7654

      7698

      7788

      7844

      7876

      7900

      7902

11 rows selected.

Does it mean SQL run in different order or there is a some reason for getting error in this order?

This post has been answered by Frank Kulash on Feb 24 2014
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 24 2014
Added on Feb 23 2014
4 comments
9,819 views