INSERT in SQL

Let’s understand Insert in SQL. In SQL,  INSERT statement is used to insert the records or data into the table.

 

The SQL INSERT statement is used to insert a single record or multiple records into a table.

  • If the columns are not specified, it means that values are added for all of the columns of the table resulting addition of a single row.

  • If it is required to insert values for one or more specific column(s), then it is necessary to specify the name(s) of the column(s) in the SQL query.

 

SQL Syntax : To insert the single row or insert values in all columns of the table.

SQL > INSERT INTO table_name
VALUES (value1,value2,value3,…);

SQL Syntax : To insert into specific columns of the table.

SQL > INSERT INTO table_name (column1,column2,column3,…)
VALUES (value1,value2,value3,…);

 

INSERT INTO Example

Let’s see the example of the INSERT statement. Let’s insert the data into JOBS table. In jobs table, we have 4 columns (Job_ID, Job_Tittle, Min_Salary, Max_Salary) . We will insert the single record in this table and enter the data in all columns of the table (In this case we don’t need to specify the column name in SQL statement).

 

SQL Syntax :

SQL> Insert Into Jobs
Values (‘IT_DEV’, ‘Developement Engineer’, 90000, 140000);

This will insert the record in Jobs Table, we can verify using Select query.

Insert in SQL

 

INSERT Data Only in Specified Columns

We can insert data only in specific columns. SQL will insert the NULL in not defined columns automatically.

SQL Syntax :

SQL> Insert Into Jobs Columns(Job_Id, Job_Title, Max_Salary)
Values (‘IT_CUS’, ‘System Analyst’, 150000);

 

It will Insert the row in Jobs table and Insert the Null value in column Min_Salary. We will query from Jobs table and will see that record is inserted or not.

Insert in SQL

 

INSERT NULL values in SQL

In SQL, We can also insert the NULL in the table.

SQL Syntax :

SQL> Insert Into Jobs
Values (‘IT_DEV’, ‘Developement Engineer’, NULL, NULL);

 

SQL Syntax :

SQL> Insert Into Jobs Columns(Job_Id, Job_Title, Max_Salary)
Values (‘IT_CUS’, Null, 150000);

 

INSERT Result of a Query in Another Table

We also can Insert the result of one query into the another table. In the below example, We will see how can we insert the result of one query into another table.

SQL Syntax :

SQL > INSERT INTO MonthOrder (ord_date,ord_amount,advance_amount)

          SELECT ord_date,SUM(ord_amount),SUM(advance_amount)

          FROM orders   

          GROUP BY ord_date  

          ORDER BY ord_date DESC ;

 

0 Comment

Leave a Reply