Bird
Raised Fist0
SQLquery~20 mins

ORDER BY with NULL values behavior in SQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
ORDER BY NULL Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
How does ORDER BY sort NULLs by default?
Consider a table Employees with a column salary that contains some NULL values. What is the order of salaries when you run this query?

SELECT salary FROM Employees ORDER BY salary ASC;

Assume the salaries are: 50000, NULL, 70000, 60000.
SQL
CREATE TABLE Employees (salary INT);
INSERT INTO Employees VALUES (50000), (NULL), (70000), (60000);
SELECT salary FROM Employees ORDER BY salary ASC;
A[50000, 60000, 70000, NULL]
B[NULL, 50000, 60000, 70000]
C[70000, 60000, 50000, NULL]
D[60000, 70000, 50000, NULL]
Attempts:
2 left
💡 Hint
Think about where NULL values appear when sorting ascending by default in most SQL databases.
❓ query_result
intermediate
2:00remaining
How does ORDER BY sort NULLs with DESC?
Using the same Employees table, what is the order of salaries when you run:

SELECT salary FROM Employees ORDER BY salary DESC;

Given salaries: 50000, NULL, 70000, 60000.
SQL
CREATE TABLE Employees (salary INT);
INSERT INTO Employees VALUES (50000), (NULL), (70000), (60000);
SELECT salary FROM Employees ORDER BY salary DESC;
A[NULL, 70000, 60000, 50000]
B[70000, 60000, 50000, NULL]
C[50000, 60000, 70000, NULL]
D[60000, 70000, 50000, NULL]
Attempts:
2 left
💡 Hint
Consider where NULLs appear when sorting descending by default.
📝 Syntax
advanced
2:00remaining
Which query correctly places NULLs first when ordering ascending?
You want to sort the salary column ascending but have NULL values appear first. Which query does this correctly?
ASELECT salary FROM Employees ORDER BY NULLS FIRST salary ASC;
BSELECT salary FROM Employees ORDER BY salary NULLS FIRST ASC;
CSELECT salary FROM Employees ORDER BY salary ASC NULLS FIRST;
DSELECT salary FROM Employees ORDER BY salary ASC FIRST NULLS;
Attempts:
2 left
💡 Hint
The NULLS FIRST clause comes after ASC or DESC in standard SQL.
🧠 Conceptual
advanced
2:00remaining
Why might NULLS FIRST or NULLS LAST be important in sorting?
Which of these best explains why you might want to use NULLS FIRST or NULLS LAST in an ORDER BY clause?
ATo change the data type of NULL values to a default value.
BTo convert NULL values into zeros before sorting.
CTo automatically remove rows with NULL values from the result set.
DTo control whether missing or unknown values appear at the start or end of sorted results for clearer data interpretation.
Attempts:
2 left
💡 Hint
Think about how NULLs represent missing data and how their position affects reading sorted data.
🔧 Debug
expert
2:00remaining
Identify the error in this ORDER BY with NULLS clause
This query is intended to sort salaries ascending with NULLs last, but it causes an error:

SELECT salary FROM Employees ORDER BY salary NULLS LAST ASC;

What is the cause of the error?
SQL
SELECT salary FROM Employees ORDER BY salary NULLS LAST ASC;
AThe NULLS LAST clause must come after ASC or DESC, not before.
BNULLS LAST is not supported in ORDER BY clauses.
CThe salary column must be cast to a non-nullable type first.
DThe query is missing a WHERE clause to filter NULLs.
Attempts:
2 left
💡 Hint
Check the order of keywords in the ORDER BY clause.

Practice

(1/5)
1. What does the ORDER BY clause do when sorting a column that contains NULL values by default in SQL?
easy
A. It treats NULL as zero and sorts accordingly.
B. It places NULL values at the beginning when sorting in ascending order.
C. It places NULL values at the end when sorting in ascending order.
D. It causes a syntax error if NULL values exist.

Solution

  1. Step 1: Understand default ORDER BY behavior

    By default, when sorting ascending, SQL places NULL values last because NULL means unknown or missing data.
  2. Step 2: Confirm NULL placement in ascending order

    Since NULL is considered neither high nor low, it appears after all actual values in ascending order.
  3. Final Answer:

    It places NULL values at the end when sorting in ascending order. -> Option C
  4. Quick Check:

    Default ascending sort puts NULL last [OK]
Hint: Remember: NULLs come last by default in ascending order [OK]
Common Mistakes:
  • Thinking NULLs are treated as zero
  • Assuming NULLs appear first by default
  • Believing NULLs cause errors in ORDER BY
2. Which of the following is the correct syntax to sort a column named score in ascending order but place NULL values first?
easy
A. ORDER BY score ASC NULLS FIRST
B. ORDER BY score ASC NULLS LAST
C. ORDER BY score NULLS FIRST ASC
D. ORDER BY NULLS FIRST score ASC

Solution

  1. Step 1: Recall correct syntax for NULL placement

    The correct syntax places NULLS FIRST or NULLS LAST after the sort direction (ASC or DESC).
  2. Step 2: Match syntax with options

    ORDER BY score ASC NULLS FIRST uses ORDER BY score ASC NULLS FIRST, which is the correct order of keywords.
  3. Final Answer:

    ORDER BY score ASC NULLS FIRST -> Option A
  4. Quick Check:

    NULLS FIRST comes after ASC/DESC [OK]
Hint: Put NULLS FIRST or LAST after ASC/DESC in ORDER BY [OK]
Common Mistakes:
  • Placing NULLS FIRST before column name
  • Mixing order of ASC/DESC and NULLS FIRST
  • Using NULLS FIRST without ASC or DESC
3. Given the table students with column grade containing values (85, NULL, 90, 70), what is the result of this query?
SELECT grade FROM students ORDER BY grade DESC NULLS LAST;
medium
A. [NULL, 90, 85, 70]
B. [90, 85, 70, NULL]
C. [90, 85, 70]
D. [70, 85, 90, NULL]

Solution

  1. Step 1: Sort grades descending with NULLs last

    Descending order sorts from highest to lowest: 90, 85, 70. NULLs go last as specified.
  2. Step 2: Arrange final output

    So the order is 90, 85, 70, then NULL at the end.
  3. Final Answer:

    [90, 85, 70, NULL] -> Option B
  4. Quick Check:

    DESC with NULLS LAST puts NULL at end [OK]
Hint: DESC sorts high to low; NULLS LAST puts NULL at end [OK]
Common Mistakes:
  • Placing NULL at start despite NULLS LAST
  • Ignoring NULLS LAST and using default NULL position
  • Sorting ascending instead of descending
4. Identify the error in this SQL query:
SELECT name FROM employees ORDER BY salary NULLS FIRST ASC;
medium
A. The order of NULLS FIRST and ASC is incorrect.
B. NULLS FIRST cannot be used with ASC.
C. Missing comma between salary and NULLS FIRST.
D. The query is correct and will run without errors.

Solution

  1. Step 1: Check correct syntax order for ORDER BY

    The correct syntax is ORDER BY salary ASC NULLS FIRST. NULLS FIRST must come after ASC or DESC.
  2. Step 2: Identify error in given query

    The query places NULLS FIRST before ASC, which is invalid syntax.
  3. Final Answer:

    The order of NULLS FIRST and ASC is incorrect. -> Option A
  4. Quick Check:

    NULLS FIRST must follow ASC/DESC [OK]
Hint: NULLS FIRST/LAST always after ASC or DESC [OK]
Common Mistakes:
  • Placing NULLS FIRST before ASC/DESC
  • Adding commas in ORDER BY clause
  • Assuming NULLS FIRST is invalid with ASC
5. You want to list products by price ascending but ensure that products with unknown price (NULL) appear at the top. Which query achieves this correctly?
hard
A. SELECT * FROM products ORDER BY price ASC NULLS LAST;
B. SELECT * FROM products ORDER BY price NULLS FIRST ASC;
C. SELECT * FROM products ORDER BY NULLS FIRST price ASC;
D. SELECT * FROM products ORDER BY price ASC NULLS FIRST;

Solution

  1. Step 1: Understand requirement for NULL placement

    We want ascending order but NULL values first, so use NULLS FIRST after ASC.
  2. Step 2: Check syntax correctness

    SELECT * FROM products ORDER BY price ASC NULLS FIRST; uses ORDER BY price ASC NULLS FIRST, which is valid and meets the requirement.
  3. Final Answer:

    SELECT * FROM products ORDER BY price ASC NULLS FIRST; -> Option D
  4. Quick Check:

    ASC with NULLS FIRST puts NULLs on top [OK]
Hint: Use ASC NULLS FIRST to put NULLs at top in ascending sort [OK]
Common Mistakes:
  • Placing NULLS FIRST before ASC
  • Using NULLS LAST instead of NULLS FIRST
  • Misordering keywords causing syntax errors
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