Bird
Raised Fist0
SQLquery~10 mins

CASCADE delete preview in SQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - CASCADE delete preview
Delete row in Parent Table
↓
Check for Foreign Key with CASCADE
↓
Find matching rows in Child Table
↓
Delete matching rows in Child Table
↓
Repeat cascade if Child has further CASCADE FKs
↓
Finish
When you delete a row in a parent table, the database automatically deletes related rows in child tables that have CASCADE delete set on their foreign keys.
Execution Sample
SQL
DELETE FROM departments WHERE dept_id = 10;
Deletes department with id 10 and automatically deletes employees linked to it due to CASCADE.
Execution Table
StepActionTableRows AffectedNotes
1Delete department with dept_id=10departments1Parent row targeted for deletion
2Check foreign keys with CASCADEemployeesN/Aemployees.dept_id FK with CASCADE found
3Delete employees with dept_id=10employees3Child rows deleted automatically
4Check further CASCADE FKsN/AN/ANo further CASCADE relationships
5Finish deletionN/AN/AAll related rows deleted
💡 No more CASCADE foreign keys to process, deletion complete
Variable Tracker
VariableStartAfter Step 1After Step 3Final
departments_rows5444
employees_rows101077
Key Moments - 3 Insights
Why do employees get deleted when we delete a department?
Because the foreign key from employees to departments has CASCADE delete, so deleting a department automatically deletes employees linked to it (see execution_table step 3).
What happens if there are no CASCADE foreign keys?
Only the parent row is deleted; child rows remain untouched (would be shown in execution_table step 2 with no CASCADE found).
Can CASCADE delete cause multiple levels of deletions?
Yes, if child tables also have CASCADE foreign keys, deletions cascade further (execution_table step 4 checks for this).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, how many employee rows are deleted due to CASCADE?
A3
B1
C5
D0
💡 Hint
Check the 'Rows Affected' column for employees in step 3.
At which step does the database check for further CASCADE foreign keys?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column describing checking further CASCADE FKs.
If the employees table had no CASCADE foreign key, what would change in the execution table?
AStep 3 would delete employees anyway
BStep 2 would find no CASCADE FK and step 3 would be skipped
CStep 4 would delete employees
DStep 1 would delete employees
💡 Hint
Refer to step 2 and 3 where CASCADE FK presence controls deletion.
Concept Snapshot
CASCADE delete automatically removes child rows when a parent row is deleted.
Defined by foreign key constraints with ON DELETE CASCADE.
Prevents orphaned child rows.
Can cascade through multiple related tables.
Useful for maintaining data integrity easily.
Full Transcript
When you delete a row in a parent table, the database looks for foreign keys in child tables that have the CASCADE delete option. It then deletes all matching child rows automatically. This process repeats if those child tables have further CASCADE foreign keys. The execution table shows deleting a department row, then deleting employees linked to it. Variables track how many rows remain after each step. Key moments clarify why child rows delete and how cascading can go multiple levels. The quiz tests understanding of these steps and effects. CASCADE delete helps keep data consistent by cleaning up related rows automatically.

Practice

(1/5)
1. What does CASCADE DELETE do in a database?
easy
A. Creates a backup of rows before deletion
B. Prevents deletion of parent rows if child rows exist
C. Deletes only the parent row without affecting child rows
D. Automatically deletes related child rows when a parent row is deleted

Solution

  1. Step 1: Understand CASCADE DELETE behavior

    CASCADE DELETE means when you delete a parent row, all related child rows linked by foreign keys are also deleted automatically.
  2. Step 2: Compare options with this behavior

    Automatically deletes related child rows when a parent row is deleted matches this behavior exactly. Options A, B, and D describe different or incorrect behaviors.
  3. Final Answer:

    Automatically deletes related child rows when a parent row is deleted -> Option D
  4. Quick Check:

    CASCADE DELETE = auto-remove child rows [OK]
Hint: CASCADE means child rows go away with parent [OK]
Common Mistakes:
  • Thinking CASCADE prevents deletion
  • Confusing CASCADE with backup
  • Assuming child rows stay after parent deletion
2. Which SQL syntax correctly previews rows that will be deleted by CASCADE when deleting from orders table with customer_id = 5?
easy
A. SELECT * FROM customers WHERE id = 5;
B. DELETE FROM order_items WHERE customer_id = 5;
C. SELECT * FROM order_items WHERE order_id IN (SELECT id FROM orders WHERE customer_id = 5);
D. SELECT * FROM orders WHERE customer_id = 5;

Solution

  1. Step 1: Identify child rows linked to parent rows

    To preview cascade deletes, select child rows where foreign key matches parent rows to be deleted. Here, order_items link to orders by order_id.
  2. Step 2: Use subquery to find parent rows with customer_id = 5

    The subquery selects orders with customer_id = 5. The outer query selects order_items with those order_ids.
  3. Final Answer:

    SELECT * FROM order_items WHERE order_id IN (SELECT id FROM orders WHERE customer_id = 5); -> Option C
  4. Quick Check:

    Preview child rows with IN + subquery [OK]
Hint: Use IN with subquery to find child rows [OK]
Common Mistakes:
  • Trying to delete instead of preview
  • Selecting wrong table without join
  • Using wrong column in WHERE clause
3. Given tables departments and employees with employees.department_id referencing departments.id with CASCADE DELETE, what rows will this query return?
SELECT * FROM employees WHERE department_id IN (SELECT id FROM departments WHERE name = 'Sales');
medium
A. All employees who work in the Sales department
B. All departments named Sales
C. All employees regardless of department
D. No rows, syntax error

Solution

  1. Step 1: Understand the subquery

    The subquery selects department ids where the name is 'Sales'.
  2. Step 2: Outer query filters employees by those department ids

    Employees with department_id matching those ids are returned.
  3. Final Answer:

    All employees who work in the Sales department -> Option A
  4. Quick Check:

    IN subquery filters child rows correctly [OK]
Hint: IN with subquery filters child rows by parent condition [OK]
Common Mistakes:
  • Confusing employees with departments
  • Assuming syntax error
  • Ignoring the subquery effect
4. You wrote this query to preview cascade deletes:
SELECT * FROM order_items WHERE order_id = (SELECT id FROM orders WHERE customer_id = 10);

Why might this query cause an error?
medium
A. Subquery returns multiple rows but '=' expects one value
B. Table order_items does not exist
C. Missing WHERE clause in subquery
D. Incorrect column name order_id

Solution

  1. Step 1: Analyze subquery result

    The subquery selects all order ids for customer_id 10, which can be multiple rows.
  2. Step 2: Check operator usage

    The outer query uses '=' which expects a single value, causing an error if multiple rows returned.
  3. Final Answer:

    Subquery returns multiple rows but '=' expects one value -> Option A
  4. Quick Check:

    Use IN for multiple subquery results [OK]
Hint: Use IN, not =, when subquery returns many rows [OK]
Common Mistakes:
  • Using '=' with multi-row subquery
  • Assuming table or column names are wrong
  • Ignoring subquery result count
5. You want to preview all child rows that will be deleted when deleting customers with country = 'USA'. The schema has customers and orders (orders.customer_id references customers.id with CASCADE). Which query correctly previews all orders to be deleted?
hard
A. SELECT * FROM customers WHERE country = 'USA';
B. SELECT * FROM orders WHERE customer_id IN (SELECT id FROM customers WHERE country = 'USA');
C. DELETE FROM orders WHERE customer_id IN (SELECT id FROM customers WHERE country = 'USA');
D. SELECT * FROM orders WHERE country = 'USA';

Solution

  1. Step 1: Identify parent rows to delete

    Customers with country = 'USA' are the parent rows to be deleted.
  2. Step 2: Find child rows linked to those parents

    Orders with customer_id matching those customers will be deleted by CASCADE.
  3. Step 3: Write query to preview child rows

    Use IN with subquery selecting customer ids from customers where country = 'USA'.
  4. Final Answer:

    SELECT * FROM orders WHERE customer_id IN (SELECT id FROM customers WHERE country = 'USA'); -> Option B
  5. Quick Check:

    Preview child rows with IN + subquery [OK]
Hint: Preview child rows with IN and parent filter subquery [OK]
Common Mistakes:
  • Selecting from wrong table
  • Using DELETE instead of SELECT
  • Filtering child table by parent's column directly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard