📖 Scenario: You are managing a small bookstore database. You need to add new books to the books table to keep the inventory updated.
🎯 Goal: Learn how to insert a single row into the books table using the INSERT INTO statement.
📋 What You'll Learn
Create a table named books with columns id, title, and author
Add a configuration variable for the new book's id
Write an INSERT INTO statement to add a single row with the new book's details
Complete the insertion by committing the transaction
💡 Why This Matters
🌍 Real World
Adding new records to a database is a common task in managing inventories, customer data, or any information system.
💼 Career
Database administrators and developers frequently insert data into tables to keep databases current and accurate.
Progress0 / 4 steps
1
Create the books table
Write a SQL statement to create a table called books with three columns: id as an integer primary key, title as text, and author as text.
SQL
Hint
Use CREATE TABLE books (id INTEGER PRIMARY KEY, title TEXT, author TEXT); to create the table.
2
Set the new book's ID
Create a variable called new_book_id and set it to 101 to represent the ID of the book you want to add.
SQL
Hint
Assign 101 to new_book_id like new_book_id = 101;
3
Insert a single row into books
Write an INSERT INTO statement to add a new book with id equal to new_book_id, title as 'The Great Gatsby', and author as 'F. Scott Fitzgerald' into the books table.
SQL
Hint
Use INSERT INTO books (id, title, author) VALUES (new_book_id, 'The Great Gatsby', 'F. Scott Fitzgerald');
4
Commit the transaction
Add the SQL statement to commit the transaction so the new row is saved permanently.
SQL
Hint
Use COMMIT; to save the changes.
Practice
(1/5)
1. What does the SQL statement INSERT INTO students (name, age) VALUES ('Alice', 20); do?
easy
A. Deletes the student named Alice from the students table.
B. Adds a new student named Alice aged 20 to the students table.
C. Updates the age of all students to 20.
D. Creates a new table named students with columns name and age.
Solution
Step 1: Understand the INSERT INTO statement
The statement adds data to an existing table by specifying columns and values.
Step 2: Match columns and values
The columns are name and age, and the values are 'Alice' and 20, so a new row is added with these values.
Final Answer:
Adds a new student named Alice aged 20 to the students table. -> Option B
Quick Check:
INSERT INTO adds one row [OK]
Hint: INSERT INTO adds one row with matching columns and values [OK]
Common Mistakes:
Confusing INSERT with DELETE or UPDATE
Not matching columns and values order
Using wrong quotes for text values
2. Which of the following is the correct syntax to insert a single row into the table employees with columns id and name?
easy
A. INSERT employees (id, name) VALUES (1, 'John');
B. INSERT INTO employees (id, name) SET (1, 'John');
C. INSERT INTO employees VALUES id=1, name='John';
D. INSERT INTO employees (id, name) VALUES (1, 'John');
Solution
Step 1: Recall correct INSERT syntax
The correct syntax requires the keywords INSERT INTO, table name, columns in parentheses, then VALUES with matching values.
Step 2: Check each option
INSERT INTO employees (id, name) VALUES (1, 'John'); matches the correct syntax exactly; others have missing INTO, wrong assignment, or SET keyword which is invalid here.
Final Answer:
INSERT INTO employees (id, name) VALUES (1, 'John'); -> Option D
Quick Check:
INSERT INTO ... VALUES (...) is correct syntax [OK]
Hint: Always use INSERT INTO table (columns) VALUES (values) [OK]
Common Mistakes:
Omitting INTO keyword
Using SET instead of VALUES
Incorrect assignment inside VALUES
3. Given the table products with columns product_id (integer) and product_name (text), what will be the result of this query?
INSERT INTO products (product_id, product_name) VALUES (101, 'Pen');
medium
A. A new row with product_id 101 and product_name 'Pen' is added to products.
B. The query returns an error because product_name is text and needs double quotes.
C. The query updates product_id 101 to have product_name 'Pen'.
D. No change happens because INSERT INTO requires multiple rows.
Solution
Step 1: Understand the INSERT INTO effect
The query adds one new row with specified values to the products table.
Step 2: Check syntax and data types
Text values use single quotes in SQL, so 'Pen' is correct. No error occurs.
Final Answer:
A new row with product_id 101 and product_name 'Pen' is added to products. -> Option A
Quick Check:
INSERT INTO adds one row with given values [OK]
Hint: Text values need single quotes; INSERT adds one row [OK]
Common Mistakes:
Using double quotes for text values
Thinking INSERT updates existing rows
Believing INSERT requires multiple rows
4. Identify the error in this SQL statement:
INSERT INTO orders (order_id, customer) VALUES (123, John);
medium
A. The table name orders is invalid.
B. Missing parentheses around the VALUES keyword.
C. Missing single quotes around the text value John.
D. Using double quotes instead of single quotes for John.
Solution
Step 1: Check the VALUES clause
The value John is a text string and must be enclosed in single quotes in SQL.
Step 2: Verify other syntax parts
Parentheses around VALUES are correct; table name orders is valid; double quotes are not used here.
Final Answer:
Missing single quotes around the text value John. -> Option C
Quick Check:
Text values need single quotes [OK]
Hint: Text values must be in single quotes [OK]
Common Mistakes:
Forgetting quotes around text
Using double quotes instead of single
Misplacing parentheses
5. You have a table users with columns id (integer), username (text), and email (text). Which SQL statement correctly inserts a new user with id 5, username 'maria', and email 'maria@example.com'?
hard
A. INSERT INTO users (id, username, email) VALUES (5, 'maria', 'maria@example.com');
B. INSERT INTO users VALUES (5, maria, maria@example.com);
C. INSERT INTO users (id, username, email) VALUES ('5', 'maria', maria@example.com);
D. INSERT INTO users (id, username, email) SET (5, 'maria', 'maria@example.com');
Solution
Step 1: Match columns and values correctly
The columns id, username, and email are listed, so values must be in the same order and properly quoted.
Step 2: Check value types and syntax
Integer 5 does not need quotes, text values 'maria' and 'maria@example.com' need single quotes. INSERT INTO users (id, username, email) VALUES (5, 'maria', 'maria@example.com'); matches this exactly.
Final Answer:
INSERT INTO users (id, username, email) VALUES (5, 'maria', 'maria@example.com'); -> Option A
Quick Check:
Match columns and values with correct quotes [OK]
Hint: Match columns and values; quote text, not numbers [OK]
Common Mistakes:
Not quoting text values
Using SET instead of VALUES
Quoting integers unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.