Challenge - 5 Problems
Insert Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this INSERT query?
Consider a table users with columns
id INT PRIMARY KEY and name VARCHAR(50). What happens when you run this query?INSERT INTO users (id, name) VALUES (1, 'Alice'), (1, 'Bob');
SQL
INSERT INTO users (id, name) VALUES (1, 'Alice'), (1, 'Bob');
Attempts:
2 left
💡 Hint
Primary keys must be unique. What happens if you insert duplicate keys?
✗ Incorrect
The primary key column 'id' must have unique values. Trying to insert two rows with the same 'id' value 1 causes a primary key violation error on the second row.
❓ query_result
intermediate2:00remaining
What error does this INSERT cause?
Given a table products with columns
product_id INT, name VARCHAR(100), and price DECIMAL(5,2), what error occurs when running:INSERT INTO products (product_id, name, price) VALUES (101, 'Pen', 'free');
SQL
INSERT INTO products (product_id, name, price) VALUES (101, 'Pen', 'free');
Attempts:
2 left
💡 Hint
Check the data type of the price column and the value provided.
✗ Incorrect
The price column expects a decimal number. The string 'free' cannot be converted to a decimal, causing a data type mismatch error.
📝 Syntax
advanced2:00remaining
Which option fixes the syntax error in this INSERT?
This query causes a syntax error:
INSERT INTO orders order_id, customer_id VALUES (1001, 2002);Which option fixes it?
SQL
INSERT INTO orders order_id, customer_id VALUES (1001, 2002);
Attempts:
2 left
💡 Hint
Check the correct syntax for specifying columns in INSERT.
✗ Incorrect
The column list must be enclosed in parentheses immediately after the table name.
❓ query_result
advanced2:00remaining
What happens if you omit columns in INSERT?
Given a table employees with columns
id INT PRIMARY KEY, name VARCHAR(50), and department VARCHAR(50) DEFAULT 'Sales', what is the result of:INSERT INTO employees (id, name) VALUES (10, 'John');
SQL
INSERT INTO employees (id, name) VALUES (10, 'John');
Attempts:
2 left
💡 Hint
What happens when a column has a default value and is not specified in INSERT?
✗ Incorrect
If a column has a default value and is omitted in the INSERT, the default value is used.
🔧 Debug
expert2:00remaining
Why does this INSERT fail with a foreign key error?
Tables:
Assuming no department with
- departments:
dept_id INT PRIMARY KEY,dept_name VARCHAR(50) - employees:
emp_id INT PRIMARY KEY,name VARCHAR(50),dept_id INTFOREIGN KEY REFERENCES departments(dept_id)
INSERT INTO employees (emp_id, name, dept_id) VALUES (1, 'Anna', 5);
Assuming no department with
dept_id = 5 exists, what happens?SQL
INSERT INTO employees (emp_id, name, dept_id) VALUES (1, 'Anna', 5);
Attempts:
2 left
💡 Hint
Foreign keys require referenced values to exist.
✗ Incorrect
The foreign key constraint requires that dept_id in employees must exist in departments. Since dept_id 5 does not exist, the insert fails.