×

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 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 the following two ways through which we can add data to the table:

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 TABLE

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 TABLE

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:

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

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.

SQL SELECT SUM

The SQL Sum() function is an aggregate function in SQL that returns the total values of an expression. The expression may be numerical, or it may be an expression. Syntax: SELECT SUM(columnname)...

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 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 ORDER BY

SQL ORDER BY The SQL ORDER BY clause is used to sort the data stored in tables in the database. The sorting can be done in an ascending way, descending way,...

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

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

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.

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.

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.

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.

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.

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 WHERE Statement

SQL WHERE Statement Introduction WHERE clause is used to include a condition while fetching data from tables.When you have to specify a condition that has to be obeyed while data is...

9 minutes read.

SQL Scalar Functions

TEACHER Table LCASE() Function The LCASE() function is used to return lower case values of specified column. Syntax: Select Lcase(column_name) from table_name; Example: Select Lcase(teacher_name) from teacher; Syntax: Select Lcase(column_name) from table_name [where condition]; Example: Select Lcase(teacher_name) from teacher where teacher_id=1; UCASE() Function The UCASE()...

1 minute read.

SQL Injection

SQL injection is a technique, this may destroy the database. It is one type of hacking technique. SQL IN WEB PAGES: Injection occurs when we ask for input like an id or...

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.

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.