0
0
SQLquery~20 mins

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

Choose your learning style9 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.