NULL in DISTINCT, GROUP BY, and ORDER BY in SQL - Time & Space Complexity
We want to understand how the presence of NULL values affects the time it takes to run queries using DISTINCT, GROUP BY, and ORDER BY.
Specifically, how does the query time grow as the number of rows with NULLs increases?
Analyze the time complexity of this SQL snippet:
SELECT column1
FROM table1
WHERE column1 IS NULL OR column1 IS NOT NULL
GROUP BY column1
ORDER BY column1;
This query selects values from a column that may contain NULLs, groups them, and orders the results.
Look for repeated work done by the database engine:
- Primary operation: Scanning all rows to check and group values including NULLs.
- How many times: Once for each row in the table.
As the number of rows (n) grows, the database must examine each row to group and order values, including NULLs.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks and groupings |
| 100 | About 100 checks and groupings |
| 1000 | About 1000 checks and groupings |
Pattern observation: The work grows roughly in direct proportion to the number of rows.
Time Complexity: O(n log n)
This means the time to run the query grows a bit faster than the number of rows because of sorting, but still scales well as data grows.
[X] Wrong: "NULL values make the query run much slower because they are special and need extra work."
[OK] Correct: NULLs are treated like any other value during grouping and ordering, so they don't add extra repeated work beyond normal processing.
Understanding how NULLs affect query time helps you explain database behavior clearly and confidently in interviews.
"What if the query used DISTINCT instead of GROUP BY? How would the time complexity change?"
Practice
NULL values when using DISTINCT in SQL?Solution
Step 1: Understand DISTINCT behavior with NULL
In SQL,DISTINCTtreats allNULLvalues as equal, grouping them together.Step 2: Result of DISTINCT on NULL values
This means only oneNULLappears in the output, no matter how manyNULLs exist.Final Answer:
All NULL values are treated as one group and appear once. -> Option BQuick Check:
NULLs grouped once = A [OK]
- Thinking NULLs are ignored in DISTINCT
- Assuming each NULL is unique
- Believing DISTINCT errors on NULL
employees on column department?Solution
Step 1: Identify correct DISTINCT syntax
The correct SQL syntax to get unique values isSELECT DISTINCT column FROM table;. Parentheses are not used with DISTINCT.Step 2: Check each option
SELECT DISTINCT department FROM employees; uses correct syntax. SELECT UNIQUE department FROM employees; uses UNIQUE which is not standard SQL. SELECT DISTINCT(department) FROM employees; incorrectly uses parentheses. SELECT department DISTINCT FROM employees; has wrong keyword order.Final Answer:
SELECT DISTINCT department FROM employees; -> Option DQuick Check:
Standard DISTINCT syntax = D [OK]
- Using UNIQUE instead of DISTINCT
- Adding parentheses after DISTINCT
- Misplacing DISTINCT keyword
sales with column region containing values: ('East', 'West', NULL, 'East', NULL), what will be the result of this query?SELECT region FROM sales GROUP BY region ORDER BY region ASC;
Solution
Step 1: GROUP BY groups NULLs together
The GROUP BY groups all NULL values as one group. So NULL appears once.Step 2: ORDER BY ascending places NULL first
In ascending order, NULL values come before any text values. Then 'East' and 'West' follow alphabetically.Final Answer:
[NULL, 'East', 'West'] -> Option AQuick Check:
NULL first in ORDER BY ASC = A [OK]
- Assuming NULL sorts last
- Treating NULL as a string value
- Expecting multiple NULL rows in GROUP BY
category values including NULLs?SELECT category FROM products GROUP BY category ORDER BY category;
Solution
Step 1: Check syntax and logic
The query groups by category and orders by category. This is valid SQL syntax.Step 2: Understand NULL handling
GROUP BY includes NULL as a group, so NULL values appear once. ORDER BY sorts NULLs first by default.Final Answer:
Query is correct and will work as intended. -> Option AQuick Check:
GROUP BY with ORDER BY and NULLs works = C [OK]
- Thinking NULLs are excluded by GROUP BY
- Believing ORDER BY can't follow GROUP BY
- Assuming missing semicolon breaks query
orders with a column status that can be 'Pending', 'Shipped', or NULL. You want to count how many orders exist for each status, including NULLs as a separate group. Which query correctly achieves this?Solution
Step 1: Understand grouping with NULL
GROUP BY status groups NULL values as one group, so counting includes NULLs separately.Step 2: Analyze each option
SELECT status, COUNT(*) FROM orders GROUP BY status ORDER BY status ASC; groups all statuses including NULL and orders them ascending. SELECT status, COUNT(*) FROM orders GROUP BY status HAVING status IS NOT NULL ORDER BY status; excludes NULL with HAVING. SELECT status, COUNT(*) FROM orders WHERE status IS NOT NULL GROUP BY status ORDER BY status; filters NULL out in WHERE. SELECT DISTINCT status, COUNT(*) FROM orders ORDER BY status; uses DISTINCT with COUNT(*) incorrectly.Final Answer:
SELECT status, COUNT(*) FROM orders GROUP BY status ORDER BY status ASC; -> Option CQuick Check:
GROUP BY includes NULL groups = B [OK]
- Filtering NULLs out before grouping
- Using HAVING to exclude NULLs unintentionally
- Misusing DISTINCT with aggregate functions
Ready to prove it?
Pass the test and this pattern moves unknown
