Menu

SQL INSERT INTO – A Comprehensive Guide to master SQL INSERT INTO Statement

Written by Jagdeesh | 3 min read

In the realm of SQL, learning how to insert data into tables is a fundamental skill for anyone aspiring to manipulate databases effectively.

The SQL INSERT INTO statement is the command you need for this task, allowing you to add new rows of data into a table. Let’s delve deeper into this key statement with a plethora of examples and illustrative code blocks.

What is INSERT INTO Statement?

The SQL INSERT INTO statement facilitates inserting new records into a database. You can add either a single row containing values for each column or multiple rows at once. Here’s the basic syntax

sql
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

In the syntax above, table_name is where you want to insert the data. (column1, column2, column3, …) specifies the columns for the data, and VALUES (value1, value2, value3, …) represents the data values to be inserted.

It’s also possible to use INSERT INTO without specifying column names. However, you should ensure the values are in the same order as the columns in the table. The syntax for this approach is

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

Examples to Deepen Your Understanding

1) Inserting Single Row

Let’s assume we have a table named Employees, which includes the following columns: EmployeeID, FirstName, LastName, Email, and PhoneNumber.

sql
CREATE TABLE Employees (
    EmployeeID INT,
    FirstName VARCHAR(100),
    LastName VARCHAR(100),
    Email VARCHAR(100),
    PhoneNumber VARCHAR(15)
);

To insert data into this table, we can run the following SQL command

sql
INSERT INTO Employees (EmployeeID, FirstName, LastName, Email, PhoneNumber)
VALUES (1, 'John', 'Doe', '[email protected]', '123-456-7890');

The table would now look like this

output
| EmployeeID | FirstName | LastName | Email                 | PhoneNumber  |
|------------|-----------|----------|-----------------------|--------------|
| 1          | John      | Doe      | [email protected]  | 123-456-7890 |

2) Inserting Multiple Rows

We can also insert multiple rows at once

sql
INSERT INTO Employees (EmployeeID, FirstName, LastName, Email, PhoneNumber)
VALUES 
    (2, 'Jane', 'Doe', '[email protected]', '098-765-4321'),
    (3, 'Jim', 'Beam', '[email protected]', '567-890-1234');

After running this statement, our Employees table would look like

output
| EmployeeID | FirstName | LastName | Email                 | PhoneNumber  |
|------------|-----------|----------|-----------------------|--------------|
| 1          | John      | Doe      | [email protected]  | 123-456-7890 |
| 2          | Jane      | Doe      | [email protected]  | 098-765-4321 |
| 3          | Jim       | Beam     | [email protected]  | 567-890-1234 |

3) Inserting Data without Specifying Columns

Consider a table Students with three columns: StudentID, FirstName, and LastName. To insert data into the Students table without specifying the column names, we ensure the order of values matches the order of the columns.

sql
INSERT INTO Students
VALUES (2, 'Jane', 'Smith');

The Students table now looks like

output
| StudentID | FirstName | LastName |
|-----------|-----------|----------|
| 1         | John      | Doe      |
| 2         | Jane      | Smith    |

4) Inserting Data into Specific Columns

If we only have data for some columns, we can still use INSERT INTO by specifying the known columns

sql
INSERT INTO Students (StudentID, FirstName)
VALUES (3, 'Emma');

This command inserts a new record with StudentID 3 and FirstName ‘Emma’. The LastName field will remain NULL for this record (assuming the table schema allows NULLs). The table now looks like

output
| StudentID | FirstName | LastName |
|-----------|-----------|----------|
| 1         | John      | Doe      |
| 2         | Jane      | Smith    |
| 3         | Emma      | NULL     |

5) Inserting Data from Another Table

The INSERT INTO statement can also insert data into a table selected from another table. Let’s say we want to copy all students from Students to GraduatedStudents

sql
INSERT INTO GraduatedStudents (StudentID, FirstName, LastName)
SELECT StudentID, FirstName, LastName FROM Students;

This command will copy all records from Students to GraduatedStudents.

Conclusion

Mastering the SQL INSERT INTO statement is crucial for effective database manipulation. By providing the means to add new records to our database tables, this statement is undoubtedly a vital command in SQL. Continue practicing, and soon enough, you’ll be confidently managing your databases like a pro.

Free Course
Master Core Python — Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Jagdeesh
Written by
Related Course
Master SQL — Hands-On
Join 5,000+ students at edu.machinelearningplus.com
Explore Course
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science