Home » SQL » INSERT INTO command in SQL – Best 3 Example

INSERT INTO command in SQL – Best 3 Example

Definition: INSERT INTO command in SQL 

INSERT INTO command in SQL is used to insert one or more then one rows in SQL table OR you can say used to Insert new records in a table

Using INSERT INTO command in SQL , either you can insert new records direct from SQL query And Second, INSERT also used to insert/add records in a table from another existing table in SQL

INSERT INTO statement have two way to insert records.

  • First Case: Specifies both the column names and the values to be inserted
  • Second case: Do not need to specify the column names in the SQL query.

First Case Syntax:

Syntax:

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

 

Example #1: First Case

INSERT INTO Customers_Tbl
           (Customer_ID, Customer_Name, Region, Customer_Segment)
VALUES
           ('1230', 'Tom Erico', 'West', 'Home Office')

Result:

Insert in SQL

if you would like to check that result or Row in SQL table you can run the SELECT query for these columns with condition on any column using WHERE Clause

Below we are trying to get result applying filter on Customer_name

SELECT 
      Customer_ID, Customer_Name, Region, Customer_Segment
FROM 
      Customers_Tbl 
WHER 
      Customer_Name = 'Tom Erico'

Result: you will see below result that one row or record has been added in the Customers_Tbl

Insert in SQL

 

Second Case Syntax:

  • Second, do not need to specify the column names in the SQL query.

Syntax:

INSERT INTO
           Table_name
VALUES
          (value1, value2, value3 ...)

 

Example #2: Second Case

In this Second case, you need to insert data or rows in SQL table only in the same sequence of column structure in SQL table and same number of data should be inserted in table.

INSERT INTO 
           Orders_Tbl
VALUES
          ('1235', '2011-10-22', '8748', 'Not Specified', '30', '288.56', '0.03')

Result:

Insert in SQL

If you will miss any data for any one column or many columns then data will not inserted and it will though the error as shown below

Error Result:

Insert in SQL

 

Example #3: INSERT INTO data from another SQL Table

It is really good feature and easy to import and load data from one table to another table, its need sometime to use this functionalities depends on your requirements

INSERT INTO 
        Customers_Tbl_Temp
FROM
        Customers_Tbl

In this case all records from Customers_Tbl will be  Inserted into Customers_Tbl_Temp, However, it is important If you are not specifying any columns names then both Tables should have same number of columns and columns names.

If there is any difference in columns numbers and names, Then you can specify the name of columns in both tables in SQL query, then it will work.