Based on the SQLD exam syllabusReport an error
NULLS FIRST / NULLS LAST let you set the NULL position directly (Oracle).The rows of a table have no fixed order. Running the same SELECT twice does not guarantee the same order, and if the execution plan or the storage location changes, the order can change too. The only thing that decides the order of a result is ORDER BY.
ASC, and for descending you add DESC.SELECT ENAME, SAL FROM EMP WHERE DEPTNO = 20 ORDER BY SAL;ENAME SAL
---------- ----------
SMITH 800
ADAMS 1100
JONES 2975
FORD 3000
SCOTT 3000SELECT ENAME, SAL FROM EMP WHERE DEPTNO = 20 ORDER BY SAL DESC;ENAME SAL
---------- ----------
SCOTT 3000
FORD 3000
JONES 2975
ADAMS 1100
SMITH 800| Method | Example | Note |
|---|---|---|
| Column name (or expression) | ORDER BY SAL | It does not have to be in SELECT (see the restrictions below) |
| SELECT alias | ORDER BY ANNUAL | ORDER BY comes after SELECT, so it knows the alias |
| Position number | ORDER BY 3 | Which column of the SELECT list it is |
An example of sorting by alias.
SELECT ENAME, SAL * 12 AS ANNUAL FROM EMP WHERE DEPTNO = 10 ORDER BY ANNUAL DESC;ENAME ANNUAL
---------- ----------
KING 60000
CLARK 29400
MILLER 15600An example of sorting by position number. The 3 is the third column of the SELECT list, SAL.
SELECT ENAME, JOB, SAL FROM EMP WHERE DEPTNO = 10 ORDER BY 3 DESC;ENAME JOB SAL
---------- --------- ----------
KING PRESIDENT 5000
CLARK MANAGER 2450
MILLER CLERK 1300SELECT ENAME, SAL FROM EMP WHERE DEPTNO = 10 ORDER BY 4;SELECT ENAME, SAL FROM EMP WHERE DEPTNO = 10 ORDER BY 4
*
ERROR at line 1:
ORA-01785: ORDER BY item must be the number of a SELECT-list expressionAn alias created with double quotation marks is case-sensitive, so it must be written in the same form in ORDER BY too.
SELECT ENAME AS "Name", SAL FROM EMP WHERE DEPTNO = 10 ORDER BY "Name";Name SAL
---------- ----------
CLARK 2450
KING 5000
MILLER 1300If you list keys separated by commas, the result is sorted by the first key, and only rows with the same first key are sorted by the second key. ASC and DESC are attached to each key separately.
SELECT ENAME, SAL FROM EMP WHERE DEPTNO = 20 ORDER BY SAL DESC, ENAME ASC;ENAME SAL
---------- ----------
FORD 3000
SCOTT 3000
JONES 2975
ADAMS 1100
SMITH 800DESC applies only to the key right before it, SAL. ENAME follows the ASC written separately (it is the same if omitted).This is the typical two-level sort: highest salary first within each department.
SELECT ENAME, DEPTNO, SAL
FROM EMP
WHERE JOB IN ('CLERK', 'MANAGER')
ORDER BY DEPTNO, SAL DESC;ENAME DEPTNO SAL
---------- ---------- ----------
CLARK 10 2450
MILLER 10 1300
JONES 20 2975
ADAMS 20 1100
SMITH 20 800
BLAKE 30 2850
JAMES 30 950ORDER BY SAL DESC, DEPTNO, you get a completely different result, mixed into one salary order.Sometimes a key is meaningless. If the first key values are all different, the second key never comes into play.
SELECT ENAME, SAL FROM EMP WHERE DEPTNO = 10 ORDER BY ENAME DESC, SAL;ENAME SAL
---------- ----------
MILLER 1300
KING 5000
CLARK 2450When sorting, Oracle treats NULL as a value larger than any other. We check it with COMM in department 30 (BLAKE and JAMES are NULL, TURNER is 0).
SELECT ENAME, COMM FROM EMP WHERE DEPTNO = 30 ORDER BY COMM, ENAME;ENAME COMM
---------- ----------
TURNER 0
ALLEN 300
WARD 500
MARTIN 1400
BLAKE
JAMESSELECT ENAME, COMM FROM EMP WHERE DEPTNO = 30 ORDER BY COMM DESC, ENAME;ENAME COMM
---------- ----------
BLAKE
JAMES
MARTIN 1400
WARD 500
ALLEN 300
TURNER 0Oracle lets you set the NULL position separately from the sort direction.
SELECT ENAME, COMM FROM EMP WHERE DEPTNO = 30 ORDER BY COMM NULLS FIRST, ENAME;ENAME COMM
---------- ----------
BLAKE
JAMES
TURNER 0
ALLEN 300
WARD 500
MARTIN 1400SELECT ENAME, COMM FROM EMP WHERE DEPTNO = 30 ORDER BY COMM DESC NULLS LAST, ENAME;ENAME COMM
---------- ----------
MARTIN 1400
WARD 500
ALLEN 300
TURNER 0
BLAKE
JAMES| Sort | Oracle default | To change it |
|---|---|---|
ASC | NULL last | ASC NULLS FIRST |
DESC | NULL first | DESC NULLS LAST |
NULLS FIRST and NULLS LAST go right after the key they apply to. They can be used together with the sort direction (ASC, DESC).ORDER BY NVL(COMM, -1) DESC treats NULL as -1 and sends it to the end.⚠️ DBMS difference: SQL Server treats NULL as the smallest value. So NULL comes first in ASC and last in DESC, exactly the opposite of Oracle. If the question names the DBMS, judge by that one.
A plain SELECT can sort by a column that is not in the SELECT list. Even though ORDER BY is processed after SELECT, the original row's column values are still available.
SELECT ENAME FROM EMP WHERE DEPTNO = 10 ORDER BY SAL DESC;ENAME
----------
KING
CLARK
MILLERDISTINCT merges duplicates based on the SELECT list. A merged row contains several original rows, so a single SAL value cannot be determined.
SELECT DISTINCT JOB FROM EMP ORDER BY SAL;SELECT DISTINCT JOB FROM EMP ORDER BY SAL
*
ERROR at line 1:
ORA-01791: not a SELECTed expressionSELECT DISTINCT JOB FROM EMP ORDER BY JOB.ORDER BY in a GROUP BY query follows the same rules as SELECT. Only GROUP BY columns or aggregate functions can be used.
SELECT DEPTNO, COUNT(*) FROM EMP GROUP BY DEPTNO ORDER BY SAL;SELECT DEPTNO, COUNT(*) FROM EMP GROUP BY DEPTNO ORDER BY SAL
*
ERROR at line 1:
ORA-00979: "SAL": must appear in the GROUP BY clause or be used in an aggregate functionWrapped in an aggregate function, it can be a sort key even if it is not in SELECT.
SELECT DEPTNO, COUNT(*) AS CNT FROM EMP GROUP BY DEPTNO ORDER BY MAX(SAL) DESC; DEPTNO CNT
---------- ----------
10 3
20 5
30 6| Query form | Sorting by a column not in SELECT |
|---|---|
| Plain SELECT | Possible |
SELECT DISTINCT | Not possible (ORA-01791) |
| GROUP BY | Only GROUP BY columns and aggregate functions (anything else is ORA-00979) |
Tomorrow we cover joins, which attach two tables together, using Oracle's notation.
ORDER BY 2.Click a choice to reveal the answer and explanation.
Question 1
The COMM values of the employees in department 30 are 0, 300, 500, 1400, and two NULLs. When the following SQL is run in Oracle, what is the COMM value in the last row? ```sql SELECT ENAME, COMM FROM EMP WHERE DEPTNO = 30 ORDER BY COMM; ```
Question 2
The salaries in department 20 are SMITH 800, ADAMS 1100, JONES 2975, SCOTT 3000, and FORD 3000. What is the ENAME in the second row of the following SQL result? ```sql SELECT ENAME, SAL FROM EMP WHERE DEPTNO = 20 ORDER BY SAL DESC, ENAME; ```
Question 3
Which is the correct result of running the following SQL in Oracle? ```sql SELECT DISTINCT JOB FROM EMP ORDER BY SAL; ```
Question 4
Which of the following ORDER BY clauses causes an error when attached after SELECT ENAME, SAL FROM EMP?
Question 5
The COMM values for department 30 are MARTIN 1400, WARD 500, ALLEN 300, TURNER 0, and BLAKE and JAMES are NULL. What is the ENAME in the first row of the following SQL result? ```sql SELECT ENAME, COMM FROM EMP WHERE DEPTNO = 30 ORDER BY COMM DESC NULLS LAST; ```