0
0
SQLquery~20 mins

CASCADE delete preview in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cascade Delete Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What rows will be deleted with CASCADE?

Consider two tables: Authors and Books.
The Books table has a foreign key author_id referencing Authors(id) with ON DELETE CASCADE.

If you delete the author with id = 3, which rows in Books will be deleted?

SQL
CREATE TABLE Authors (id INT PRIMARY KEY, name VARCHAR(50));
CREATE TABLE Books (id INT PRIMARY KEY, title VARCHAR(100), author_id INT, FOREIGN KEY (author_id) REFERENCES Authors(id) ON DELETE CASCADE);

INSERT INTO Authors VALUES (1, 'Alice'), (2, 'Bob'), (3, 'Carol');
INSERT INTO Books VALUES (10, 'Book A', 1), (11, 'Book B', 3), (12, 'Book C', 3), (13, 'Book D', 2);
ABooks with id 11 and 12 will be deleted.
BBooks with id 10 and 13 will be deleted.
CNo books will be deleted.
DAll books will be deleted.
Attempts:
2 left
💡 Hint

Think about which books belong to the author being deleted.

🧠 Conceptual
intermediate
1:30remaining
Effect of CASCADE delete on child table

What is the main effect of ON DELETE CASCADE on a foreign key constraint?

ADeleting a parent row sets child foreign keys to NULL.
BDeleting a child row automatically deletes the parent row.
CDeleting a parent row automatically deletes all related child rows.
DDeleting a child row sets parent keys to NULL.
Attempts:
2 left
💡 Hint

Think about what happens to dependent rows when the main row is removed.

📝 Syntax
advanced
2:00remaining
Identify the correct CASCADE delete syntax

Which SQL statement correctly creates a foreign key with ON DELETE CASCADE?

AALTER TABLE Orders ADD CONSTRAINT fk_customer FOREIGN KEY (customer_id) REFERENCES Customers(id) DELETE CASCADE;
BALTER TABLE Orders ADD CONSTRAINT fk_customer FOREIGN KEY (customer_id) REFERENCES Customers(id) ON DELETE CASCADE;
CALTER TABLE Orders ADD FOREIGN KEY customer_id REFERENCES Customers(id) CASCADE DELETE;
DALTER TABLE Orders ADD CONSTRAINT fk_customer FOREIGN KEY (customer_id) REFERENCES Customers(id) ON DELETE SET CASCADE;
Attempts:
2 left
💡 Hint

Look for the exact phrase used to specify cascade delete.

🔧 Debug
advanced
2:30remaining
Why does this CASCADE delete not work?

Given these tables, deleting a parent row does not delete child rows. Why?

CREATE TABLE Parent (id INT PRIMARY KEY);
CREATE TABLE Child (id INT PRIMARY KEY, parent_id INT, FOREIGN KEY (parent_id) REFERENCES Parent(id));

Delete statement: DELETE FROM Parent WHERE id = 1;

AThe parent table has no primary key.
BThe delete statement syntax is incorrect.
CThe child table's foreign key references a non-existent column.
DThe foreign key lacks <code>ON DELETE CASCADE</code>, so child rows are not deleted.
Attempts:
2 left
💡 Hint

Check the foreign key definition for cascade behavior.

optimization
expert
3:00remaining
Optimizing CASCADE delete performance

You have a large parent table and a child table with millions of rows. Deleting a parent row cascades deletes to many child rows, but the operation is slow. Which approach can improve performance?

AAdd an index on the child table's foreign key column to speed up cascade deletes.
BRemove the foreign key constraint to avoid cascade overhead.
CUse triggers instead of foreign keys for cascading deletes.
DDisable transactions during delete to speed up operations.
Attempts:
2 left
💡 Hint

Think about how the database finds child rows to delete.