×

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 add records to the table. We can add single or multiple data to a table using the INSERT query.

We can add data to the tables by mentioning the column name in the syntax or without mentioning the column name in the syntax.

We can add records to the new table and copy the existing table data into the newly created table by using the insert into select query.

There are two ways we can add data to the table as follows:

1. SQL INSERT INTO query.

2. SQL INSERT INTO SELECT query.

Let's understand how to use the INSERT query with the help of examples.

1. SQL INSERT INTO query

SQL INSERT INTO query is used to add data to a table.

There are two ways to add data to the table by using the SQL INSERT INTO query as follows:

1. In this type, we need not mention the column's name in the syntax. We just need to mention the data to be added to the table.

2. In this type, we need to mention the specific column name in the syntax; mentioning the columns allows us to add records to the column name with their values.

1. SQL INSERT INTO query without mentioning the columns name:

The syntax of the SQL INSERT INTO query without mentioning columns name is as follows:

INSERT INTO Table_Name VALUES (Value_1, Value_2, Value_3, Value_4, Value_5, Value_6, Value_7);

We can add multiple data simultaneously into the table in the same method.

Let's take an example of this method.

Firstly, we will create a new table to add records to the table.

CREATE TABLE Player(Player_No int not null, Player_Name Varchar(100) not null, ODI_Runs int, T20_Runs int, Test_Runs int, IPL_Runs int, Best_ODI_Ranking int, Best_T20_Ranking int, Best_Test_Ranking int, Team varchar(100));

In the above query, we created the Player name table.

We will run the DESC query followed by the table name to check whether the Player table is successfully created or not:

DESC Player;

The output of the above query is as follows:

FieldType Null KeyDefaultExtra
Player_Noint(11)NO NULL 
Player_Namevarchar(100)NO NULL 
ODI_Runsint(11)YES NULL 
T20_Runsint(11)YES NULL 
Test_Runsint(11)YES NULL 
IPL_Runsint(11)YES NULL 
Best_ODI_Rankingint(11)YES NULL 
Best_T20_Rankingint(11)YES NULL 
Best_Test_Rankingint(11)YES NULL 
Teamvarchar(100)YES NULL 

Now, we will add records into the table is as follows:

INSERT INTO Player VALUES (10, ‘Sachin Tendulkar’, 18426, 10, 15921, 2334, 1, 63, 1, ‘India’);
INSERT INTO Player VALUES (19, 'Rahul Dravid', 10889, 31, 13288, 2174, 5, 0, 1, 'India');
INSERT INTO Player VALUES (14, 'Ricky Ponting', 13704, 401, 13378, 91, 1, 1, 1, 'Australia');

We added three records to the Player table. We will run the select query to check whether the data added successfully or not is as follows:

SELECT * FROM Player;

The output of the above query is as follows:

Player_NoPlayer_NameODI_RunsT20_RunsTest_RunsIPL_RunsBest_ODI_RankingBest_T20_RankingBest_Test_RankingTeam
10Sachin Tendulkar18426101592123341631India
19Rahul Dravid1088931132882174501India
14Ricky Ponting137044011337891111Australia
SQL INSERT Statement

2. SQL INSERT INTO query with mentioning the columns name: 

The syntax of the SQL INSERT INTO query with mentioning columns name is as follows:

INSERT INTO Table_Name (Column_Name1, Column_Name2, Column_Name3, Column_Name4, Column_Name5, Column_Name6, Column_Name7) VALUES (Value_1, Value_2, Value_3, Value_4, Value_5, Value_6, Value_7);

Let’s take an example on this method.

INSERT INTO Player (Player_No, Player_Name, ODI_Runs, T20_Runs, Test_Runs, IPL_Runs, Best_ODI_Ranking, Best_T20_Ranking, Best_Test_Ranking, Team) VALUES (18, ‘Virat Kohli’, 12327, 3308, 8074, 6624, 1, 1, 1, ‘India’);
INSERT INTO Player (Player_No, Player_Name, ODI_Runs, T20_Runs, Test_Runs, IPL_Runs, Best_ODI_Ranking, Best_T20_Ranking, Best_Test_Ranking, Team) VALUES (45, ‘Rohit Sharma’, 9359, 3379, 3137, 5879, 2, 7, 5, ‘India’);
 INSERT INTO Player (Player_No, Player_Name, ODI_Runs, T20_Runs, Test_Runs, IPL_Runs, Best_ODI_Ranking, Best_T20_Ranking, Best_Test_Ranking, Team) VALUES (66, ‘Joe Root’, 6120, 893, 10458, 0, 2, 4, 1, ‘England’);
INSERT INTO Player (Player_No, Player_Name, ODI_Runs, T20_Runs, Test_Runs, IPL_Runs, Best_ODI_Ranking, Best_T20_Ranking, Best_Test_Ranking, Team) VALUES (22, ‘Kane Williamson’, 6174, 2021, 7368, 2101, 3, 3, 1, ‘New Zealand’);

We added Four records to the Player table. We will run the select query to check whether the data added successfully or not is as follows:

SELECT * FROM Player;

The output of the above query is as follows:

Player_NoPlayer_NameODI_RunsT20_RunsTest_RunsIPL_RunsBest_ODI_RankingBest_T20_RankingBest_Test_RankingTeam
10Sachin Tendulkar18426101592123341631India
19Rahul Dravid1088931132882174501India
14Ricky Ponting137044011337891111Australia
18Virat Kohli12327330880746624111India
45Rohit Sharma9359337931375879275India
66Joe Root6120893104580241England
22Kane Williamson6174202173682101331New Zealand
SQL INSERT Statement

2. SQL INSERT INTO SELECT query

The SQL INSERT INTO SELECT query is used to add or copy data from an existing table into a newly created table. But there should be an equal number of columns and data types between the tables.

The syntax of the SQL INSERT INTO SELECT query is as follows:

INSERT INTO Table_Name_2 (SELECT * FROM Table_Name_1);

To insert into a new table from an old table, we will create a players table as shown below:

CREATE TABLE Players(Player_No int not null, Player_Name Varchar(100) not null, ODI_Runs int, T20_Runs int, Test_Runs int, IPL_Runs int, Best_ODI_Ranking int, Best_T20_Ranking int, Best_Test_Ranking int, Team varchar(100));

We will run the DESC query followed by the table name to check whether the Player table is successfully created or not:

DESC Players;

The output of the above query is as follows:

FieldType Null KeyDefaultExtra
Player_Noint(11)NO NULL 
Player_Namevarchar(100)NO NULL 
ODI_Runsint(11)YES NULL 
T20_Runsint(11)YES NULL 
Test_Runsint(11)YES NULL 
IPL_Runsint(11)YES NULL 
Best_ODI_Rankingint(11)YES NULL 
Best_T20_Rankingint(11)YES NULL 
Best_Test_Rankingint(11)YES NULL 
Teamvarchar(100)YES NULL 

We will not insert into the players table using the INSERT INTO SELECT query as shown below:

INSERT INTO Players (SELECT * FROM Player);

We will run the select query to check whether the data added successfully or not is as follows:

SELECT * FROM Players;

The output of the above query is as follows:

Player_NoPlayer_NameODI_RunsT20_RunsTest_RunsIPL_RunsBest_ODI_RankingBest_T20_RankingBest_Test_RankingTeam
10Sachin Tendulkar18426101592123341631India
19Rahul Dravid1088931132882174501India
14Ricky Ponting137044011337891111Australia
18Virat Kohli12327330880746624111India
45Rohit Sharma9359337931375879275India
66Joe Root6120893104580241England
22Kane Williamson6174202173682101331New Zealand

Related Topics

SQL VIEW

The SQL VIEW concept helps to hide the difficulty of the records and provides limitations to access to the database. The SQL view is similar to the SQL tables. In SQL...

7 minutes read.

How to Update Table in SQL?

How to Update Table in SQL Introduction UPDATE query is used to update a record in a table. UPDATE is a DML command, which operates on the data of the table and not...

4 minutes read.

How to add column in table in SQL ?

How to add column in table in SQL  Introduction To add a column in already created table, one needs to use the ALTER command along with the ADD clause.If in the query,...

7 minutes read.

SQL CONSTRAINTS

SQL Constraints specifies the rules/limitations/restrictions for data present in table. SQL Constraints are specified at the time of table creation or after table creation using ALTER command. There are two...

5 minutes read.

SQL UNION ALL Operator

This page has all the information about UNION ALL operator, which is used to join all the values from the SELECT queries. Similar records were not considered in the result in...

3 minutes read.

Drop vs Truncate in SQL

In this article, we will learn and understand the Drop and Truncate commands and the difference between these two commands. What is Drop Command? Drop is a Data Definition Language command in...

6 minutes read.

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.

SQL NOT Operator

SQL NOT is a Boolean operator used with the WHERE clause. NOT operator shows the records if the expression is false. When we use the NOT operator, we fetch only...

7 minutes read.

SQL Data Definition Language

Data definition language directly effects on the structure/schema of the database. CREATE, ALTER, DROP are the commands of DDL.CREATE: Creates new database, table, or view of table.ALTER: Modifies the database or...

3 minutes read.

How to use INNER JOIN in SQL

In this article, we will learn about the INNER JOIN concept and how to use it in SQL with the WHERE clause. What is INNER JOIN in SQL? Inner Join is a...

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

DELETE VS DROP in SQL

This page contains all the information about the Delete command and Drop command concept and the difference between the DELETE and DROP commands in SQL. What is the DELETE command in...

3 minutes read.

SQL SELECT IN

SQL SELECT IN is a logical operator in Structured Query Language. It is used in SQL queries to reduce the use of multiple 'OR' operators. s The IN operator in SQL...

6 minutes read.

SQL KEYS

SQL KEYS are single or multiple attributes used to get data from the table according to the requirement or condition. They can also be used to set up relationships amongst...

3 minutes read.

SQL Right Join

The SQL Right Join query displays all the table records and similar records from the left table. The query display zero records if it doesn’t find any similar records. If...

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

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.

Update Query in SQL

Update is an SQL command that is used to modify the data that in already present in database. Update is a command of DML. DML means Data Manipulation Language. Basically,...

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