HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

The SQL INSERT INTO Statement

The INSERT INTO statement is used to insert new records in a table.

INSERT INTO Syntax


It is possible to write the INSERT INTO statement in two ways:

1. Specify both the column names and the values to be inserted:

Syntax

INSERT INTO table_name ( column1, column2, column3, ...) 
VALUES ( value1,value2,value3,...);
2. If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query. However, make sure the order of the values is in the same order as the columns in the table. Here, the INSERT INTO syntax would be as follows:


Syntax

INSERT INTO table_name 
VALUES ( value1,value2,value3,...);

Demo Employee table


This employee table is used for examples:

demo employee table

INSERT INTO Example


Example

The following SQL statement inserts a new record in the "employee" table:
 INSERT    INTO  employee 
 ( Employee_name , Address , City , Pincode , Country )
VALUES ('Krity Rana' , 'Nutannagar' ,'Hazaribagh' ,'825301' , 'India' ) ;
You can click on above box to edit the code and run again.

Output

 insert new row

Did you notice that we did not insert any number into the Employee_id field?
The Employee_id column is an auto-increment field and will be generated automatically when a new record is inserted into the table.

Insert Data Only in Specified Columns


It is also possible to only insert data in specific columns.

The following SQL statement will insert a new record, but only insert data in the "Employee_name", "City", and "Country" columns (Employee_id will be updated automatically):

Example

  INSERT    INTO  employee 
( Employee_name , City , Country )
VALUES ('Chandan Gupta' , 'kalkata' , 'India' ) ;
You can click on above box to edit the code and run again.

Output

 insert column

Insert Multiple Rows


It is also possible to insert multiple rows in one statement.
To insert multiple rows of data, we use the same INSERT INTO statement, but with multiple values:

Example

The following SQL statement inserts a new record in the "employee" table:
INSERT INTO employee ( Employee_name , Address , City , Pincode , Country )
VALUES
('Ranjeet Singh' , 'Patel Nagar,patel Rd,Block 2' ,'Delhi' ,'110008' , 'India' ), ('Ishfak Khan' , 'KD Colony,Sector 12' ,'Delhi' ,'110022' , 'India' ), ('Jagdish Khurana' , '11,Lord Sinha Road' ,'KolKata' ,'700071' , 'India' ) ;
You can click on above box to edit the code and run again.

Output

 insert more row