When working with databases, you may need to duplicate data from one table into another for analysis, backups, or temporary operations. This can be easily achieved using the SELECT INTO statement in SQL.
The SELECT INTO statement allows you to copy the data and structure of an existing table into a new table within the same database.
Complete Advance AI topics:-ย CLICK HERE Complete Python Course with Advance topics:-Click here
Syntax of SELECT INTO Statement
SELECT * INTO New_Table_Name FROM Old_Table_Name;
SELECT INTO Statement Examples in SQL
To illustrate how to copy data from one table to another, consider the following examples:
Example 1: Copying All Data
Original Table: Vehicles
Vehicle_Model
Vehicle_Color
Vehicle_Price
Honda Civic
Blue
12,50,000
Toyota Corolla
White
14,00,000
Ford Mustang
Red
25,00,000
Tesla Model 3
Black
35,00,000
SQL Query to Copy Data
SELECT * INTO Vehicle_Details FROM Vehicles;
Resulting Table: Vehicle_Details
Vehicle_Model
Vehicle_Color
Vehicle_Price
Honda Civic
Blue
12,50,000
Toyota Corolla
White
14,00,000
Ford Mustang
Red
25,00,000
Tesla Model 3
Black
35,00,000
Example 2: Copying Specific Columns
Original Table: Gadgets
Gadget_Name
Gadget_Brand
Price
Launch_Year
iPhone 14
Apple
80,000
2022
Galaxy S22
Samsung
70,000
2022
OnePlus 10 Pro
OnePlus
60,000
2022
Pixel 6
Google
65,000
2022
SQL Query to Copy Selected Columns
SELECT Gadget_Name, Price INTO Gadget_Pricing FROM Gadgets;
Resulting Table: Gadget_Pricing
Gadget_Name
Price
iPhone 14
80,000
Galaxy S22
70,000
OnePlus 10 Pro
60,000
Pixel 6
65,000
Example 3: Using WHERE Clause to Copy Data
Original Table: Books
Book_ID
Book_Title
Genre
Price
101
The Alchemist
Fiction
400
102
Rich Dad Poor Dad
Self-help
500
103
The Lean Startup
Business
600
104
Atomic Habits
Self-help
700
SQL Query to Copy Specific Rows
SELECT * INTO Self_Help_Books FROM Books WHERE Genre = 'Self-help';
Resulting Table: Self_Help_Books
Book_ID
Book_Title
Genre
Price
102
Rich Dad Poor Dad
Self-help
500
104
Atomic Habits
Self-help
700
Example 4: Adding a Condition to a New Table
Original Table: Laptops
Laptop_Brand
Processor
RAM
Price
Dell
i7
16GB
80,000
HP
i5
8GB
50,000
Lenovo
Ryzen 5
16GB
70,000
Apple
M1
8GB
1,00,000
SQL Query to Copy Data with a Price Filter
SELECT * INTO High_End_Laptops FROM Laptops WHERE Price > 75,000;
Resulting Table: High_End_Laptops
Laptop_Brand
Processor
RAM
Price
Dell
i7
16GB
80,000
Apple
M1
8GB
1,00,000
Syntax with WHERE Clause
You can also add conditions using the WHERE clause to filter the data being copied:
SELECT * INTO New_Table_Name FROM Old_Table_Name WHERE [condition];
The SELECT INTO statement in SQL provides a seamless way to duplicate data into new tables. Whether copying all records or only specific columns/rows, this method is efficient and easy to use. Use it thoughtfully to manage your data effectively.
For more SQL tips and tricks, visit UpdateGadh, your go-to destination for professional tech insights!
sql copy table to another table how to create duplicate table in sql with data sql copy table from one database to another mysql copy table sql select into existing table how to copy table structure in sql without data postgresql copy table create table in sql sql create table sql joins sql copy table w3schools sql copy table example sql copy table oracle
Leave a Reply