SQL INSERT INTO : Statement



Demo Database :

Consider the following demo table –

+----+----------+-----+-----------+----------+
| ID | ENAME    | AGE | ADDRESS   | SALARY   |
+----+----------+-----+-----------+----------+
|  1 | Ramesh   |  32 | Kota      |  2000.00 |
|  2 | Raj      |  23 | Delhi     |  1500.00 |
|  3 | Anand    |  21 | Karachi   |  2000.00 |
|  4 | Saurabh  |  25 | Mumbai    |  6500.00 |
|  5 | Poonam   |  29 | Bhopal    |  8500.00 |
|  6 | Komal    |  23 | Pune      |  4500.00 |
|  7 | Omkar    |  24 | Indore    | 10000.00 |
+----+----------+-----+-----------+----------+

SQL INSERT INTO Statement : No Column Specified

Insert into statement is used to insert a new record in a table. Syntax of the insert statement is as below –

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

Above syntax is used to insert the record to the database table where column names are not specified.

INSERT INTO Employee 
VALUES (8,'Sam',21,'Manali',9000.00);

After the execution of the above query table will look like this –

+----+----------+-----+-----------+----------+
| ID | ENAME    | AGE | ADDRESS   | SALARY   |
+----+----------+-----+-----------+----------+
|  1 | Ramesh   |  32 | Kota      |  2000.00 |
|  2 | Raj      |  23 | Delhi     |  1500.00 |
|  3 | Anand    |  21 | Karachi   |  2000.00 |
|  4 | Saurabh  |  25 | Mumbai    |  6500.00 |
|  5 | Poonam   |  29 | Bhopal    |  8500.00 |
|  6 | Komal    |  23 | Pune      |  4500.00 |
|  7 | Omkar    |  24 | Indore    | 10000.00 |
|  8 | Sam      |  21 | Manali    |  9000.00 |
+----+----------+-----+-----------+----------+

SQL INSERT INTO Statement : Column Specified

In the below syntax, we have specified the column names of the table.

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

Consider the below query –

INSERT INTO Employee (ID, ENAME, AGE ,ADDRESS ,SALARY)
VALUES (8,'Sam',21,'Manali',9000.00);

The output of the above query will be –

+----+----------+-----+-----------+----------+
| ID | ENAME    | AGE | ADDRESS   | SALARY   |
+----+----------+-----+-----------+----------+
|  1 | Ramesh   |  32 | Kota      |  2000.00 |
|  2 | Raj      |  23 | Delhi     |  1500.00 |
|  3 | Anand    |  21 | Karachi   |  2000.00 |
|  4 | Saurabh  |  25 | Mumbai    |  6500.00 |
|  5 | Poonam   |  29 | Bhopal    |  8500.00 |
|  6 | Komal    |  23 | Pune      |  4500.00 |
|  7 | Omkar    |  24 | Indore    | 10000.00 |
|  8 | Sam      |  21 | Manali    |  9000.00 |
+----+----------+-----+-----------+----------+