Question 1
The `employees` table holds eight rows. Four have a commission (Grace 0.2, Carol 0.15, Bob 0.1, Eve 0.05) and four have `commission` set to NULL (Alice, Dave, Frank, Heidi); `emp_id` is unique. You must return **only the three highest commissions, largest first** — a row whose `commission` is NULL must never reach the result, and the sort must be fully deterministic. Which query does that?
A. SELECT first_name, commission FROM employees ORDER BY commission DESC, emp_id FETCH FIRST 3 ROWS ONLY;
Assumes DESC also pushes NULLs to the end. It does not: NULLS FIRST is the default for descending order, so the three rows fetched are the NULL-commission employees Alice, Dave and Frank — exactly the rows that had to be excluded.
B. SELECT first_name, commission FROM employees ORDER BY commission DESC NULLS LAST, emp_id FETCH FIRST 3 ROWS ONLY;Correct answer
Direction and null placement are specified independently, so DESC NULLS LAST puts the largest commission first and defers every NULL past the fetched rows; emp_id breaks any tie, making the result Grace 0.2, Carol 0.15, Bob 0.1.
C. SELECT first_name, commission FROM employees ORDER BY commission NULLS LAST, emp_id FETCH FIRST 3 ROWS ONLY;
Stating NULLS LAST does keep the NULLs out of the first three rows, but it does not change the direction: with no DESC the sort is ascending, so this returns the three *lowest* commissions (Eve 0.05, Bob 0.1, Carol 0.15) instead of the three highest.
D. SELECT first_name, commission FROM employees ORDER BY 1 DESC NULLS LAST, emp_id FETCH FIRST 3 ROWS ONLY;
Treats the integer as a reference to the intended sort column. A bare integer in ORDER BY is a one-based position in the *select list*, and position 1 there is first_name, so this sorts names descending and returns Heidi, Grace and Frank.
Explanation
In an ORDER BY item the sort direction and the null-placement default are two separate settings: ascending defaults to NULLS LAST while descending defaults to NULLS FIRST, so asking for the largest values first *and* NULLs at the end requires writing both DESC and NULLS LAST — each keyword alone gives one half of the requirement and the opposite default for the other. Because the row limit is applied after the sort, that null placement decides which rows survive FETCH FIRST, not merely how they are arranged. A bare integer in ORDER BY is a select-list position rather than a name, so it sorts whichever column happens to sit at that position, and a unique trailing key such as emp_id is what makes the ordering deterministic when the leading key ties.