Insert Query in SQL

Back to home
Logicmojo - Updated Jan 5, 2023



What is SQL INNER JOIN?

INSERT statement is an SQL Query. It can be used to insert one or more records into a table. To add new records to a table, use the INSERT INTO statement.
Inserting one or more rows into a database table with specified table column values is done with the insert command.

Inserting data into a table can be done in two ways:
1. By inserting a SQL query
     a)Column names can be specified.
     b)Without naming the columns,
2. By inserting a SQL statement into a select statement

Let's Discuss them in brief,

Inserting data directly into a table

The SQL Put INTO command can be used to insert a row into a table. In a table, there are two methods for inserting values.
In this page we will discuss both of the methods in brief to getting the proper idea on it.



Method 1 : You don't need to indicate the column name where the data will be entered in the first procedure; all you need are their values.


Syntax :
INSERT INTO table_name
VALUES (value1, value2, ....);

Here, value1, value2.. indicates the value that need to be inserted in the table.

Let's understand it by taking an example,

Inserting rows in Student Table :
INSERT INTO Student VALUES (1, 'Amit', 7545XXXXXX, 18);
INSERT INTO Student VALUES (2, 'Aayushi', 7898XXXXXX, 21);
INSERT INTO Student VALUES (3, 'Riya', 7556XXXXXX, 20);
INSERT INTO Student VALUES (4, 'Saransh', 9867XXXXXX, 19);
INSERT INTO Student VALUES (5, 'Sarthak', 8848XXXXXX, 21);
INSERT INTO Student VALUES (7, 'Vishal', 7821XXXXXX, 18);
INSERT INTO Student VALUES (8, 'Yashi', 9878XXXXXX, 20);

After the execution of the above query we can verify the data in the table as follows,

SELECT *
FROM Student;

Output :



Method 2 : The second method specifies the column name as well as the values to be inserted.


Syntax :
INSERT INTO table_name (column1, column2, column3,...columnN)  
VALUES (value1, value2, value3,....valueN)

Here, column1, column2, column3,...columnN are the names of the columns in the table into which you want to insert the data and value1, value2.. indicates the respective values that need to be inserted in the table.

Let's understand it by taking an example,

Inserting rows in Student Table :
INSERT INTO Student (Roll_no, Name, Phone No., Age)
VALUES (1, 'Amit', 7545XXXXXX, 18);

INSERT INTO Student (Roll_no, Name, Phone No., Age)
VALUES (2, 'Aayushi', 7898XXXXXX, 21);

INSERT INTO Student (Roll_no, Name, Phone No., Age)
VALUES (3, 'Riya', 7556XXXXXX, 20);

INSERT INTO Student (Roll_no, Name, Phone No., Age)
VALUES (4, 'Saransh', 9867XXXXXX, 19);

INSERT INTO Student (Roll_no, Name, Phone No., Age)
VALUES (5, 'Sarthak', 8848XXXXXX, 21);

INSERT INTO Student (Roll_no, Name, Phone No., Age)
VALUES (7, 'Vishal', 7821XXXXXX, 18);

INSERT INTO Student (Roll_no, Name, Phone No., Age)
VALUES (8, 'Yashi', 9878XXXXXX, 20);

After the execution of the above query we can verify the data in the table as follows,

SELECT *
FROM Student;

Output :



Insert Data Only in Specified Columns

It's also possible to insert data only into certain columns.

The SQL query below will create a new record, but only populate the "EmployeeName," "City," and "Country" fields (the "EmpID" column will be updated automatically):



INSERT INTO Employee (EmployeeName, City, Country)
VALUES ('Shivam', 'Noida', 'India');

Inserting data through SELECT Statement

You can populate a table with data from another table using a select statement, as long as the second table provides a set of fields that are required to populate the first table.


INSERT INTO table_name  
[(column1, column2, .... column)]  
SELECT column1, column2, .... Column N  
FROM table_name 
WHERE condition;  


With this article at Logicmojo, you must have the complete idea of Insert Query in SQL.

Logicmojo Learning Library