Skip to main content
SQL Fundamentals
CHAPTER 06 Beginner

INSERT Queries and Adding Data

Updated: May 16, 2026
15 min read

# CHAPTER 6

INSERT Queries and Adding Data

1. Introduction

A beautifully designed database with perfect column data types is useless if it doesn't contain any information. The very first step of the application lifecycle is Create. In SQL, we execute data creation using the DML (Data Manipulation Language) command called INSERT INTO. In this chapter, we will learn the precise syntax required to write new rows of data into our tables, handling text, numbers, and multiple records simultaneously.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Understand the syntax of the INSERT INTO statement.
  • Add a single row of data into a table.
  • Insert data into specific, targeted columns.
  • Perform bulk inserts to add multiple rows in one command.
  • Understand how strings and numbers are formatted in SQL queries.

3. The INSERT INTO Syntax

The INSERT command requires two sets of parentheses:
  1. 1. The columns you want to fill.
  1. 2. The VALUES you want to put into those columns.

*Important Syntax Rule:*

  • Numbers (INT, DECIMAL) do not need quotes. e.g., 50
  • Strings/Text (VARCHAR, DATE) MUST be wrapped in single quotes! e.g., 'Alice' or '2023-01-01'.

4. Inserting a Single Row

Let's assume we have a table called employees with three columns: id, name, and salary.
sql
123
-- Insert a brand new employee into the table
INSERT INTO employees (id, name, salary) 
VALUES (1, 'John Doe', 55000);

*(Notice how the order of the VALUES exactly matches the order of the columns defined in the first set of parentheses!)*

5. Inserting into Specific Columns

You do not have to provide data for every single column! If an employee hasn't negotiated their salary yet, you can leave it blank (it will become NULL, which we will cover in Chapter 11).
sql
123
-- Only inserting the ID and the Name. The salary is skipped!
INSERT INTO employees (id, name) 
VALUES (2, 'Jane Smith');

6. Bulk Inserts (Inserting Multiple Rows)

If you have a spreadsheet of 50 new hires, writing INSERT INTO 50 separate times is incredibly inefficient and slows down the database. You can insert multiple rows in a single command by separating the VALUES blocks with a comma!
sql
123456
-- Bulk Insert 3 employees at once!
INSERT INTO employees (id, name, salary) 
VALUES 
    (3, 'Alice Johnson', 60000),
    (4, 'Bob Builder', 45000),
    (5, 'Charlie Brown', 50000);

*(This is how web applications upload massive CSV files into a database in milliseconds!)*

7. Mini Project: Product Inventory System

Let's populate an E-commerce inventory table. *(Assume the table products has columns: productid, productname, price, and in_stock).*
sql
123456
-- Add our initial inventory
INSERT INTO products (product_id, product_name, price, in_stock)
VALUES 
    (101, 'Mechanical Keyboard', 129.99, TRUE),
    (102, 'Wireless Mouse', 49.50, TRUE),
    (103, 'Gaming Monitor', 349.00, FALSE);

8. Common Mistakes

  • Mismatched Column/Value Count: If you list 3 columns in the first parentheses, but only provide 2 values in the second parentheses, SQL will instantly crash with a "Column count doesn't match value count" error.
  • Forgetting Single Quotes for Text: INSERT INTO users (name) VALUES (John) will fail. SQL thinks John is a system command because it lacks quotes. It MUST be 'John'.
  • Double Quotes vs Single Quotes: Standard SQL requires strings to be wrapped in Single Quotes ''. Double quotes "" are technically used for system identifiers in strict SQL engines like PostgreSQL. Always use single quotes for data!

9. Best Practices

  • Always Explicitly Name Columns: Technically, if you insert data into *every* column, SQL lets you skip writing the column names: INSERT INTO employees VALUES (1, 'John', 50000). Never do this. If another developer adds a "department" column to the table tomorrow, your query will instantly break because the implicit order changed!

10. Exercises

  1. 1. What SQL keyword precedes the data you are trying to insert into the table?
  1. 2. Which of the following values MUST be wrapped in single quotes during an INSERT: 100, 'New York', or 45.99?

11. SQL Challenges

Write a single SQL command to insert two rows of data into the books table. The columns are bookid, title, and publishedyear. Row 1: ID 1, Title '1984', Year 1949. Row 2: ID 2, Title 'Dune', Year 1965.
sql
1234
INSERT INTO books (book_id, title, published_year)
VALUES 
    (1, '1984', 1949),
    (2, 'Dune', 1965);

12. MCQ Quiz with Answers

Question 1

In an INSERT INTO statement, what happens if you list 4 columns in the first set of parentheses, but only provide 3 values in the VALUES parentheses?

Question 2

When writing raw SQL, what is the strict syntax rule for inserting text/string data (like a user's name) versus inserting integer data (like a user's age)?

13. Interview Questions

  • Q: Explain why relying on "Implicit Inserts" (omitting the column names in the INSERT INTO statement) is considered a dangerous architectural anti-pattern in production codebases.
  • Q: A backend Node.js script needs to insert 1,000 new users into the database. Should the script execute 1,000 separate INSERT commands, or a single bulk INSERT command? Explain the performance implications.

14. FAQs

Q: What if a user's name has an apostrophe in it, like "O'Connor"? A: Since single quotes define a string, 'O'Connor' will break the SQL syntax (it thinks the string ends after the O). To fix this, you must "escape" the quote by using two single quotes! 'O''Connor'. *(Note: Modern web backends like PHP/Node handle this automatically using "Prepared Statements", which we cover in Chapter 26!)*

15. Summary

The database is no longer empty. By mastering the INSERT INTO command, mapping data to the correct columns, and utilizing bulk inserts for massive data uploads, you possess the ability to actively populate a relational database schema.

16. Next Chapter Recommendation

Data is locked in the vault. Now, we must retrieve it. In Chapter 7: SELECT Queries and Retrieving Data, we will learn the most frequently used command in all of SQL: The mighty SELECT statement.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·