Hone logo
Hone
Problems

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 NULL operator for the filtering condition.
    • The query should return all columns (*) for the matching rows.
  • Expected behavior: The output should be a result set containing only the rows where the target column's value is NULL. Rows with non-NULL values in that column should be excluded.
  • Edge cases to consider:
    • The table might contain no rows with NULL values in the target column.
    • The table might contain only rows with NULL values in the target column.

Examples

Example 1:

Input Table: Products

ProductIDProductNamePriceStockCount
1Laptop1200.0050
2Keyboard75.50NULL
3Mouse25.00150
4Monitor300.00NULL
5Webcam50.0080

Query: Select all products where StockCount is NULL.

Output Table:

ProductIDProductNamePriceStockCount
2Keyboard75.50NULL
4Monitor300.00NULL

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

CustomerIDFirstNameLastNameEmail
101AliceSmithalice.s@example.com
102BobJohnsonNULL
103CharlieBrowncharlie.b@example.com
104DianaPrinceNULL

Query: Select all customers where Email is NULL.

Output Table:

CustomerIDFirstNameLastNameEmail
102BobJohnsonNULL
104DianaPrinceNULL

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

OrderIDOrderDateShipDate
12023-10-262023-10-27
22023-10-262023-10-28
32023-10-272023-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 NULL is not equal to any value, including itself. Therefore, you cannot use standard comparison operators like = or != to check for NULL.
  • The IS NULL operator is the correct way to identify NULL values.
  • You can also use IS NOT NULL to filter for rows that do have a value.
Loading editor...
plaintext