Hone logo
Hone
Problems

Filtering Records with SQL's WHERE Clause

This challenge focuses on understanding and implementing the fundamental WHERE clause in SQL. The WHERE clause is crucial for selecting specific rows from a database table based on defined conditions, allowing for targeted data retrieval and manipulation. Mastering this concept is essential for any data-driven application.

Problem Description

You are tasked with writing a SQL query that retrieves specific records from a given table based on a set of filtering conditions. You will be provided with a table schema and a set of criteria. Your query should return only the rows that satisfy all specified conditions.

Requirements:

  • Select all columns from the specified table.
  • Apply filtering conditions using the WHERE clause.
  • The conditions will involve comparing values in different columns against specific literals or other column values.
  • The query should be robust and handle cases where no records match the criteria.

Expected Behavior:

The query should return an empty result set if no records in the table meet the specified filtering criteria. Otherwise, it should return all rows that satisfy all conditions specified in the WHERE clause.

Edge Cases:

  • Conditions involving NULL values.
  • Using various comparison operators (=, !=, <, >, <=, >=).
  • Combining multiple conditions using logical operators (AND, OR).

Examples

Example 1:

Input Table: Customers

customer_idfirst_namelast_namecitystatesignup_date
1JohnDoeNew YorkNY2023-01-15
2JaneSmithLos AngelesCA2023-03-20
3PeterJonesNew YorkNY2023-05-10
4MaryBrownChicagoIL2023-01-15

Filtering Criteria:

  • city is 'New York'
  • state is 'NY'

Output:

customer_idfirst_namelast_namecitystatesignup_date
1JohnDoeNew YorkNY2023-01-15
3PeterJonesNew YorkNY2023-05-10

Explanation: The query selects records where the city column is exactly 'New York' AND the state column is exactly 'NY'.

Example 2:

Input Table: Orders

order_idcustomer_idorder_datetotal_amountstatus
10112023-02-10150.50shipped
10222023-04-0575.00pending
10312023-02-20200.00delivered
10432023-06-0199.99pending
10522023-04-15120.25shipped

Filtering Criteria:

  • status is 'pending' OR total_amount is greater than 100.00

Output:

order_idcustomer_idorder_datetotal_amountstatus
10222023-04-0575.00pending
10432023-06-0199.99pending
10112023-02-10150.50shipped
10522023-04-15120.25shipped

Explanation: The query selects records where either the status is 'pending' OR the total_amount is greater than 100.00. Notice that order_id 102 and 104 meet the 'pending' condition, while order_id 101 and 105 meet the total_amount condition.

Example 3:

Input Table: Products

product_idproduct_namecategorypricestock_quantity
P001LaptopElectronics120050
P002KeyboardElectronics75150
P003MouseElectronics25NULL
P004Desk ChairFurniture30020
P005MonitorElectronics30030

Filtering Criteria:

  • stock_quantity IS NULL

Output:

product_idproduct_namecategorypricestock_quantity
P003MouseElectronics25NULL

Explanation: This demonstrates filtering for NULL values. The IS NULL operator is specifically used for this purpose, as direct comparison with NULL using = would not work as expected.

Constraints

  • The table will contain at least one row.
  • Column names will be alphanumeric.
  • Literal values in conditions will be of appropriate data types (strings, numbers, dates).
  • You will not need to worry about aggregate functions or subqueries for this challenge.
  • The provided filtering criteria will be unambiguous.

Notes

  • Remember that SQL is case-insensitive for keywords but often case-sensitive for string literals depending on database configuration. For this challenge, assume standard case-sensitivity for string comparisons unless otherwise specified.
  • Pay close attention to the logical operators (AND, OR) and how they combine conditions.
  • When dealing with NULL values, use the IS NULL or IS NOT NULL operators.
Loading editor...
plaintext