SQL Tutorial

SQL Tutorial SQL Introduction SQL Syntax SQL Data Types SQL OPERATORS SQL COMMANDS SQL Queries

SQL Database

SQL Create Database SQL DROP Database SQL SELECT Database

SQL Table

SQL TABLE SQL CREATE TABLE SQL COPY TABLE SQL ALTER TABLE SQL DELETE SQL TRUNCATE TABLE SQL DROP TABLE SQL UPDATE TABLE SQL INSERT TABLE

SQL SELECT

SQL SELECT Statement SQL SELECT WHERE Clause SQL SELECT IN Operator SQL BETWEEN Operator SQL SELECT BETWEEN Operator SQL SELECT AND Operator SQL SELECT OR Operator SQL SELECT LIKE Operator SQL SELECT DISTINCT SQL SELECT SUM SQL SELECT MAX SQL SELECT MIN SQL SELECT AVG

SQL Clause

SQL WHERE Clause SQL GROUP BY CLAUSE SQL ORDER BY Clause SQL HAVING Clause

SQL INSERT

SQL INSERT Statement SQL INSERT INTO Statement SQL INSERT INTO Values SQL INSERT INTO SELECT SQL Insert multiple rows

SQL JOIN

SQL JOIN SQL Inner Join SQL Left Join SQL Right Join SQL Full Join SQL CROSS Join

SQL OPERATOR

SQL Comparison SQL LOGICAL Operator SQL Cast Operator SQL Arithmetic

Difference

SQL vs NOSQL WHERE vs HAVING DELETE vs DROP GROUP BY vs ORDER BY DROP vs TRUNCATE SQL IN vs SQL EXISTS Difference between Delete, Drop and Truncate in SQL

MISC

SQL SubQuery SQL CASE Commit and Rollback in SQL Pattern Matching in SQL DDL Commands in SQL DML Commands in SQL Types of SQL Commands SQL COUNT SQL Primary Key SQL FOREIGN KEY SET Operators in SQL Check Constraint in SQL SQL EXCEPT SQL VIEW SQL WHERE Statement SQL CRUD Operation Where Condition in SQL TCL Commands in SQL Types of SQL JOINS SQL Nth Highest Salary SQL NOT OPERATOR SQL UNION ALL SQL INTERSECT SQL Data Definition Language SQL Data Manipulation Language SQL Data Control Language SQL CONSTRAINTS SQL Aggregate Operators SQL KEYS Codd’s Rules in SQL What is SQL Injection? Trigger In SQL SQL WHERE Multiple Conditions Truncate function in SQL SQL Formatter WEB SQL SQL Auto Increment Save Point in SQL space() function in SQL SQL Aggregate Functions SQL Topological Sorting SQL Injection SQL Cloning Tables SQL Aliases SQL Handling Duplicate Update Query in SQL Grant Command in SQL SQL SET Keyword SQL Order BY LIMIT SQL Order BY RANDOM

How To

How to use the BETWEEN operator in SQL How To Use INNER JOIN In SQL How to use LIKE in SQL How to use HAVING Clause in SQL How to use GROUP BY Clause in SQL How To Remove Duplicates In SQL How To Delete A Row In SQL How to add column in table in SQL ? How to drop a column in SQL? How to create a database in SQL? How to use COUNT in SQL? How to Create Temporary Table in SQL? How to Add Foreign Key in SQL? How to Add Comments in SQL? How To Use Group By Clause In SQL How To Use Having Clause In SQL How To Delete Column In Table How To Compare Date In SQL How index works in SQL How to calculate age from Date of Birth in SQL How to Rename Column name in SQL What are single row and multiple row subqueries?

SQL INSERT INTO Statement

SQL INSERT INTO statement adds data to the newly created tables or existing tables. We can add single records or multiple records in a table by using this query.

There are two possible ways to add data to a table:

  • Mention the field's name and the values are added to the table.
  • If we add values for all the table fields, we do not need to mention the field's name in the SQL INSERT INTO statement or query. But, make sure the order of the values is the same as the order of the columns in the table.

Syntax for SQL INSERT INTO statement by mentioning the fields names in the statement or query:  

INSERT INTO Table_Name (Column_Name1, Column_Name2, Column_Name3, Column_Name4, Column_Name5) VALUES (Value1, Value2, Value3, Value4, Value5);

Column_Name1, Column_Name2, Column_Name3, Column_Name4, Column_Name5 are the fields name in the tables into which we want to add values.

The syntax for SQL INSERT INTO statement without mentioning the names of the fields in the statement or query:  

INSERT INTO Table_Name VALUES (Value1, Value2, Value3, Value4, Value5);

The above syntax is used to insert values in all the fields of the tables.

The following statements would create eight records in the Customer table.

INSERT INTO Customer (Customer_Id, Customer_Name, Age, Address, Salary)

VALUES (1, 'Rakesh', 32, 'Ahmedabad', 20000);

INSERT INTO Customer (Customer_Id, Customer_Name, Age, Address, Salary)

VALUES (2, 'Kamlesh', 27, 'Delhi', 15000);

INSERT INTO Customer (Customer_Id, Customer_Name, Age, Address, Salary)

VALUES (3, 'kaustubh', 25, 'Pune', 20000);

INSERT INTO Customer (Customer_Id, Customer_Name, Age, Address, Salary)

VALUES (4, 'Chaitali', 25, 'Mumbai', 15000);

INSERT INTO Customer (Customer_Id, Customer_Name, Age, Address, Salary)

VALUES (5, 'Himesh', 29, 'Delhi', 45000);

INSERT INTO Customer (Customer_Id, Customer_Name, Age, Address, Salary)

VALUES (6, 'Komal', 22, 'MP', 45000);

INSERT INTO Customer (Customer_Id, Customer_Name, Age, Address, Salary)

VALUES (7, 'Nikhlesh', 28, 'Delhi', 40000);

INSERT INTO Customer (Customer_Id, Customer_Name, Age, Address, Salary)

VALUES (8, 'Kamolika', 24, 'Pune', 50000);

In the above INSERT INTO statement, we have added the records in the Customer table by mentioning the table's field names.

We can add the data to the table without mentioning the field's name of the table:

The following example statements would create six records in the Customer table.

INSERT INTO Customer VALUES (9, ‘Raman’, 30, ‘Mumbai’, 35500);

INSERT INTO Customer VALUES (10, ‘Manoj’, 40, ‘Pune’, 45000);

INSERT INTO Customer VALUES (11, ‘Shweta’, 26, ‘MP’, 42500);

INSERT INTO Customer VALUES (12, ‘Shivani’, 25, ‘Delhi’, 50000);

INSERT INTO Customer VALUES (13, ‘Rahul’, 28, ‘Nashik’, 34000);

INSERT INTO Customer VALUES (14, ‘Sahil’, 22, ‘Nashik’, 27000);

In the above INSERT INTO statement example, we have added the records without mentioning the fields name in the query.

All the above queries would generate the following data in the Customer table as shown below:

Customer_IdCustomer_NameAgeAddressSalary
1Rakesh32Ahmedabad20000
2Kamlesh27Delhi15000
3Kausubh25Pune20000
4Chaitali25Mumbai15000
5Himesh29Delhi45000
6Komal22MP45000
7Nikhlesh28Delhi40000
8Kamolika24Pune50000
9Raman30Mumbai35500
10Manoj40Pune45000
11Shweta26MP42500
12Shivani25Delhi50000
13Rahul28Nashik34000
14Sahil22Nashik27000
SQL INSERT INTO Statement

INSERT INTO SELECT Statement.

INSERT INTO SELECT statement is also a way to add records to the table. INSERT INTO SELECT statement is used to insert records into one table from the existing table. Where all the fields and order of the fields are the same.

We will create another Customer and use the same fields from the above Customer table.

CREATE TABLE Customers (Customer_Id int Primary key, Customer_Name varchar(40), Age int, Address Varchar(20), Salary int);

Use the DESC command followed by the Customers table name to show the table structure.

FieldsTypeNullKeyDefaultExtra
Customer_IdInt(11)NOPRINULL 
Customer_NameVarchar(40)YES NULL 
AgeInt(11)YES NULL 
AddressVarchar(20)YES NULL 
SalaryInt(11)YES NULL 

We can add records into a table through the SELECT statement over another table.

Syntax of the INSERT INTO SELECT:

INSERT INTO Table1 [(Column_Name1, Column_Name2, Column_Name3, Column_Name4, Column_Name5)] SELECT Column_Name1, Column_Name2, Column_Name3, Column_Name4, Column_Name5 FROM Table2;

Example of the INSERT INTO SELECT statement:

INSERT INTO Customers SELECT * FROM Customer;

In the above query INSERT INTO SELECT example, all the records from the Customer table are added to the Customers table.

The following data in the Customer table is shown below:

Customer_IdCustomer_NameAgeAddressSalary
1Rakesh32Ahmedabad20000
2Kamlesh27Delhi15000
3Kausubh25Pune20000
4Chaitali25Mumbai15000
5Himesh29Delhi45000
6Komal22MP45000
7Nikhlesh28Delhi40000
8Kamolika24Pune50000
9Raman30Mumbai35500
10Manoj40Pune45000
11Shweta26MP42500
12Shivani25Delhi50000
13Rahul28Nashik34000
14Sahil22Nashik27000
SQL INSERT INTO Statement