Challenge - 5 Problems
Master of INSERT with SELECT
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What rows are inserted by this query?
Given two tables employees and new_employees, what rows will be inserted into new_employees after running this query?
INSERT INTO new_employees (id, name, department) SELECT id, name, department FROM employees WHERE department = 'Sales';MySQL
CREATE TABLE employees (id INT, name VARCHAR(20), department VARCHAR(20)); INSERT INTO employees VALUES (1, 'Alice', 'Sales'), (2, 'Bob', 'HR'), (3, 'Charlie', 'Sales'); CREATE TABLE new_employees (id INT, name VARCHAR(20), department VARCHAR(20));
Attempts:
2 left
💡 Hint
Think about which employees belong to the 'Sales' department.
✗ Incorrect
The SELECT statement filters employees where department is 'Sales'. Only Alice and Charlie match, so only their rows are inserted.
📝 Syntax
intermediate2:00remaining
Which INSERT with SELECT syntax is correct?
Choose the correct syntax to insert data from products table into archived_products table for products with price < 100.
Both tables have columns (product_id, name, price).
Both tables have columns (product_id, name, price).
Attempts:
2 left
💡 Hint
Remember the correct order: INSERT INTO ... SELECT ...
✗ Incorrect
Option A uses the correct syntax: INSERT INTO table (columns) SELECT ... FROM ... WHERE ...; Options B, C, and D have syntax errors.
❓ optimization
advanced2:00remaining
How to optimize this INSERT with SELECT for large data?
You want to insert millions of rows from orders into archived_orders using:
Which option improves performance?
INSERT INTO archived_orders SELECT * FROM orders WHERE order_date < '2023-01-01';Which option improves performance?
Attempts:
2 left
💡 Hint
Think about how filtering large data can be faster.
✗ Incorrect
Adding an index on order_date speeds up the WHERE filtering. Removing WHERE inserts unnecessary rows. Multiple single-row inserts are slower. Disabling indexes on target table can help but is risky and not always best.
🔧 Debug
advanced2:00remaining
Why does this INSERT with SELECT fail?
Given tables users (id INT, username VARCHAR(20)) and archived_users (user_id INT, username VARCHAR(20), archived_at DATE), why does this query fail?
INSERT INTO archived_users (user_id, username) SELECT id, username FROM users;Attempts:
2 left
💡 Hint
Check if all NOT NULL columns without defaults are provided values.
✗ Incorrect
archived_at column is NOT NULL and has no default, so it must be included in INSERT or have a default. Omitting it causes failure.
🧠 Conceptual
expert2:00remaining
What happens if INSERT with SELECT returns zero rows?
If you run:
and no rows match the WHERE condition, what is the result?
INSERT INTO archive SELECT * FROM logs WHERE event_date > '2025-01-01';and no rows match the WHERE condition, what is the result?
Attempts:
2 left
💡 Hint
Think about what happens when SELECT returns empty result.
✗ Incorrect
INSERT with SELECT inserts zero rows if SELECT returns no rows. This is valid and does not cause errors.