We use NULL to represent missing or unknown values in data. Understanding how NULL behaves with DISTINCT, GROUP BY, and ORDER BY helps us organize and summarize data correctly.
NULL in DISTINCT, GROUP BY, and ORDER BY in SQL
SELECT DISTINCT column_name FROM table_name; SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name; SELECT column_name FROM table_name ORDER BY column_name [ASC|DESC];
NULL values are treated as equal in DISTINCT and GROUP BY, so they appear as one group or one distinct value.
In ORDER BY, NULLs usually appear first in ascending order and last in descending order, but this can vary by database.
SELECT DISTINCT city FROM customers;
SELECT city, COUNT(*) FROM customers GROUP BY city;
SELECT city FROM customers ORDER BY city ASC;
This example shows how NULL values appear in DISTINCT, GROUP BY, and ORDER BY results.
CREATE TABLE customers (id INT, city VARCHAR(20)); INSERT INTO customers VALUES (1, 'New York'), (2, NULL), (3, 'Los Angeles'), (4, NULL), (5, 'New York'); -- Find distinct cities SELECT DISTINCT city FROM customers; -- Count customers per city SELECT city, COUNT(*) FROM customers GROUP BY city; -- Order customers by city SELECT city FROM customers ORDER BY city ASC;
NULL is not the same as an empty string or zero; it means unknown or missing.
Some databases allow controlling NULL order in ORDER BY with NULLS FIRST or NULLS LAST.
NULL values are grouped together as one in DISTINCT and GROUP BY.
ORDER BY usually places NULLs before other values in ascending order.
Knowing how NULL behaves helps avoid surprises in data results.
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
