×

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 the BETWEEN operator in SQL

In this entire SQL article, we will understand and learn about the BETWEEN operator concept and how to use it in SQL. What is the BETWEEN operator in SQL? The Between operator...

4 minutes read.

SQL Arithmetic Operators

This page contains all the information, learn about SQL Arithmetic Operators concept in the SQL table with the help of examples. The Arithmetic Operators is used to perform mathematical calculations on...

5 minutes read.

SQL UPDATE

SQL UPDATE The SQL UPDATE statement is utilized to update and modify the records present into a database. It is used to change the already existing records stored in the tables...

3 minutes read.

SQL Insert multiple rows

This article will deal with a new insertion method in SQL which inserts multiple rows at a time. Multiple records can be inserted at a time into a specific table with...

4 minutes read.

Types of SQL Commands

The Structured Query Language is used to deal with structured data. The data which are stored in the form of tables are structured data. These SQL commands store records or...

10 minutes read.

SQL CREATE TABLE

In SQL tutorial, we learned and created different databases. To stores data in databases, we need to create a table. To create the table, we need to use CREATE TABLE...

5 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.

SQL Aggregate Operators

In SQL, the aggregate operators perform a calculation on multiple rows or multiple values of a single column and give the output a single value. Here, multiple rows of data are...

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 Topological Sorting

It is for Directed Acyclic Graph, which linearly describes the ordering of graphs. It is not possible for all graphs, it is possible for only Directed acyclic graphs. Applications of Topological...

3 minutes read.

SQL Truncate

This command deletes all records from table. Truncate is a DDL command. Syntax: TRUNCATE table table_name; Example: Truncate table teacher; ORDER BY The ORDER BY clause arranges the table or column in ascending...

5 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.

How to delete column in table

Introduction In SQL, sometimes it is required to delete a column of a table.The use of ALTER TABLE command with DROP COLUMN clause will serve the purpose to delete/remove a column...

4 minutes read.

DML Commands in SQL

DML is an abbreviation of Data Manipulation Language. Data Manipulation Language commands in Structured Query Language manipulate the data in the database. DML commands are used to retrieve records, add records,...

4 minutes read.

SQL SET Operator

In this tutorial, we will understand the operator who falls under the SET operator in SQL with the help of different examples. The SQL SET Operator is used to merge the...

5 minutes read.

SQL Between Operator

SQL Between operator is a logical operator in the Structured Query Language. The Between operator is used to retrieve data within the range specified in the condition in the query. The...

7 minutes read.

How to Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

2 minutes read.

SQL JOIN

As the name says, joins mean to merge something or to combine something. But in the case of SQL, joins mean to merge or combine two different tables. The Join clause...

12 minutes read.

SQL Comparision Operator

The Comparison Operator compares different data of the Structured Query Language table and checks whether the data are the same, less than, greater than, less than, or greater than equal....

14 minutes read.

space() function in SQL

 This function returns a string with a specified number of spaces. If we specify a number, it returns the strings having that specified number of spaces. SPACE Function is available...

2 minutes read.