Challenge - 5 Problems
Null Sorting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of ORDER BY with NULLS FIRST
Given the table employees with a column
salary containing some NULL values, what is the output order of salaries for the query:SELECT salary FROM employees ORDER BY salary NULLS FIRST;Assuming salaries are: 50000, NULL, 70000, NULL, 60000.
PostgreSQL
CREATE TEMP TABLE employees (salary INTEGER); INSERT INTO employees VALUES (50000), (NULL), (70000), (NULL), (60000); SELECT salary FROM employees ORDER BY salary NULLS FIRST;
Attempts:
2 left
💡 Hint
Remember that NULLS FIRST places all NULL values before any non-NULL values in the order.
✗ Incorrect
The ORDER BY clause with NULLS FIRST sorts all NULL values before any non-NULL values. Then the non-NULL values are sorted ascending by default.
❓ query_result
intermediate2:00remaining
Output of ORDER BY with NULLS LAST
Given the table products with a column
price containing some NULL values, what is the output order of prices for the query:SELECT price FROM products ORDER BY price NULLS LAST;Assuming prices are: 100, NULL, 50, NULL, 75.
PostgreSQL
CREATE TEMP TABLE products (price INTEGER); INSERT INTO products VALUES (100), (NULL), (50), (NULL), (75); SELECT price FROM products ORDER BY price NULLS LAST;
Attempts:
2 left
💡 Hint
NULLS LAST places all NULL values after the non-NULL values in the order.
✗ Incorrect
The ORDER BY clause with NULLS LAST sorts all non-NULL values first in ascending order, then places NULL values at the end.
📝 Syntax
advanced1:30remaining
Identify the syntax error in ORDER BY with NULLS FIRST
Which of the following queries will cause a syntax error in PostgreSQL?
Attempts:
2 left
💡 Hint
Check the correct order of keywords in the ORDER BY clause for NULLS placement.
✗ Incorrect
The correct syntax is 'ORDER BY column [ASC|DESC] NULLS {FIRST|LAST}'. Option D reverses NULLS and FIRST causing a syntax error.
❓ query_result
advanced2:00remaining
Effect of ORDER BY DESC NULLS FIRST
Given the table scores with column
points containing values: 10, NULL, 20, NULL, 15, what is the output order of points for the query:SELECT points FROM scores ORDER BY points DESC NULLS FIRST;
PostgreSQL
CREATE TEMP TABLE scores (points INTEGER); INSERT INTO scores VALUES (10), (NULL), (20), (NULL), (15); SELECT points FROM scores ORDER BY points DESC NULLS FIRST;
Attempts:
2 left
💡 Hint
NULLS FIRST places NULLs before non-NULLs even when sorting descending.
✗ Incorrect
The query orders NULL values first, then non-NULL values in descending order: NULL, NULL, 20, 15, 10.
🧠 Conceptual
expert2:30remaining
Why use NULLS FIRST or NULLS LAST in ORDER BY?
Which of the following best explains why you might want to use
NULLS FIRST or NULLS LAST in an ORDER BY clause?Attempts:
2 left
💡 Hint
Think about how NULL values behave in sorting and why you might want to control their position.
✗ Incorrect
NULLS FIRST and NULLS LAST let you decide if NULL values appear at the start or end of the sorted list. By default, NULLs sort last in ASC and first in DESC, which can be confusing.