Handling NULL Values in DISTINCT, GROUP BY, and ORDER BY
📖 Scenario: You are managing a small customer database for a local store. Some customers have not provided their email addresses, so those entries are NULL. You want to learn how SQL treats NULL values when using DISTINCT, GROUP BY, and ORDER BY clauses.
🎯 Goal: Build SQL queries that demonstrate how NULL values are handled in DISTINCT, GROUP BY, and ORDER BY clauses using a sample customers table.
📋 What You'll Learn
Create a customers table with id, name, and email columns
Insert sample data including some NULL email values
Write a query using DISTINCT on the email column
Write a query using GROUP BY on the email column
Write a query using ORDER BY on the email column
💡 Why This Matters
🌍 Real World
Handling NULL values correctly is important in real databases where some data may be missing or unknown.
💼 Career
Understanding how NULLs behave in DISTINCT, GROUP BY, and ORDER BY helps in writing accurate SQL queries for reports and data analysis.
Progress0 / 4 steps
1
Create the customers table and insert data
Create a table called customers with columns id (integer), name (text), and email (text). Insert these exact rows: (1, 'Alice', 'alice@example.com'), (2, 'Bob', NULL), (3, 'Charlie', 'charlie@example.com'), (4, 'David', NULL), (5, 'Eve', 'eve@example.com').
SQL
Hint
Use CREATE TABLE to define the table and INSERT INTO to add rows. Use NULL for missing emails.
2
Write a query using DISTINCT on email
Write a SQL query that selects distinct email values from the customers table using SELECT DISTINCT email FROM customers.
SQL
Hint
Use SELECT DISTINCT email FROM customers; to get unique emails including NULL once.
3
Write a query using GROUP BY on email
Write a SQL query that selects email and counts how many customers have each email using SELECT email, COUNT(*) FROM customers GROUP BY email.
SQL
Hint
Use GROUP BY email to group rows and COUNT(*) to count customers per email, including NULL group.
4
Write a query using ORDER BY on email
Write a SQL query that selects all columns from customers and orders the results by email using SELECT * FROM customers ORDER BY email.
SQL
Hint
Use ORDER BY email to sort customers by email. NULL values usually appear first or last depending on the database.
Practice
(1/5)
1. What happens to NULL values when using DISTINCT in SQL?
easy
A. NULL values are ignored and not shown in the result.
B. All NULL values are treated as one group and appear once.
C. Each NULL is treated as a unique value and appears multiple times.
D. DISTINCT causes an error if NULL values exist.
Solution
Step 1: Understand DISTINCT behavior with NULL
In SQL, DISTINCT treats all NULL values as equal, grouping them together.
Step 2: Result of DISTINCT on NULL values
This means only one NULL appears in the output, no matter how many NULLs exist.
Final Answer:
All NULL values are treated as one group and appear once. -> Option B
Quick Check:
NULLs grouped once = A [OK]
Hint: NULLs count as one in DISTINCT and GROUP BY [OK]
Common Mistakes:
Thinking NULLs are ignored in DISTINCT
Assuming each NULL is unique
Believing DISTINCT errors on NULL
2. Which of the following is the correct syntax to select distinct values including NULLs from a table named employees on column department?
easy
A. SELECT department DISTINCT FROM employees;
B. SELECT UNIQUE department FROM employees;
C. SELECT DISTINCT(department) FROM employees;
D. SELECT DISTINCT department FROM employees;
Solution
Step 1: Identify correct DISTINCT syntax
The correct SQL syntax to get unique values is SELECT 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 D
Quick Check:
Standard DISTINCT syntax = D [OK]
Hint: Use DISTINCT without parentheses for unique values [OK]
Common Mistakes:
Using UNIQUE instead of DISTINCT
Adding parentheses after DISTINCT
Misplacing DISTINCT keyword
3. Given the table 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;
medium
A. [NULL, 'East', 'West']
B. ['East', 'West', NULL]
C. ['East', NULL, 'West']
D. [NULL, 'West', 'East']
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 A
Quick Check:
NULL first in ORDER BY ASC = A [OK]
Hint: ORDER BY ASC puts NULL before text values [OK]
Common Mistakes:
Assuming NULL sorts last
Treating NULL as a string value
Expecting multiple NULL rows in GROUP BY
4. What is wrong with this query if the goal is to list unique category values including NULLs?
SELECT category FROM products GROUP BY category ORDER BY category;
medium
A. Query is correct and will work as intended.
B. ORDER BY cannot be used with GROUP BY.
C. Missing closing semicolon causes syntax error.
D. GROUP BY ignores NULL values, so NULLs won't appear.
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 A
Quick Check:
GROUP BY with ORDER BY and NULLs works = C [OK]
Hint: GROUP BY includes NULLs; ORDER BY works with GROUP BY [OK]
Common Mistakes:
Thinking NULLs are excluded by GROUP BY
Believing ORDER BY can't follow GROUP BY
Assuming missing semicolon breaks query
5. You have a table 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?
hard
A. SELECT status, COUNT(*) FROM orders GROUP BY status HAVING status IS NOT NULL ORDER BY status;
B. SELECT status, COUNT(*) FROM orders WHERE status IS NOT NULL GROUP BY status ORDER BY status;
C. SELECT status, COUNT(*) FROM orders GROUP BY status ORDER BY status ASC;
D. SELECT DISTINCT status, COUNT(*) FROM orders ORDER BY status;
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 C
Quick Check:
GROUP BY includes NULL groups = B [OK]
Hint: Use GROUP BY without WHERE to include NULL counts [OK]
Common Mistakes:
Filtering NULLs out before grouping
Using HAVING to exclude NULLs unintentionally
Misusing DISTINCT with aggregate functions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.