Categories: DBMS

SQL: INSERT Query

Insert is a commonly used command in the Structured Query Language (SQL) data manipulation language (DML) used by SQL Server and Oracle relational databases. The insert command inserts one or more rows into a database table with the specified table column values. The insert statement is the first DML command executed immediately after creating a table.

Syntax:

There are two basic syntaxes of the INSERT query, shown below.

  • Only values: The first method is to specify only the data value to be inserted without specifying the column names.
INSERT INTO TABLE_NAME VALUES (value1,value2,value3,...valueN);
  • Both column names and values: In the second method, we will specify both the columns to be filled and their associated values, as shown below:
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)  
VALUES (value1, value2, value3,...valueN);
Example:

Insert value into Customer Table:

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 );

Insert value into the specific column:

INSERT INTO CUSTOMERS (ID,NAME)
VALUES (2, 'Fatima');

Insert NULL value to a column:

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (3, 'Sharda', 29, NULL, 34000.00 );

Insert Default value to a column:

INSERT INTO CUSTOMERS VALUES (4, 'Abdul', 30, NULL, default );
Output:
IDNAMEAGEADDRESSSALARY
1Ramesh32Ahmedabad25000.00
2Fatima
3Sharda2934000.00
4Abdul3010000.00
  • Using SELECT in INSERT INTO Statement:

To copy rows from one table and insert them into another, we can use the SELECT statement in conjunction with the INSERT INTO statement. This statement is used in the same way as the INSERT INTO statement. In this case, the SELECT statement is used to select data from a different table.

Syntax:

INSERT INTO first_table SELECT * FROM second_table;

OR

INSERT INTO table1 SELECT * FROM table2 WHERE condition;

here we are inserting into another table based on some conditions.

Note: also read about SQL: Truncate, Drop, Rename Query

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

3 days ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

2 weeks ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

3 weeks ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

1 month ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

1 month ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

1 month ago