SQL Insert Query With Examples

SQL INSERT Query also referred as SQL INSERT INTO is the command used for inserting data into database. SQL INSERT statement allows insertion of data into one table at a time.

SQL Insert Query

SQL INSERT statement inserts data into a table of the database. There are three ways to insert data into database using the INSERT INTO statement. Let’s try to understand all the ways we can use insert query in sql statements.

SQL Insert Examples

The three ways to use INSERT INTO statement are as mentioned below.

  1. SQL Insert with Column Names

    In order to insert data into a table we have to specify the columns and the corresponding values as shown below.

    Syntax:

    In the syntax mentioned above the values should be in the same order as the column mentioned in the INSERT INTO statement. Otherwise it might throw error or data might go into wrong columns. Here is an example of sql insert with column names.

  2. SQL Insert Query without Column Names

    In order to insert data into a table without specifying the columns, it is important to provide the values in the same order as the columns are present in the table. Also, this should be used only when we want to insert data into every column of the table.

    Syntax:

    Example:

    This type of insert is error prone, specially when you add another column in the table later on. The data might get corrupted because of getting inserted into wrong columns, this type of insert query should be avoided wherever possible.

  3. SQL INSERT INTO SELECT

    SQL INSERT statement can be used along with the SELECT statement. This is usually used when we have to insert data from one table to other table.

    Syntax:

    Example:

SQL Insert Multiple Rows

There are situations when multiple record needs to be inserted in the database table. Let’s try to understand how to accomplish such requirements.

Using the below mentioned syntax we can make multiple insertion in the database table.

Example:

In the above mentioned example multiple rows will be inserted in the Student table.

SQL Insert Date

Before I conclude SQL insert tutorial, let’s see how to insert date into tables. This is slightly tricky because there are vendor specific functions to get current date and time. Also there are vendor specific functions to parse string to date. Below queries will work for inserting date in MySQL database table.

Corresponding insert queries for Oracle database.

That’s all for insert query in sql statements.

Reference: Oracle Documentation

By admin

Leave a Reply