SQL Tutorial
SQL Database
SQL Table
SQL Select
SQL Order By
SQL Insert
SQL Update
SQL Delete
Difference
SQL Injection
SQL String Functions
Miscl
- SQL Formatter
- SQL group by
- SQL add/drop/update column operation
- SQL CAST Function
- SQL Comments
- SQL CONCAT Function
- CTE (Common Table Expression)SQL
- How to use distinct in SQL?
- Joining Three or More Tables in SQL
- What is Web SQL?
- How to create functions in SQL?
- How to run SQL Script?
- How to Delete Duplicate Rows in SQL?
- Nth Highest salary
- 12 Codd's Rules
- SQL EXCEPT
- Types of SQL JOIN
- Change datatype of column in SQL
- SQL Auto Increment
- SQL Like
- Commit and Rollback in SQL
- SQL Concatenate
- SQL get month from the date
- Savepoint in SQL
- SQL ORDER BY DATE
- TIME Datatype in SQL
- SQL BETWEEN
- CRUD Operations in SQL
- SQL INDEX
- Scalar Functions in SQL
- SET Operators in SQL
- Types of SQL Commands
- TCL Commands in SQL
- SQL Subquery
- SQL View
- Constraints in SQL
- Pattern Matching in SQL
- SQL Date Functions
- DDL Commands in SQL
- DML Commands in SQL
- SQL CASE
- SQL Inner Join
- SQL IN Operator
- Check Constraint in SQL
- SQL CLAUSES
- SQL LOGICAL OPERATORS
- Delete Column from Table
- Add Column in the Table
- Delete one row in SQL
- Change the Column Value
- How to Add Foreign Key in SQL
- Add a Primary Key
- Insert One or More rows
- How to Use LIKE in SQL
- Cursor in SQL
- Difference Between DROP and Truncate
- SQL Comparison Operators
- SQL COUNT WHERE
- SQL SELECT MIN
- SQL Stored Procedure
- SQL SELECT AVG
- SQL SELECT MAX
- SQL ADD COLUMN
- How to use Auto-Increment in SQL
- SQL Languages
- SQL Arithmetic Operators
- How to Use GROUP BY in SQL
- How to Use ORDER BY in SQL
- Trigger in SQL
- What is Race Condition
- SQL COUNT DISTINCT
PL/SQL Tutorial
Sql Interview Question
SQl Quiz
Insert One or More rows
In this article, we will learn how to insert one or more records in the table of Structured Query Language.
The INSERT INTO command in SQL allows the database users to insert one or more records into the SQL table.
Following are the two syntaxes for inserting the single row in the table:
Syntax 1:
This syntax inserts the row in the SQL table by specifying the column names of the table.
Syntax 2:
In this syntax, we have no need to specify the column names for inserting the row into the SQL table.
If you want to insert the record into the table, you have to follow the following steps one by one in the given sequence:
- Create the Database in the system.
- Create the Table in the database.
- Insert one row into the table.
- View the table.
Now, we are going to explain each step with an example:
Step 1: Create a Database
In the Structured Query Language, creating a database is the first step for storing the structured tables in the database.
Use the following SQL syntax to create a database:
Suppose you want to create the School database. For this, you have to type the following command in Structured Query Language:
Step 2: Create a Table and Insert the data
Now, you have to use the following syntax for creating the table in the SQL database:
(
column_Name_1 data type (size of the column_1),
column_Name_2 data type (size of the column_2),
column_Name_3 data type (size of the column_3),
...
column_Name_N data type (size of the column_N)
);
Suppose you want to create the Teacher table in the School database. For this, you have to type the following query in your SQL application:
(
Teacher_ID INT AUTO_INCREMENT PRIMARY KEY,
Teacher_Name VARCHAR (80),
Teacher_Subject VARCHAR (30) NOT NULL,
Teacher_Address VARCHAR (30),
Teacher_Age INT
) ;
Step 3: Insert one row into the table
The following query inserts the record of only one teacher into the Teacher table:
Step 4: View the table
If you want to view the data of the Teacher table, then you have to write the following query in your SQL application:
Teacher_ID | Teacher_Name | Teacher_Subject | Teacher_Address | Teacher_Age |
---|---|---|---|---|
2001 | Manoj | Maths | Delhi | 27 |
Table: Teacher
Insert Multiple Rows into the table
In SQL, you can easily insert more than one row in the table using a single query.
The following Insert Into statement inserts the record of five teachers into the Teacher table:
( 2003, Vishal, English, Gorakhpur, 26),
( 2004, Shobhit, SST, Kanpur, 27),
( 2005, Rohit, Science, Lucknow, 29),
( 2006, Yogesh, Computer, Jaipur, 28) ;
To check the result of the above INSERT query, you have to write the following SELECT query in SQL:
Output:
Teacher_ID | Teacher_Name | Teacher_Subject | Teacher_Address | Teacher_Age |
---|---|---|---|---|
2001 | Manoj | Maths | Delhi | 27 |
2002 | Anita | Hindi | Ghaziabad | 28 |
2003 | Vishal | English | Gorakhpur | 26 |
2004 | shobhit | SST | Kanpur | 27 |
2005 | Rohit | Science | Lucknow | 29 |
2006 | Yogesh | Computer | Jaipur | 28 |
Table: Teacher
Insert Rows from the SELECT statement
If you want to insert the data of another table in the new table, use the INSERT statement with the SELECT statement.
The following statement passes the data of one table into another table:
SELECT
(Column_Name_1, Column_Name_2, Column_Name_3, ....., Column_Name_N)
FROM Old_Table_Name
WHERE [ condition ];
Example
The data of the old table is shown in the following table:
Teacher_ID | Teacher_Name | Teacher_Subject | Teacher_Address | Teacher_Age |
---|---|---|---|---|
2001 | Manoj | Maths | Delhi | 27 |
2002 | Anita | Hindi | Delhi | 28 |
2003 | Vishal | English | Gorakhpur | 26 |
2004 | shobhit | SST | Delhi | 27 |
2005 | Rohit | Science | Lucknow | 29 |
2006 | Yogesh | Computer | Delhi | 28 |
Table: Old_Teacher
Suppose you have a New_Teacher table whose structure is the same as the Old_Teacher table, and you want to transfer the data of those teachers into the new table from the old table whose address is Delhi. For this operation, you have to use the following INSERT statement with the SELECT query:
SELECT Teacher_ID, Teacher_Name, Teacher_Subject
FROM Old_Teacher
WHERE Teacher_Address = 'Delhi' ;
To check the result of the above query, use the following SELECT statement:
Teacher_ID | Teacher_Name | Teacher_Subject |
---|---|---|
2001 | Manoj | Maths |
2002 | Anita | Hindi |
2004 | shobhit | SST |
2006 | Yogesh | Computer |
Table: New_Teacher