Hone logo
Hone
Problems

Deleting Records Based on a Condition

This challenge focuses on the fundamental SQL operation of deleting data. You will be tasked with removing specific records from a table based on a given condition. Mastering DELETE statements is crucial for data management and maintaining the integrity of your databases.

Problem Description

Your task is to implement a SQL DELETE statement that removes records from a table named Products. The records to be deleted are those where the stock_quantity is less than a specified threshold.

Requirements:

  • You must use a DELETE statement.
  • The DELETE statement should target the Products table.
  • The condition for deletion must be based on the stock_quantity column. Records with stock_quantity less than a given minimum_stock_level should be removed.

Expected Behavior:

After executing your DELETE statement, the Products table should no longer contain any rows where the stock_quantity is below the provided minimum_stock_level. Rows with stock_quantity equal to or greater than the minimum_stock_level should remain untouched.

Edge Cases:

  • No records to delete: If no records meet the deletion criteria, the DELETE statement should execute without errors and the table should remain unchanged.
  • All records deleted: If all records in the table have a stock_quantity below the minimum_stock_level, all rows should be deleted.

Examples

Example 1:

Initial Products Table:

product_idproduct_namestock_quantity
1Apple50
2Banana15
3Cherry100
4Date5
5Elderberry20

minimum_stock_level = 20

Output (after executing DELETE):

product_idproduct_namestock_quantity
1Apple50
3Cherry100
5Elderberry20

Explanation: Records with product_id 2 (stock_quantity 15) and 4 (stock_quantity 5) were deleted because their stock_quantity was less than 20.

Example 2:

Initial Products Table:

product_idproduct_namestock_quantity
10Fig5
11Grape8

minimum_stock_level = 10

Output (after executing DELETE): (Empty Table)

Explanation: Both records had a stock_quantity less than 10, so both were deleted.

Example 3: No records to delete

Initial Products Table:

product_idproduct_namestock_quantity
20Honeydew30
21Iced Tea45

minimum_stock_level = 25

Output (after executing DELETE):

product_idproduct_namestock_quantity
20Honeydew30
21Iced Tea45

Explanation: No records had a stock_quantity less than 25, so no records were deleted.

Constraints

  • The Products table will contain at least one row.
  • The stock_quantity column will always contain non-negative integer values.
  • The minimum_stock_level will be a non-negative integer.
  • Your solution should be a single SQL DELETE statement.

Notes

  • Consider the syntax for DELETE statements in SQL, specifically how to specify the table and the condition.
  • Be careful with the comparison operator used in your WHERE clause.
  • It is good practice to understand the potential impact of a DELETE statement before executing it in a production environment. You might consider running a SELECT statement with the same WHERE clause first to preview which rows would be affected.
Loading editor...
plaintext