The Wayback Machine - http://web.archive.org/web/20201230130957/https://www.geeksforgeeks.org/sql-insert-statement/amp/

SQL | INSERT INTO Statement

The INSERT INTO statement of SQL is used to insert a new row in a table. There are two ways of using INSERT INTO statement for inserting rows:

  1. Only values: First method is to specify only the value of data to be inserted without the column names.

    INSERT INTO table_name VALUES (value1, value2, value3,…);
    table_name: name of the table.
    value1, value2,.. : value of first column, second column,… for the new record

  2. Column names and values both: In the second method we will specify both the columns which we want to fill and their corresponding values as shown below:

    INSERT INTO table_name (column1, column2, column3,..) VALUES ( value1, value2, value3,..);
    table_name: name of the table.
    column1: name of first column, second column …
    value1, value2, value3 : value of first column, second column,… for the new record

Queries:



Method 1 (Inserting only values) :

INSERT INTO Student VALUES (‘5′,’HARSH’,’WEST BENGAL’,’XXXXXXXXXX’,’19’);

Output:
The table Student will now look like:

ROLL_NO NAME ADDRESS PHONE Age
1 Ram Delhi XXXXXXXXXX 18
2 RAMESH GURGAON XXXXXXXXXX 18
3 SUJIT ROHTAK XXXXXXXXXX 20
4 SURESH Delhi XXXXXXXXXX 18
3 SUJIT ROHTAK XXXXXXXXXX 20
2 RAMESH GURGAON XXXXXXXXXX 18
5 HARSH WEST BENGAL XXXXXXXXXX 19

Method 2 (Inserting values in only specified columns):

INSERT INTO Student (ROLL_NO, NAME, Age) VALUES (‘5′,’PRATIK’,’19’);

Output:
The table Student will now look like:

ROLL_NO NAME ADDRESS PHONE Age
1 Ram Delhi XXXXXXXXXX 18
2 RAMESH GURGAON XXXXXXXXXX 18
3 SUJIT ROHTAK XXXXXXXXXX 20
4 SURESH Delhi XXXXXXXXXX 18
3 SUJIT ROHTAK XXXXXXXXXX 20
2 RAMESH GURGAON XXXXXXXXXX 18
5 PRATIK null null 19

Notice that the columns for which the values are not provided are filled by null. Which is the default values for those columns.

Using SELECT in INSERT INTO Statement



We can use the SELECT statement with INSERT INTO statement to copy rows from one table and insert them into another table.The use of this statement is similar to that of INSERT INTO statement. The difference is that the SELECT statement is used here to select data from a different table. The different ways of using INSERT INTO SELECT statement are shown below:

Table2: LateralStudent

ROLL_NO NAME ADDRESS PHONE Age
7 SOUVIK DUMDUM XXXXXXXXXX 18
8 NIRAJ NOIDA XXXXXXXXXX 19
9 SOMESH ROHTAK XXXXXXXXXX 20

Queries:

 



  • To insert multiple rows in a table using Single SQL Statement
     
    INSERT INTO table_name(Column1,Column2,Column3,.......) 
    VALUES (Value1, Value2,Value3,.....),
            (Value1, Value2,Value3,.....),
             (Value1, Value2,Value3,.....),
             ............................. ;
    table_name: name of the table
    Column1: name of first column, second column …
    Value1, Value2, Value3 : value of first column, second column,… for each new row inserted  
    You need To provide Multiple lists of values where each list is separated by ",". Every list of value corresponds to values to be inserted in each new row of the table. 
    Values in the next list tells values to be inserted in the next Row of the table.     
    

    Example:

    The following SQL statement insert multiple rows in Student Table.

    Input :
    INSERT INTO STUDENT(ID, NAME,AGE,GRADE,CITY) VALUES(1,"AMIT KUMAR",15,10,"DELHI"),
                                                       (2,"GAURI RAO",18,12,"BANGALORE"),
                                                       (3,"MANAV BHATT",17,11,"NEW DELHI"),
                                                        (4,"RIYA KAPOOR",10,5,"UDAIPUR");
    

    Output : STUDENT TABLE
    This query will insert all values in each successive row in the STUDENT TABLE . Thus STUDENT Table will look like this:

    ID NAME AGE GRADE CITY
    1 AMIT KUMAR 15 10 DELHI
    2 GAURI RAO 16 12 BANGALORE
    3 MANAV BHATT 17 11 NEW DELHI
    4 RIYA KAPOOR 10 5 UDAIPUR
  • This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

    Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.




    Article Tags :
    Practice Tags :