Bird
Raised Fist0
PostgreSQLquery~10 mins

ORDER BY with NULLS FIRST and NULLS LAST in PostgreSQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - ORDER BY with NULLS FIRST and NULLS LAST
Start with unsorted data
↓
Apply ORDER BY clause
↓
Check for NULL values
↓
Sort non-NULL values
↓
Return sorted result
The data is sorted by a column. NULL values are placed either at the start or end before sorting the non-NULL values.
Execution Sample
PostgreSQL
SELECT id, score
FROM players
ORDER BY score NULLS FIRST;
Sort players by score, placing NULL scores at the beginning.
Execution Table
StepRowscoreNULL checkPlacementOrder so far
1Row 3NULLIs NULLPlace at start[NULL]
2Row 150Not NULLPlace after NULLs[NULL, 50]
3Row 270Not NULLPlace after NULLs[NULL, 50, 70]
4Row 490Not NULLPlace after NULLs[NULL, 50, 70, 90]
5Row 5NULLIs NULLPlace at start[NULL, NULL, 50, 70, 90]
6End---Sorting complete
💡 All rows processed; NULLs placed first, then sorted non-NULL values.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
Ordered List[][NULL][NULL, 50][NULL, 50, 70][NULL, 50, 70, 90][NULL, NULL, 50, 70, 90][NULL, NULL, 50, 70, 90]
Key Moments - 2 Insights
Why do NULL values appear at the start when using NULLS FIRST?
Because the ORDER BY clause with NULLS FIRST explicitly tells the database to place all NULL values before any non-NULL values, as shown in execution_table rows 1 and 5.
Are NULL values sorted among themselves when using NULLS FIRST or NULLS LAST?
No, NULL values are treated as a group and placed either at the start or end without internal sorting, as seen in execution_table rows 1 and 5 where NULLs are simply added to the start.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the first NULL value placed?
AStep 3
BStep 2
CStep 1
DStep 5
💡 Hint
Check the 'NULL check' and 'Placement' columns in the first few rows.
According to variable_tracker, what is the ordered list after step 3?
A[50, 70]
B[NULL, 50, 70]
C[NULL, NULL, 50]
D[50, 70, 90]
💡 Hint
Look at the 'After 3' column for 'Ordered List' in variable_tracker.
If we change NULLS FIRST to NULLS LAST, where would the NULLs be placed?
AAt the end
BIn the middle
CAt the start
DRandomly
💡 Hint
Refer to the concept_flow diagram showing NULLS FIRST vs NULLS LAST placement.
Concept Snapshot
ORDER BY sorts rows by a column.
NULLS FIRST puts NULL values before non-NULLs.
NULLS LAST puts NULL values after non-NULLs.
Non-NULL values are sorted normally.
Use: ORDER BY column NULLS FIRST|LAST;
Full Transcript
This visual shows how PostgreSQL sorts data using ORDER BY with NULLS FIRST. The database checks each row's value in the sorting column. If the value is NULL, it places that row at the start of the result when using NULLS FIRST. Non-NULL values come after, sorted normally. The execution table traces each row's placement step-by-step. The variable tracker shows how the ordered list grows. Key moments clarify why NULLs are grouped and not sorted among themselves. The quiz tests understanding of placement steps and effects of changing NULLS FIRST to NULLS LAST.

Practice

(1/5)
1. What does ORDER BY column_name NULLS FIRST do in a PostgreSQL query?
easy
A. It ignores NULL values while sorting by column_name.
B. It sorts the rows by column_name and places NULL values at the end.
C. It causes a syntax error because NULLS FIRST is not valid.
D. It sorts the rows by column_name and places NULL values at the beginning.

Solution

  1. Step 1: Understand ORDER BY behavior with NULLS FIRST

    The ORDER BY clause sorts rows by the specified column. Adding NULLS FIRST means any NULL values in that column appear before non-NULL values.
  2. Step 2: Confirm the effect on sorting

    So, the rows with NULL in column_name come first, then the rest sorted normally.
  3. Final Answer:

    It sorts the rows by column_name and places NULL values at the beginning. -> Option D
  4. Quick Check:

    NULLS FIRST means NULLs appear first [OK]
Hint: NULLS FIRST puts empty values at the top of the list [OK]
Common Mistakes:
  • Confusing NULLS FIRST with NULLS LAST
  • Thinking NULLs are ignored in sorting
  • Assuming NULLS FIRST causes errors
2. Which of the following is the correct syntax to sort a table by column score in ascending order with NULL values last?
easy
A. SELECT * FROM table ORDER BY score ASC NULLS LAST;
B. SELECT * FROM table ORDER BY score NULLS LAST ASC;
C. SELECT * FROM table ORDER BY NULLS LAST score ASC;
D. SELECT * FROM table ORDER BY score ASC LAST NULLS;

Solution

  1. Step 1: Recall correct ORDER BY syntax with NULLS LAST

    The correct syntax places the column name first, then the sort order (ASC or DESC), followed by NULLS FIRST or NULLS LAST.
  2. Step 2: Check each option

    SELECT * FROM table ORDER BY score ASC NULLS LAST; follows this order: score ASC NULLS LAST. Others mix the order or use invalid syntax.
  3. Final Answer:

    SELECT * FROM table ORDER BY score ASC NULLS LAST; -> Option A
  4. Quick Check:

    Column, order, then NULLS position [OK]
Hint: Order: column, ASC/DESC, then NULLS FIRST/LAST [OK]
Common Mistakes:
  • Placing NULLS FIRST/LAST before column name
  • Swapping ASC/DESC and NULLS keywords
  • Using invalid keyword order causing syntax errors
3. Given the table students with column grade containing values (NULL, 85, 90, NULL, 75), what is the order of grade values after running:
SELECT grade FROM students ORDER BY grade DESC NULLS FIRST;?
medium
A. [NULL, NULL, 90, 85, 75]
B. [90, 85, 75, NULL, NULL]
C. [75, 85, 90, NULL, NULL]
D. [NULL, 90, 85, 75, NULL]

Solution

  1. Step 1: Understand DESC with NULLS FIRST

    Sorting by grade DESC means highest grades first. Adding NULLS FIRST places NULL values before all grades.
  2. Step 2: Arrange values accordingly

    NULLs come first: [NULL, NULL], then grades in descending order: 90, 85, 75.
  3. Final Answer:

    [NULL, NULL, 90, 85, 75] -> Option A
  4. Quick Check:

    DESC sorts high to low, NULLS FIRST puts NULLs upfront [OK]
Hint: NULLS FIRST always puts NULLs before sorted values [OK]
Common Mistakes:
  • Placing NULLs at the end despite NULLS FIRST
  • Sorting grades ascending instead of descending
  • Mixing order of NULLs and values
4. You wrote this query but it gives a syntax error:
SELECT * FROM employees ORDER BY salary NULLS FIRST DESC;
What is the error and how to fix it?
medium
A. The column name salary is missing ASC or DESC.
B. NULLS FIRST cannot be used with DESC; remove NULLS FIRST.
C. The order of NULLS FIRST and DESC is wrong; use ORDER BY salary DESC NULLS FIRST.
D. You must use parentheses around NULLS FIRST and DESC.

Solution

  1. Step 1: Identify correct keyword order in ORDER BY

    The correct syntax is: column name, then ASC or DESC, then NULLS FIRST or NULLS LAST.
  2. Step 2: Fix the query

    Change ORDER BY salary NULLS FIRST DESC to ORDER BY salary DESC NULLS FIRST to fix the syntax error.
  3. Final Answer:

    The order of NULLS FIRST and DESC is wrong; use ORDER BY salary DESC NULLS FIRST. -> Option C
  4. Quick Check:

    Column, order, then NULLS position [OK]
Hint: Put ASC/DESC before NULLS FIRST/LAST [OK]
Common Mistakes:
  • Placing NULLS FIRST before ASC/DESC
  • Assuming NULLS FIRST is invalid with DESC
  • Missing ASC/DESC keyword
5. You have a table products with a price column containing some NULLs. You want to list products sorted by price ascending, but show NULL prices at the end. Then, for products with the same price, sort by name ascending. Which query achieves this?
hard
A. SELECT * FROM products ORDER BY price NULLS LAST ASC, name ASC;
B. SELECT * FROM products ORDER BY price ASC NULLS LAST, name ASC;
C. SELECT * FROM products ORDER BY price ASC, name ASC NULLS LAST;
D. SELECT * FROM products ORDER BY price ASC NULLS FIRST, name ASC;

Solution

  1. Step 1: Sort by price ascending with NULLs last

    Use ORDER BY price ASC NULLS LAST to sort prices low to high and put NULLs at the end.
  2. Step 2: Add secondary sort by name ascending

    After sorting by price, add name ASC to sort products with the same price alphabetically.
  3. Final Answer:

    SELECT * FROM products ORDER BY price ASC NULLS LAST, name ASC; -> Option B
  4. Quick Check:

    Price ASC NULLS LAST, then name ASC [OK]
Hint: Chain ORDER BY: main column with NULLS, then tie-breaker column [OK]
Common Mistakes:
  • Placing NULLS LAST before ASC
  • Applying NULLS LAST to wrong column
  • Using NULLS FIRST instead of NULLS LAST
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard