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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago