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
DELETEstatement. - The
DELETEstatement should target theProductstable. - The condition for deletion must be based on the
stock_quantitycolumn. Records withstock_quantityless than a givenminimum_stock_levelshould 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
DELETEstatement should execute without errors and the table should remain unchanged. - All records deleted: If all records in the table have a
stock_quantitybelow theminimum_stock_level, all rows should be deleted.
Examples
Example 1:
Initial Products Table:
| product_id | product_name | stock_quantity |
|---|---|---|
| 1 | Apple | 50 |
| 2 | Banana | 15 |
| 3 | Cherry | 100 |
| 4 | Date | 5 |
| 5 | Elderberry | 20 |
minimum_stock_level = 20
Output (after executing DELETE):
| product_id | product_name | stock_quantity |
|---|---|---|
| 1 | Apple | 50 |
| 3 | Cherry | 100 |
| 5 | Elderberry | 20 |
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_id | product_name | stock_quantity |
|---|---|---|
| 10 | Fig | 5 |
| 11 | Grape | 8 |
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_id | product_name | stock_quantity |
|---|---|---|
| 20 | Honeydew | 30 |
| 21 | Iced Tea | 45 |
minimum_stock_level = 25
Output (after executing DELETE):
| product_id | product_name | stock_quantity |
|---|---|---|
| 20 | Honeydew | 30 |
| 21 | Iced Tea | 45 |
Explanation: No records had a stock_quantity less than 25, so no records were deleted.
Constraints
- The
Productstable will contain at least one row. - The
stock_quantitycolumn will always contain non-negative integer values. - The
minimum_stock_levelwill be a non-negative integer. - Your solution should be a single SQL
DELETEstatement.
Notes
- Consider the syntax for
DELETEstatements in SQL, specifically how to specify the table and the condition. - Be careful with the comparison operator used in your
WHEREclause. - It is good practice to understand the potential impact of a
DELETEstatement before executing it in a production environment. You might consider running aSELECTstatement with the sameWHEREclause first to preview which rows would be affected.