×

SQL INSERT INTO Statement

SQL INSERT INTO statement adds data to the newly created tables or existing tables. We can add single records or multiple records in a table by using this query.

There are two possible ways to add data to a table:

  • Mention the field's name and the values are added to the table.
  • If we add values for all the table fields, we do not need to mention the field's name in the SQL INSERT INTO statement or query. But, make sure the order of the values is the same as the order of the columns in the table.

Syntax for SQL INSERT INTO statement by mentioning the fields names in the statement or query:  

INSERT INTO Table_Name (Column_Name1, Column_Name2, Column_Name3, Column_Name4, Column_Name5) VALUES (Value1, Value2, Value3, Value4, Value5);

Column_Name1, Column_Name2, Column_Name3, Column_Name4, Column_Name5 are the fields name in the tables into which we want to add values.

The syntax for SQL INSERT INTO statement without mentioning the names of the fields in the statement or query:  

INSERT INTO Table_Name VALUES (Value1, Value2, Value3, Value4, Value5);

The above syntax is used to insert values in all the fields of the tables.

The following statements would create eight records in the Customer table.

INSERT INTO Customer (Customer_Id, Customer_Name, Age, Address, Salary)

VALUES (1, 'Rakesh', 32, 'Ahmedabad', 20000);

INSERT INTO Customer (Customer_Id, Customer_Name, Age, Address, Salary)

VALUES (2, 'Kamlesh', 27, 'Delhi', 15000);

INSERT INTO Customer (Customer_Id, Customer_Name, Age, Address, Salary)

VALUES (3, 'kaustubh', 25, 'Pune', 20000);

INSERT INTO Customer (Customer_Id, Customer_Name, Age, Address, Salary)

VALUES (4, 'Chaitali', 25, 'Mumbai', 15000);

INSERT INTO Customer (Customer_Id, Customer_Name, Age, Address, Salary)

VALUES (5, 'Himesh', 29, 'Delhi', 45000);

INSERT INTO Customer (Customer_Id, Customer_Name, Age, Address, Salary)

VALUES (6, 'Komal', 22, 'MP', 45000);

INSERT INTO Customer (Customer_Id, Customer_Name, Age, Address, Salary)

VALUES (7, 'Nikhlesh', 28, 'Delhi', 40000);

INSERT INTO Customer (Customer_Id, Customer_Name, Age, Address, Salary)

VALUES (8, 'Kamolika', 24, 'Pune', 50000);

In the above INSERT INTO statement, we have added the records in the Customer table by mentioning the table's field names.

We can add the data to the table without mentioning the field's name of the table:

The following example statements would create six records in the Customer table.

INSERT INTO Customer VALUES (9, ‘Raman’, 30, ‘Mumbai’, 35500);

INSERT INTO Customer VALUES (10, ‘Manoj’, 40, ‘Pune’, 45000);

INSERT INTO Customer VALUES (11, ‘Shweta’, 26, ‘MP’, 42500);

INSERT INTO Customer VALUES (12, ‘Shivani’, 25, ‘Delhi’, 50000);

INSERT INTO Customer VALUES (13, ‘Rahul’, 28, ‘Nashik’, 34000);

INSERT INTO Customer VALUES (14, ‘Sahil’, 22, ‘Nashik’, 27000);

In the above INSERT INTO statement example, we have added the records without mentioning the fields name in the query.

All the above queries would generate the following data in the Customer table as shown below:

Customer_IdCustomer_NameAgeAddressSalary
1Rakesh32Ahmedabad20000
2Kamlesh27Delhi15000
3Kausubh25Pune20000
4Chaitali25Mumbai15000
5Himesh29Delhi45000
6Komal22MP45000
7Nikhlesh28Delhi40000
8Kamolika24Pune50000
9Raman30Mumbai35500
10Manoj40Pune45000
11Shweta26MP42500
12Shivani25Delhi50000
13Rahul28Nashik34000
14Sahil22Nashik27000
SQL INSERT INTO Statement

INSERT INTO SELECT Statement.

INSERT INTO SELECT statement is also a way to add records to the table. INSERT INTO SELECT statement is used to insert records into one table from the existing table. Where all the fields and order of the fields are the same.

We will create another Customer and use the same fields from the above Customer table.

CREATE TABLE Customers (Customer_Id int Primary key, Customer_Name varchar(40), Age int, Address Varchar(20), Salary int);

Use the DESC command followed by the Customers table name to show the table structure.

FieldsTypeNullKeyDefaultExtra
Customer_IdInt(11)NOPRINULL 
Customer_NameVarchar(40)YES NULL 
AgeInt(11)YES NULL 
AddressVarchar(20)YES NULL 
SalaryInt(11)YES NULL 

We can add records into a table through the SELECT statement over another table.

Syntax of the INSERT INTO SELECT:

INSERT INTO Table1 [(Column_Name1, Column_Name2, Column_Name3, Column_Name4, Column_Name5)] SELECT Column_Name1, Column_Name2, Column_Name3, Column_Name4, Column_Name5 FROM Table2;

Example of the INSERT INTO SELECT statement:

INSERT INTO Customers SELECT * FROM Customer;

In the above query INSERT INTO SELECT example, all the records from the Customer table are added to the Customers table.

The following data in the Customer table is shown below:

Customer_IdCustomer_NameAgeAddressSalary
1Rakesh32Ahmedabad20000
2Kamlesh27Delhi15000
3Kausubh25Pune20000
4Chaitali25Mumbai15000
5Himesh29Delhi45000
6Komal22MP45000
7Nikhlesh28Delhi40000
8Kamolika24Pune50000
9Raman30Mumbai35500
10Manoj40Pune45000
11Shweta26MP42500
12Shivani25Delhi50000
13Rahul28Nashik34000
14Sahil22Nashik27000
SQL INSERT INTO Statement

Related Topics

How to use LIKE in SQL

In this SQL article, we will learn and understand how to use LIKE to the columns in the SQL tables. What is Like? Like is an operator in the SQL. It is...

7 minutes read.

SQL SELECT Statement

The SQL SELECT statement is used to retrieve the data from the tables. We can also retrieve the selected records from the table using the query condition. The SQL SELECT...

5 minutes read.

How to use COUNT in SQL?

How to use COUNT in SQL Introduction COUNT( ) is an aggregate function in SQL.This function counts the number of records in a table if the condition is not specified.If the condition...

4 minutes read.

SQL Auto Increment

As we all know, In SQL for unique identification, we assign a column with the primary key. But in some tables, we find difficult to differentiate a column as a...

5 minutes read.

SQL CASE

This page contains all the information about SQL CASE. The CASE is an If-Else type of logical query used in the statement. The CASE in Structured Query Language is similar...

5 minutes read.

SQL Cloning Tables

Cloning a Table: To create a copy of the table. To perform the operations, without affecting the actual table. Steps for creating a Cloning Table: Step 1: Empty Table Creation The syntax for Creating...

3 minutes read.

SQL DELETE

In this tutorial, you will learn about the SQL DELETE concept by using examples. In Structured Query Language (SQL), we fetch the data from the database table using SELECT Statement and...

3 minutes read.

SQL INSERT Statement

In this tutorial, we will help you to understand and learn how to insert records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.

SET Operators in SQL

The operator used to join or combine two queries is none other than SET operators. Operators categorized into SET operators are as follows: UNION Operator.UNION ALL’ Operator.INTERSECT Operator.MINUS Operator. Rules to be...

8 minutes read.

Trigger in SQL

In this article, we will learn about the concept of trigger in SQL and its implementation with the help of an example. A Trigger in Structured Query Language is a set...

4 minutes read.

SQL HAVING Clause

In this tutorial, we will understand the HAVING clause concept in SQL with the help of examples. The HAVING Clause in the SQL is used as we cannot use the WHERE...

3 minutes read.

Codd’s Rules in SQL

Codd’s Rules Dr. Edgar F. Codd, in 1985, laid down 13 fundamental rules after doing large-scale research on the Relational Model of databases. According to him, every database must follow these...

3 minutes read.

Difference between SQL and NoSQL

SQL vs. NoSQL | Difference between SQL and NoSQL Choosing a database is the most fundamental decision that needs to be decided before starting a task. Relational and non-relational databases are...

3 minutes read.

WEB SQL

What is Web SQL? In SQL we can create databases, read the data in the databases, insert the records into the databases, and delete the records from the databases. For storing...

4 minutes read.

SQL SELECT OR Operator

This SQL tutorial explains and helps us understand how to use the OR operator in the SELECT query with examples. The OR operator in the Structured Query Language is used to...

3 minutes read.

SQL WHERE Clause

The WHERE clause is a search filter that returns only true records. The WHERE clause chooses the selected records based on the given conditions. The WHERE clause is used with...

4 minutes read.

SQL Left Join

The SQL Left Join query displays all the records from the table and displays similar records from the right table. The query displays zero records if it doesn’t find any...

4 minutes read.

Check Constraint in SQL

The Check Constraint in SQL is the rule or set of rules used to limit the data range that can be entered in a table column. Check constraint is used...

8 minutes read.

Where Condition in SQL

WHERE clause is used with the Data Manipulation Language Command in SQL, the Data Manipulation Language commands are the SELECT statement, UPDATE statement, and DELETE statement. WHERE clause condition is an...

5 minutes read.

SQL FOREIGN KEY

In this article, we will learn about the FOREIGN KEY constraints and how to define a FOREIGN KEY constraint to build the relationship between two tables. In a Relational Databases Management...

4 minutes read.