📖 Scenario: You are managing a small library database. You want to remove some books that are no longer available. But deleting data can cause problems if done carelessly.
🎯 Goal: Build a safe SQL DELETE operation that removes only specific books without affecting other data.
📋 What You'll Learn
Create a table called books with columns id, title, and available
Insert 3 specific book records into books
Add a condition variable to select only unavailable books
Write a DELETE query that deletes only books marked as unavailable
💡 Why This Matters
🌍 Real World
Deleting data is common in databases but must be done carefully to avoid losing important information.
💼 Career
Database administrators and developers must write safe DELETE queries to maintain data integrity and prevent accidental data loss.
Progress0 / 4 steps
1
Create the books table and insert data
Write SQL to create a table called books with columns id (integer), title (text), and available (boolean). Then insert these three rows exactly: (1, 'Moby Dick', true), (2, 'Hamlet', false), (3, '1984', true).
SQL
Hint
Use CREATE TABLE to define the table and INSERT INTO to add rows.
2
Add a condition variable for unavailable books
Create a variable called unavailable_condition that holds the SQL condition available = FALSE as a string.
SQL
Hint
Think of the condition you want to use in the DELETE query's WHERE clause.
3
Write the DELETE query with condition
Write a SQL DELETE statement that removes rows from books where available = FALSE.
SQL
Hint
Use DELETE FROM books WHERE available = FALSE; to remove only unavailable books.
4
Add a safety check with a transaction
Wrap the DELETE statement inside a transaction using BEGIN TRANSACTION; before and COMMIT; after the DELETE statement.
SQL
Hint
Use BEGIN TRANSACTION; before and COMMIT; after the DELETE to ensure safety.
Practice
(1/5)
1. Why should you be careful when using the DELETE statement in SQL?
easy
A. Because it creates a backup automatically
B. Because it only hides rows temporarily
C. Because it permanently removes rows from the table
D. Because it only works on empty tables
Solution
Step 1: Understand what DELETE does
The DELETE statement removes rows from a table permanently, not temporarily.
Step 2: Consider the impact of DELETE
Since rows are permanently removed, careless use can cause data loss.
Final Answer:
Because it permanently removes rows from the table -> Option C
Quick Check:
DELETE = permanent removal [OK]
Hint: Remember DELETE removes data permanently, no undo [OK]
Common Mistakes:
Thinking DELETE only hides data
Assuming DELETE creates backups
Believing DELETE works only on empty tables
2. Which of the following is the correct syntax to delete rows from a table named employees where the department is 'Sales'?
easy
A. DELETE * FROM employees WHERE department = 'Sales';
B. REMOVE FROM employees WHERE department = 'Sales';
C. DELETE employees WHERE department = 'Sales';
D. DELETE FROM employees WHERE department = 'Sales';
Solution
Step 1: Recall correct DELETE syntax
The correct syntax is DELETE FROM table_name WHERE condition;
Step 2: Check each option
DELETE FROM employees WHERE department = 'Sales'; matches the correct syntax exactly. The other options have syntax errors.
Final Answer:
DELETE FROM employees WHERE department = 'Sales'; -> Option D
Quick Check:
Correct DELETE syntax = DELETE FROM employees WHERE department = 'Sales'; [OK]
Hint: Use DELETE FROM table WHERE condition; [OK]
Common Mistakes:
Using REMOVE instead of DELETE
Omitting FROM keyword
Using DELETE * which is invalid
3. Given the table products with 5 rows, what will be the result of this query?
DELETE FROM products;
medium
A. Deletes all 5 rows from the products table
B. Deletes no rows because no WHERE clause is given
C. Deletes only the first row
D. Causes a syntax error
Solution
Step 1: Understand DELETE without WHERE
DELETE without a WHERE clause removes all rows from the table.
Step 2: Apply to products table
Since products has 5 rows, all 5 will be deleted.
Final Answer:
Deletes all 5 rows from the products table -> Option A
Quick Check:
DELETE without WHERE = all rows deleted [OK]
Hint: No WHERE means delete all rows [OK]
Common Mistakes:
Thinking DELETE without WHERE deletes no rows
Assuming only one row is deleted
Expecting a syntax error
4. Identify the error in this DELETE statement:
DELETE employees WHERE id = 10;
medium
A. id cannot be used in WHERE clause
B. Missing FROM keyword after DELETE
C. WHERE clause is not allowed in DELETE
D. Incorrect table name
Solution
Step 1: Review DELETE syntax
The correct syntax requires FROM keyword: DELETE FROM table WHERE condition;
Step 2: Check the given statement
The statement misses FROM after DELETE, causing a syntax error.
Final Answer:
Missing FROM keyword after DELETE -> Option B
Quick Check:
DELETE needs FROM keyword [OK]
Hint: Always use DELETE FROM, not DELETE alone [OK]
Common Mistakes:
Omitting FROM keyword
Thinking WHERE is not allowed
Assuming id cannot be used in WHERE
5. You want to delete only inactive users from the users table, but accidentally run:
DELETE FROM users;
What is the best way to avoid this mistake in the future?
hard
A. Always run DELETE inside a transaction and review before commit
B. Use DELETE without WHERE to remove all users quickly
C. Avoid using DELETE and only use UPDATE to mark inactive
D. Delete the entire database to start fresh
Solution
Step 1: Understand the risk of DELETE without WHERE
Running DELETE without WHERE removes all rows, causing data loss.
Step 2: Learn best practice to prevent mistakes
Using transactions allows you to review changes and rollback if needed before final commit.
Final Answer:
Always run DELETE inside a transaction and review before commit -> Option A
Quick Check:
Use transactions to avoid accidental deletes [OK]
Hint: Use transactions to review DELETE before finalizing [OK]
Common Mistakes:
Thinking DELETE without WHERE is safe
Avoiding DELETE completely instead of careful use
Deleting entire database as a solution
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.