Skip to content

Instantly share code, notes, and snippets.

@hirenchauhan2
Last active May 2, 2018 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hirenchauhan2/95da24022c9e2025ed10c2297a020693 to your computer and use it in GitHub Desktop.
Save hirenchauhan2/95da24022c9e2025ed10c2297a020693 to your computer and use it in GitHub Desktop.
How Oracle Query is executed in steps

How the SQL SELECT Query is exceuted in Oracle

Consider the following SQL Query

SELECT
  deptno,
  sum(sal) total_salary
FROM
  emp
WHERE
  deptno <> 40
GROUP BY
  deptno
HAVING
  SUM(sal) > 5000
ORDER BY
  total_salary;

Result will be something like

DEPTNO TOTAL_SALARY
10 5500
20 7150
30 9500

This is how the query will be executed in Oracle Server

from emp

This will be exceuted first, as we want to get data from the emp table.

where deptno <> 40

This will filter out the result where no record has deptno 40.

group by deptno

This will group the result into groups of deptnos.

having sum(sal) > 5000

This will eliminated the groups that are having total_salary less than or equal to 5000

select deptno, sum(sal)

This will fetch the deptno and sum the sal column based on the groups from the result set.

order by total_salary

This will sort the result by the total_salary in ascending order
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment