Lesson 14 of 60 – INSERT INTO
23%

INSERT INTO in SQL

The INSERT INTO statement is used to add new records (rows) to a table in a database.

Note: Before inserting data, make sure that the table already exists and that the values match the column data types.

1. What is INSERT INTO?

The INSERT INTO statement adds new data to an existing table.

INSERT INTO Students
VALUES (1, 'Rahul', 20);

This statement inserts one student record into the Students table.

2. Basic INSERT Syntax

The basic syntax is:

INSERT INTO table_name
VALUES (value1, value2, value3);

The order of values must match the order of columns in the table.

3. Example Table

CREATE TABLE Students (
    id INT,
    name VARCHAR(100),
    age INT
);

Now we can insert records into this table.

4. Insert One Record

INSERT INTO Students
VALUES (1, 'Rahul', 20);

This inserts one row into the Students table.

ID Name Age
1 Rahul 20

5. Insert Multiple Records

Multiple records can be inserted using a single INSERT statement.

INSERT INTO Students
VALUES
(1, 'Rahul', 20),
(2, 'Priya', 21),
(3, 'Amit', 19);

This statement inserts three records.

6. Specifying Column Names

It is generally better to specify the column names when inserting data.

INSERT INTO Students (id, name, age)
VALUES (1, 'Rahul', 20);

This makes the query easier to understand and maintain.

7. Insert Data into Selected Columns

You can insert values into only selected columns.

INSERT INTO Students (id, name)
VALUES (2, 'Priya');

The age column is not specified in this query. Its value depends on the column definition, such as a default value or whether NULL is allowed.

8. Insert Text Values

Text values are normally written inside single quotes.

INSERT INTO Students (id, name, age)
VALUES (3, 'Amit', 22);

Here, Amit is a text value.

9. Insert Numeric Values

Numeric values such as integers do not normally require quotes.

INSERT INTO Students (id, name, age)
VALUES (4, 'Neha', 23);

Here, 4 and 23 are numeric values.

10. Insert Date Values

Date values can be inserted into a DATE column.

CREATE TABLE Students (
    id INT,
    name VARCHAR(100),
    admission_date DATE
);

Insert a date:

INSERT INTO Students
(id, name, admission_date)
VALUES
(1, 'Rahul', '2026-09-20');

11. Insert Decimal Values

Decimal values can be inserted into columns such as DECIMAL.

CREATE TABLE Courses (
    id INT,
    course_name VARCHAR(100),
    fee DECIMAL(10,2)
);
INSERT INTO Courses
(id, course_name, fee)
VALUES
(1, 'Python Full Stack', 15000.00);

12. Insert NULL Values

If a column allows NULL values, you can explicitly insert NULL.

INSERT INTO Students
(id, name, age)
VALUES
(5, 'Ravi', NULL);

NULL means that the value is missing or unknown. It is not the same as zero or an empty string.

13. INSERT with AUTO_INCREMENT

If a column uses AUTO_INCREMENT, you normally do not need to provide its value.

CREATE TABLE Students (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100),
    age INT
);

Insert a student:

INSERT INTO Students (name, age)
VALUES ('Rahul', 20);

The database can automatically generate the ID.

14. INSERT with DEFAULT Values

If a column has a default value, that value can be used when the column is omitted.

CREATE TABLE Students (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100),
    city VARCHAR(50) DEFAULT 'Aurangabad'
);
INSERT INTO Students (name)
VALUES ('Amit');

The database can use Aurangabad as the default city.

15. Insert Multiple Rows with Column Names

INSERT INTO Students (name, age)
VALUES
('Rahul', 20),
('Priya', 21),
('Amit', 19),
('Neha', 22);

This is a convenient way to insert several records at once.

16. Insert Data into an Employee Table

CREATE TABLE Employees (
    employee_id INT,
    employee_name VARCHAR(100),
    department VARCHAR(50),
    salary DECIMAL(10,2)
);

Insert a record:

INSERT INTO Employees
(employee_id, employee_name, department, salary)
VALUES
(1, 'Amit Kumar', 'IT', 35000.00);

17. Insert Data into a Course Table

CREATE TABLE Courses (
    course_id INT,
    course_name VARCHAR(100),
    duration VARCHAR(50),
    fee DECIMAL(10,2)
);
INSERT INTO Courses
(course_id, course_name, duration, fee)
VALUES
(1, 'ADCA', '6 Months', 10000.00);

18. Insert Student Course Data

INSERT INTO Courses
(course_id, course_name, duration, fee)
VALUES
(2, 'Python Full Stack', '1 Year', 35000.00),
(3, 'Tally Prime', '3 Months', 4000.00),
(4, 'Web Development', '6 Months', 15000.00);

19. Check Inserted Data

After inserting records, you can use the SELECT statement to check the data.

SELECT * FROM Students;

The * means that all columns should be displayed.

20. Insert Data in a Specific Column Order

The order of values should match the order of the specified columns.

INSERT INTO Students
(name, age, id)
VALUES
('Rahul', 20, 10);

This is valid because the values correspond to name, age, id in that order.

21. Common Error: Wrong Number of Values

The number of supplied values should match the number of columns being inserted into.

Incorrect:

INSERT INTO Students (id, name, age)
VALUES (1, 'Rahul');

There are three specified columns but only two values.

Correct:

INSERT INTO Students (id, name, age)
VALUES (1, 'Rahul', 20);

22. Common Error: Wrong Data Type

Values should be compatible with the column data types.

For example:

CREATE TABLE Students (
    id INT,
    name VARCHAR(100)
);

A numeric ID should normally be inserted as a numeric value:

INSERT INTO Students (id, name)
VALUES (1, 'Rahul');

23. Common Error: Missing Quotes

Text values should normally be enclosed in quotes.

Incorrect:

INSERT INTO Students (name)
VALUES (Rahul);

Correct:

INSERT INTO Students (name)
VALUES ('Rahul');

24. INSERT INTO vs CREATE TABLE

Statement Purpose
CREATE TABLE Creates a new table
INSERT INTO Adds records to a table
SELECT Retrieves records

25. Practical Student Example

CREATE TABLE Students (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    mobile VARCHAR(15),
    course VARCHAR(100),
    fee DECIMAL(10,2)
);

Now insert student records:

INSERT INTO Students
(name, mobile, course, fee)
VALUES
('Rahul Kumar', '9876543210', 'Python', 15000.00),
('Priya Singh', '9876543211', 'ADCA', 10000.00),
('Amit Kumar', '9876543212', 'Tally Prime', 4000.00);

26. INSERT INTO with SELECT

SQL also supports inserting records selected from another table.

INSERT INTO NewStudents (id, name)
SELECT id, name
FROM Students;

This technique is useful when copying selected data from one table into another table.

27. Important INSERT Rules

  • Use INSERT INTO to add records.
  • Specify column names whenever practical.
  • Text values are normally written inside quotes.
  • Numeric values normally do not require quotes.
  • Make sure values match the column data types.
  • The number of values must match the specified columns.
  • Use NULL only when the column allows it.
  • AUTO_INCREMENT columns can generate IDs automatically.

📌 Key Points

  • INSERT INTO is used to add new records to a table.
  • You can insert one or multiple records.
  • Column names can be specified in an INSERT statement.
  • Text values are normally written inside single quotes.
  • Numeric values are normally written without quotes.
  • NULL represents a missing or unknown value.
  • AUTO_INCREMENT can automatically generate numeric IDs in MySQL.
  • DEFAULT values can be used when a column is omitted.
  • Always make sure the number and order of values match the specified columns.

🧠 Quick Quiz

Question: Which SQL statement is used to add a new record to an existing table?