Filtering NULL Values with IS NULL in SQL
In many real-world datasets, missing values are represented as NULL. When querying data, it's often necessary to specifically include or exclude rows where certain columns contain NULL values. This challenge will test your ability to use the IS NULL operator in SQL to precisely filter your results.
Problem Description
You are tasked with writing a SQL query that retrieves records from a given table, but only those where a specified column contains a NULL value. This is a fundamental operation for data cleaning, analysis, and reporting, ensuring you can isolate and handle missing information effectively.
- What needs to be achieved: Select all columns from a specified table for rows where a designated column's value is
NULL. - Key requirements:
- You must use the
IS NULLoperator for the filtering condition. - The query should return all columns (
*) for the matching rows.
- You must use the
- Expected behavior: The output should be a result set containing only the rows where the target column's value is
NULL. Rows with non-NULLvalues in that column should be excluded. - Edge cases to consider:
- The table might contain no rows with
NULLvalues in the target column. - The table might contain only rows with
NULLvalues in the target column.
- The table might contain no rows with
Examples
Example 1:
Input Table: Products
| ProductID | ProductName | Price | StockCount |
|---|---|---|---|
| 1 | Laptop | 1200.00 | 50 |
| 2 | Keyboard | 75.50 | NULL |
| 3 | Mouse | 25.00 | 150 |
| 4 | Monitor | 300.00 | NULL |
| 5 | Webcam | 50.00 | 80 |
Query: Select all products where StockCount is NULL.
Output Table:
| ProductID | ProductName | Price | StockCount |
|---|---|---|---|
| 2 | Keyboard | 75.50 | NULL |
| 4 | Monitor | 300.00 | NULL |
Explanation: The query filters the Products table to include only those rows where the StockCount column has a NULL value, returning the corresponding ProductID, ProductName, Price, and StockCount.
Example 2:
Input Table: Customers
| CustomerID | FirstName | LastName | |
|---|---|---|---|
| 101 | Alice | Smith | alice.s@example.com |
| 102 | Bob | Johnson | NULL |
| 103 | Charlie | Brown | charlie.b@example.com |
| 104 | Diana | Prince | NULL |
Query: Select all customers where Email is NULL.
Output Table:
| CustomerID | FirstName | LastName | |
|---|---|---|---|
| 102 | Bob | Johnson | NULL |
| 104 | Diana | Prince | NULL |
Explanation: This query identifies and returns records for customers who do not have an email address recorded (i.e., Email is NULL).
Example 3: (Edge Case)
Input Table: Orders
| OrderID | OrderDate | ShipDate |
|---|---|---|
| 1 | 2023-10-26 | 2023-10-27 |
| 2 | 2023-10-26 | 2023-10-28 |
| 3 | 2023-10-27 | 2023-10-29 |
Query: Select all orders where ShipDate is NULL.
Output Table: (Empty table)
Explanation: In this scenario, all orders have a ShipDate, so no rows satisfy the IS NULL condition for the ShipDate column. An empty result set is the correct output.
Constraints
- The table will have at least one column.
- The target column for filtering will exist in the table.
- The database system supports standard SQL syntax.
- For performance, the query should be efficient, but for this challenge, focus on correctness.
Notes
- Remember that
NULLis not equal to any value, including itself. Therefore, you cannot use standard comparison operators like=or!=to check forNULL. - The
IS NULLoperator is the correct way to identifyNULLvalues. - You can also use
IS NOT NULLto filter for rows that do have a value.