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
SQL COPY TABLE
If you want to copy the data of one SQL table into another SQL table in the same SQL server, then it is possible by using the SELECT INTO statement in SQL.
The SELECT INTO statement in Structured Query Language copies the content from one existing table into the new table. SQL creates the new table by using the structure of the existing table.
Syntax of SELECT INTO statement in SQL
Examples of SELECT INTO statement in SQL
In this article, we have taken the following three different SQL examples which will help you how to copy the content of one table into another table in SQL:
Example 1: In this example, we have a table called Cars with three columns:
Car Name | Car Color | Car Cost |
---|---|---|
Hyundai Creta | White | 10,85,000 |
Hyundai Venue | White | 9,50,000 |
Hyundai i20 | Red | 9,00,000 |
Kia Sonet | White | 10,00,000 |
Kia Seltos | Black | 8,00,000 |
Swift Dezire | Red | 7,95,000 |
Table: Cars
- Suppose you want to copy the content of the above Car table into the new table Car_Details. For this, you have to type the following query in SQL:
-
SELECT * INTO Car_Details FROM Cars;
- Let's check the Car_Details table is created successfully or not in the database:
-
SELECT * FROM Car_Details;
Car Name | Car Color | Car Cost |
---|---|---|
Hyundai Creta | White | 10,85,000 |
Hyundai Venue | White | 9,50,000 |
Hyundai i20 | Red | 9,00,000 |
Kia Sonet | White | 10,00,000 |
Kia Seltos | Black | 8,00,000 |
Swift Dezire | Red | 7,95,000 |
Table: Car_Details
Example 2: In this example, we have a table called Employee with four columns:
Emp_Id | Emp_Name | Emp_Salary | Emp_City |
---|---|---|---|
201 | Abhay | 25000 | Goa |
202 | Ankit | 45000 | Delhi |
203 | Bheem | 30000 | Goa |
204 | Ram | 29000 | Goa |
205 | Sumit | 40000 | Delhi |
- Suppose you want to copy the record of the above Employee table into the new table Coding_Employees. For this, you have to type the following query in SQL:
-
SELECT * INTO Coding_Employees FROM Employee
- Let's check the Coding_Employees table is created successfully or not in the database:
-
SELECT * FROM Coding_Employees;
Emp_Id | Emp_Name | Emp_Salary | Emp_City |
---|---|---|---|
201 | Abhay | 25000 | Goa |
202 | Ankit | 45000 | Delhi |
203 | Bheem | 30000 | Goa |
204 | Ram | 29000 | Goa |
205 | Sumit | 40000 | Delhi |
Table: Coding_Employees
Example 3: In this example, we have a table called Student with four columns:
RollNo | Name | Marks | Age |
---|---|---|---|
1001 | Bhanu | 88 | 17 |
1002 | Raman | 82 | 16 |
1003 | Sumit | 80 | 16 |
1004 | Shobhit | 95 | 15 |
1005 | Akash | 85 | 16 |
Table: Student
- Suppose you want to copy the record of the above Student table into the new table Class_12_Students. For this, you have to type the following query in SQL:
-
SELECT * INTO Class_12_Students FROM Student;
- Let's check the table is Class_12_Students table created successfully or not in the database:
-
SELECT * FROM Class_12_Students;
RollNo | Name | Marks | Age |
---|---|---|---|
1001 | Bhanu | 88 | 17 |
1002 | Raman | 82 | 16 |
1003 | Sumit | 80 | 16 |
1004 | Shobhit | 95 | 15 |
1005 | Akash | 85 | 16 |
Table: Class_12_Students
Example 4: In this example, we have a table called Cars with three columns:
Car Name | Car Color | Car Cost |
---|---|---|
Hyundai Creta | White | 10,85,000 |
Hyundai Venue | White | 9,50,000 |
Hyundai i20 | Red | 9,00,000 |
Kia Sonet | White | 10,00,000 |
Kia Seltos | Black | 8,00,000 |
Swift Dezire | Red | 7,95,000 |
Table: Cars
- Suppose you want to copy Car_Color and Car_Name columns of the above Cars table into the new table Car_Color. For this, you have to type the following query in SQL:
-
SELECT Car_Name, Car_Color INTO Car_Color FROM Cars;
- Let's check the Car_Color table is created successfully or not in the database:
-
SELECT * FROM Car_Color;
Car Name | Car Color |
---|---|
Hyundai Creta | White |
Hyundai Venue | White |
Hyundai i20 | Red |
Kia Sonet | White |
Kia Seltos | Black |
Swift Dezire | Red |
Table: Car_Color
Syntax of SELECT INTO statement with WHERE clause in SQL
Examples of SELECT INTO statement with WHERE clause in SQL
Here, we have taken the following three different SQL examples, which will help you how to copy the content of one table into another table with a specific condition in SQL:
Example 1: In this example, we have a table called Cars with three columns:
Car Name | Car Color | Car Cost |
---|---|---|
Hyundai Creta | Black | 10,85,000 |
Hyundai Venue | Black | 9,50,000 |
Hyundai i20 | Red | 9,00,000 |
Kia Sonet | White | 10,00,000 |
Kia Seltos | Black | 8,00,000 |
Swift Dezire | Red | 7,95,000 |
Table: Cars
-
Suppose we want to copy only the record of those cars whose color is black. For this, we have to type the following query in sql
-
SELECT * INTO Black_Car_Details FROM Cars WHERE Car_Color = 'Black';
- Let's check the Black_Car_Details table is created successfully or not in the database:
-
SELECT * FROM Black_Car_Details;
Car Name | Car Color | Car Cost |
---|---|---|
Hyundai Creta | Black | 10,85,000 |
Hyundai Venue | Black | 9,50,000 |
Kia Seltos | Black | 8,00,000 |
Table: Black_Car_Details
Example 2: In this example, we have a table called Employee with four columns:
Emp_Id | Emp_Name | Emp_Salary | Emp_City |
---|---|---|---|
201 | Abhay | 45000 | Goa |
202 | Ankit | 45000 | Delhi |
203 | Bheem | 38000 | Goa |
204 | Ram | 49000 | Goa |
205 | Sumit | 40000 | Delhi |
Table: Employee
- Suppose we want to copy only the record of those employees whose Salary is more than 40,000. For this, we have to type the following query in SQL:
-
SELECT * INTO Emp_Salary_40000 FROM Cars WHERE Emp_Salary > 40000;
- Let's check the Emp_Salary_40000 table created successfully or not in the database:
-
SELECT * FROM Emp_Salary_40000;
Emp_Id | Emp_Name | Emp_Salary | Emp_City |
---|---|---|---|
201 | Abhay | 45000 | Goa |
202 | Ankit | 45000 | Delhi |
204 | Ram | 49000 | Goa |
Table: Emp_Salary_40000