×

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

TCL Commands in SQL

In Structured Query Language, TCL is an abbreviation for Transaction Control Language. A single unit of work in a database is formed after the consecutive execution of commands is known...

6 minutes read.

Pattern Matching in SQL

The LIKE in the Structured Query Language is a Logical Operator. The SQL LIKE is used within the WHERE clause in the SQL. This Like Operator is used with the...

5 minutes read.

SQL Cast Operator

While writing SQL queries, we can see many values which are to be converted into another datatype for accessing them. In SQL this conversion is generally done by CAST function. CAST...

4 minutes read.

Introduction to SQL

SQL Introduction SQL is Standard Query Language. This language is used to communicate or interact with database. In other words, SQL is used to access and manage data or information...

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

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.

How to create a database in SQL?

How to create a database in SQL It is very necessary to create a database in order to store the data into the database.Database name must always be unique.SQL does not...

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

Commit and Rollback in SQL

In Structured Query Language, we have Data Definition Language (DDL) and Data Manipulation Language (DML) commands the same way we have Transaction Control Language (TCL) commands in the Structured Query...

4 minutes read.

SQL Top Clause

Top Clause: It returns the first ‘n’ number of rows as output. This Top clause is used to retrieve the required number of rows when there are thousands of records stored...

3 minutes read.

How to use HAVING clause in SQL

In this article, we will learn about the HAVING clause concept and how to use it in SQL. What is the HAVING clause? In Structured Query Language, HAVING Clause used with GROUP...

7 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 INSERT Table

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

5 minutes read.

SQL Temporary Tables

Temporary Table: The Temporary tables are generally created in TempDB. If the last connection is terminated then the tables are deleted automatically. These are generally used to store and process the...

4 minutes read.

Save Point in SQL

In SQL, the classification is done into 4 languages. They are Data Definition Language (DDL)Data Manipulation Language (DML)Transaction Control Language (TCL)Data Control Language (DCL) Save Point falls under the Transaction Control Language....

4 minutes read.

SQL Data Types

In SQL DATA TYPES, each column holds value. Data types can be an integer type, character type, etc., and such data is stored in the database table. The data type is...

4 minutes read.

SQL Wildcards

SQL WILDCARD CHARACTERS: SQL wild card character is used to substitute zero or more characters of a string. These characters are used with the “LIKE” operator. Wild Card Characters in SQL: %_[]^- “%” : It is...

3 minutes read.

SQL Formatter

As a beginner, one does not focus much on Formatting the queries while writing their code. This leads to a great confusion while referring the code in future. For a...

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

SQL Operators

Arithmetic Operators Arithmetic operators are +, -, *, /, % performs addition, subtraction, multiplication, division, modulo respectively. Example:   Select 100+222; Select salary+100 From teacher Where teacher_id=1; Following output screen shows different arithmetic operations. We...

2 minutes read.