SQL INSERT QUERY - The Coding Shala

Home >> Learn SQL >> SQL INSERT Query

SQL INSERT INTO STATEMENT

SQL 'INSERT INTO' Statement is used to insert a new row of data into the table. There are two basic statements to insert a new row into the table. First is by specifying columns names in which we want to insert data. The second way, if we want to add values in all the columns then we don't need to specify column names.
SQL INSERT INTO QUERY - The Coding Shala

SQL INSERT INTO Syntax 

The following is SQL INSERT INTO syntax with specifying columns name - 
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)   
                   VALUES (value1, value2, value3,...valueN);  

We can use SQL INSERT INTO syntax without specifying columns names. In that case values for every column need to be inserted. 

INSERT INTO TABLE_NAME VALUES (value1,value2,value3,...valueN); 

SQL INSERT INTO Examples - 

We have created the 'Persons' Table that is currently empty. The following query will insert data into the table - 

insert into Persons(PersonID, LastName, FirstName, Address, City)   
       values(1, 'Saini', 'Akshay', 'A-101', 'Pune');

Output -- 

PersonID     LastName     FirstName     Address     City  
 1            Saini         Akshay        A-1       Pune   

  • We can write this query like this also -

insert into Persons values(1, 'last', 'first', 'A-101', 'Pune'); 
  
 PersonID     LastName     FirstName     Address     City  
 1             Saini       Akshay        A-101     Pune  
 1              last          first        A-101     Pune 

  • If we want to insert into 'PersonID', and 'FirstName' then -

insert into Persons(PersonID, FirstName)    
     values(2, 'Akshay'); 
  
 PersonID     LastName     FirstName     Address     City  
 1          Saini         Akshay          A-101     Pune  
 1          last          first           A-101     Pune  
 2          null          Akshay          null     null  




Other Posts You May Like
Prev<< SQL DROP Table                                                                    NEXT >>SQL SELECT Query
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to find sum, product and average of given numbers - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Add two numbers in Scala - The Coding Shala

New Year Chaos Solution - The Coding Shala

Richest Customer Wealth LeetCode Solution - The Coding Shala