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

SELECT * INTO New_table_name FROM old_table_name;  

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 NameCar ColorCar Cost
Hyundai CretaWhite10,85,000
Hyundai VenueWhite9,50,000
Hyundai i20Red9,00,000
Kia SonetWhite10,00,000
Kia SeltosBlack8,00,000
Swift DezireRed7,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 NameCar ColorCar Cost
Hyundai CretaWhite10,85,000
Hyundai VenueWhite9,50,000
Hyundai i20Red9,00,000
Kia SonetWhite10,00,000
Kia SeltosBlack8,00,000
Swift DezireRed7,95,000

Table: Car_Details

Example 2: In this example, we have a table called Employee with four columns:

Emp_IdEmp_NameEmp_SalaryEmp_City
201Abhay25000Goa
202Ankit45000Delhi
203Bheem30000Goa
204Ram29000Goa
205Sumit40000Delhi
  • 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_IdEmp_NameEmp_SalaryEmp_City
201Abhay25000Goa
202Ankit45000Delhi
203Bheem30000Goa
204Ram29000Goa
205Sumit40000Delhi

Table: Coding_Employees

Example 3: In this example, we have a table called Student with four columns:

RollNoNameMarksAge
1001Bhanu8817
1002Raman8216
1003Sumit8016
1004Shobhit9515
1005Akash8516

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;  
RollNoNameMarksAge
1001Bhanu8817
1002Raman8216
1003Sumit8016
1004Shobhit9515
1005Akash8516

Table: Class_12_Students

Example 4: In this example, we have a table called Cars with three columns:

Car NameCar ColorCar Cost
Hyundai CretaWhite10,85,000
Hyundai VenueWhite9,50,000
Hyundai i20Red9,00,000
Kia SonetWhite10,00,000
Kia SeltosBlack8,00,000
Swift DezireRed7,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 NameCar Color
Hyundai CretaWhite
Hyundai VenueWhite
Hyundai i20Red
Kia SonetWhite
Kia SeltosBlack
Swift DezireRed

Table: Car_Color

Syntax of SELECT INTO statement with WHERE clause in SQL

SELECT * INTO New_table_name FROM old_table_name WHERE [ condition ] ;  

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 NameCar ColorCar Cost
Hyundai CretaBlack10,85,000
Hyundai VenueBlack9,50,000
Hyundai i20Red9,00,000
Kia SonetWhite10,00,000
Kia SeltosBlack8,00,000
Swift DezireRed7,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 NameCar ColorCar Cost
Hyundai CretaBlack10,85,000
Hyundai VenueBlack9,50,000
Kia SeltosBlack8,00,000

Table: Black_Car_Details

Example 2: In this example, we have a table called Employee with four columns:

Emp_IdEmp_NameEmp_SalaryEmp_City
201Abhay45000Goa
202Ankit45000Delhi
203Bheem38000Goa
204Ram49000Goa
205Sumit40000Delhi

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_IdEmp_NameEmp_SalaryEmp_City
201Abhay45000Goa
202Ankit45000Delhi
204Ram49000Goa

Table: Emp_Salary_40000


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *