Bird
Raised Fist0
SQLquery~20 mins

Common INSERT errors and fixes in SQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Insert Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2: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');
ASyntax error due to multiple rows in VALUES clause.
BBoth rows are inserted successfully.
COnly the first row is inserted; the second causes a primary key violation error.
DThe second row overwrites the first row silently.
Attempts:
2 left
💡 Hint
Primary keys must be unique. What happens if you insert duplicate keys?
❓ query_result
intermediate
2: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');
AData type mismatch error because 'free' is not a valid decimal.
BThe row is inserted with NULL price.
CSyntax error due to quotes around 'free'.
DNo error; 'free' is converted to 0.00.
Attempts:
2 left
💡 Hint
Check the data type of the price column and the value provided.
📝 Syntax
advanced
2: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);
AINSERT INTO orders VALUES (order_id, customer_id) (1001, 2002);
BINSERT INTO orders VALUES order_id, customer_id (1001, 2002);
CINSERT orders INTO (order_id, customer_id) VALUES (1001, 2002);
DINSERT INTO orders (order_id, customer_id) VALUES (1001, 2002);
Attempts:
2 left
💡 Hint
Check the correct syntax for specifying columns in INSERT.
❓ query_result
advanced
2: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');
ARow inserted with department set to 'Sales'.
BError because department column is missing.
CRow inserted with department set to NULL.
DRow inserted but department column is empty string.
Attempts:
2 left
💡 Hint
What happens when a column has a default value and is not specified in INSERT?
🔧 Debug
expert
2:00remaining
Why does this INSERT fail with a foreign key error?
Tables:
  • departments: dept_id INT PRIMARY KEY, dept_name VARCHAR(50)
  • employees: emp_id INT PRIMARY KEY, name VARCHAR(50), dept_id INT FOREIGN KEY REFERENCES departments(dept_id)
Query:
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);
ARow inserted with dept_id set to NULL automatically.
BForeign key constraint violation error occurs.
CSyntax error due to missing department.
DRow inserted successfully; foreign key is ignored.
Attempts:
2 left
💡 Hint
Foreign keys require referenced values to exist.

Practice

(1/5)
1. What is a common cause of an INSERT statement failing in SQL?
easy
A. Adding extra spaces between keywords
B. Using SELECT instead of INSERT
C. Missing a value for a column that does not allow NULLs
D. Writing the table name in lowercase

Solution

  1. Step 1: Understand column constraints

    Columns that do not allow NULL values must have a value provided during insert.
  2. Step 2: Identify missing values impact

    If a required column is missing a value, the insert will fail with an error.
  3. Final Answer:

    Missing a value for a column that does not allow NULLs -> Option C
  4. Quick Check:

    Required columns need values [OK]
Hint: Always provide values for NOT NULL columns [OK]
Common Mistakes:
  • Ignoring NOT NULL constraints
  • Assuming case sensitivity causes errors
  • Confusing SELECT with INSERT
2. Which of the following INSERT statements is syntactically correct for a table users(id, name)?
easy
A. INSERT INTO users VALUES (1, 'Alice');
B. INSERT users INTO VALUES (1, 'Alice');
C. INSERT INTO users (1, 'Alice');
D. INSERT INTO users VALUES 1, 'Alice';

Solution

  1. Step 1: Review correct INSERT syntax

    The correct syntax is: INSERT INTO table_name VALUES (value1, value2, ...);
  2. Step 2: Check each option

    INSERT INTO users VALUES (1, 'Alice'); matches the correct syntax exactly; others have misplaced keywords or missing parentheses.
  3. Final Answer:

    INSERT INTO users VALUES (1, 'Alice'); -> Option A
  4. Quick Check:

    INSERT INTO table VALUES (...) [OK]
Hint: Remember: INSERT INTO table VALUES (values) [OK]
Common Mistakes:
  • Misplacing keywords order
  • Omitting parentheses around values
  • Using wrong keyword sequence
3. Given the table products(id INT PRIMARY KEY, name VARCHAR(20)), what happens when you run:
INSERT INTO products VALUES (1, 'Pen');
INSERT INTO products VALUES (1, 'Pencil');
medium
A. Syntax error on second insert
B. Both rows are inserted successfully
C. Second insert overwrites the first row
D. Second insert fails due to duplicate primary key

Solution

  1. Step 1: Understand primary key constraint

    Primary keys must be unique; duplicate values cause insert failure.
  2. Step 2: Analyze the inserts

    The first insert adds id=1. The second tries to add id=1 again, causing a duplicate key error.
  3. Final Answer:

    Second insert fails due to duplicate primary key -> Option D
  4. Quick Check:

    Primary key duplicates cause insert errors [OK]
Hint: Primary keys must be unique; duplicates fail [OK]
Common Mistakes:
  • Assuming duplicates overwrite existing rows
  • Ignoring primary key uniqueness
  • Expecting syntax error instead of constraint error
4. You run this query:
INSERT INTO employees (id, name, age) VALUES (5, 'John');
What is the error and how to fix it?
medium
A. Syntax error: missing value for age; add the missing value
B. No error; query runs fine
C. Wrong table name; change employees to employee
D. Age column must be removed from column list

Solution

  1. Step 1: Check column-value count

    Columns listed are 3, but only 2 values provided; this causes an error.
  2. Step 2: Fix by adding missing value

    Provide a value for age or remove it from the column list to match values count.
  3. Final Answer:

    Syntax error: missing value for age; add the missing value -> Option A
  4. Quick Check:

    Columns and values count must match [OK]
Hint: Match columns count with values count [OK]
Common Mistakes:
  • Providing fewer values than columns
  • Assuming missing values default automatically
  • Ignoring syntax errors from mismatch
5. You want to insert multiple rows into orders(order_id INT PRIMARY KEY, product VARCHAR(50), quantity INT). Which statement correctly inserts two rows without errors?
hard
A. INSERT INTO orders VALUES (101, 'Book', 3); INSERT INTO orders VALUES (101, 'Pen', 5);
B. INSERT INTO orders VALUES (101, 'Book', 3), (102, 'Pen', 5);
C. INSERT INTO orders (order_id, product) VALUES (101, 'Book'), (102, 'Pen');
D. INSERT INTO orders VALUES (101, 'Book', 'three'), (102, 'Pen', 5);

Solution

  1. Step 1: Check multiple row insert syntax

    INSERT INTO orders VALUES (101, 'Book', 3), (102, 'Pen', 5); uses correct syntax to insert multiple rows in one statement.
  2. Step 2: Validate data types and uniqueness

    INSERT INTO orders VALUES (101, 'Book', 3), (102, 'Pen', 5); provides unique order_id and correct data types; others have duplicates, missing columns, or wrong types.
  3. Final Answer:

    INSERT INTO orders VALUES (101, 'Book', 3), (102, 'Pen', 5); -> Option B
  4. Quick Check:

    Multiple rows insert with unique keys and correct types [OK]
Hint: Use comma-separated rows with matching columns [OK]
Common Mistakes:
  • Duplicate primary keys in multiple inserts
  • Missing required columns in multi-row insert
  • Wrong data types causing errors
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