0
0
SQLquery~20 mins

NULL in DISTINCT, GROUP BY, and ORDER BY in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NULL Mastery in SQL
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of DISTINCT with NULL values
Consider a table Employees with a column Department that contains some NULL values. What will be the result of the following query?
SELECT DISTINCT Department FROM Employees ORDER BY Department;
SQL
CREATE TABLE Employees (ID INT, Department VARCHAR(20));
INSERT INTO Employees VALUES (1, 'Sales'), (2, NULL), (3, 'HR'), (4, NULL), (5, 'Sales');

SELECT DISTINCT Department FROM Employees ORDER BY Department;
ANULL appears once at the top, followed by 'HR' and 'Sales' in alphabetical order
BNULL appears multiple times, one for each NULL row, followed by 'HR' and 'Sales'
C'HR' and 'Sales' appear, but NULL values are excluded
DQuery results in an error because NULL cannot be ordered
Attempts:
2 left
💡 Hint
DISTINCT removes duplicates including NULLs, and ORDER BY sorts NULLs first by default in many SQL dialects.
query_result
intermediate
2:00remaining
GROUP BY behavior with NULL values
Given a table Orders with a column Region that contains NULL values, what will be the output of this query?
SELECT Region, COUNT(*) FROM Orders GROUP BY Region ORDER BY Region;
SQL
CREATE TABLE Orders (OrderID INT, Region VARCHAR(20));
INSERT INTO Orders VALUES (1, 'East'), (2, NULL), (3, 'West'), (4, NULL), (5, 'East');

SELECT Region, COUNT(*) FROM Orders GROUP BY Region ORDER BY Region;
ATwo rows with NULL and 'East' only, 'West' is excluded
BThree rows: NULL, 'East', and 'West', with NULL last in order
CThree rows: NULL, 'East', and 'West', with NULL first in order
DQuery fails because NULL cannot be grouped
Attempts:
2 left
💡 Hint
GROUP BY treats NULL as a group, and ORDER BY sorts NULLs first by default.
📝 Syntax
advanced
2:00remaining
Ordering NULLs explicitly in ORDER BY
Which of the following queries will correctly order the Category column with NULLs appearing last?
SELECT Category FROM Products ORDER BY Category NULLS LAST;
ASELECT Category FROM Products ORDER BY Category NULLS LAST;
BSELECT Category FROM Products ORDER BY Category NULL LAST;
CSELECT Category FROM Products ORDER BY Category IS NULL, Category;
DSELECT Category FROM Products ORDER BY Category DESC NULLS LAST;
Attempts:
2 left
💡 Hint
The standard syntax to put NULLs last is 'NULLS LAST' after the column name.
optimization
advanced
2:00remaining
Optimizing GROUP BY with NULL values
You want to optimize a query that groups by a column Status which contains many NULL values. Which approach can improve performance while keeping NULLs grouped correctly?
AUse DISTINCT instead of GROUP BY to avoid NULL grouping overhead
BReplace NULLs with a placeholder value using COALESCE(Status, 'Unknown') in GROUP BY
CUse GROUP BY Status without changes; NULLs are grouped automatically
DExclude NULL rows using WHERE Status IS NOT NULL before GROUP BY
Attempts:
2 left
💡 Hint
Replacing NULLs with a fixed value can help indexes and grouping.
🧠 Conceptual
expert
3:00remaining
Understanding NULL behavior in DISTINCT, GROUP BY, and ORDER BY
Which statement correctly describes how NULL values behave in DISTINCT, GROUP BY, and ORDER BY clauses in SQL?
ANULLs cause errors in DISTINCT and GROUP BY, but ORDER BY ignores NULLs
BNULLs are treated as distinct in DISTINCT, grouped together in GROUP BY, and sorted first in ORDER BY by default in many SQL dialects
CNULLs are treated as equal in DISTINCT and GROUP BY, but ORDER BY always places NULLs at the end regardless of dialect
DNULLs are treated as equal in DISTINCT and GROUP BY, and ORDER BY placement of NULLs depends on the SQL dialect and can be controlled explicitly
Attempts:
2 left
💡 Hint
Think about how NULLs are grouped and sorted in different SQL clauses and dialects.