SQL COPY TABLE

SQL COPY TABLE: How to Copy Data from One Table to Another

SQL COPY TABLE: How to Copy Data from One Table to Another

Interested in above project ,Click Below
WhatsApp
Telegram
LinkedIn

SQL COPY TABLE

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_ModelVehicle_ColorVehicle_Price
Honda CivicBlue12,50,000
Toyota CorollaWhite14,00,000
Ford MustangRed25,00,000
Tesla Model 3Black35,00,000

SQL Query to Copy Data

SELECT * INTO Vehicle_Details FROM Vehicles;  

Resulting Table: Vehicle_Details

Vehicle_ModelVehicle_ColorVehicle_Price
Honda CivicBlue12,50,000
Toyota CorollaWhite14,00,000
Ford MustangRed25,00,000
Tesla Model 3Black35,00,000

Example 2: Copying Specific Columns

Original Table: Gadgets

Gadget_NameGadget_BrandPriceLaunch_Year
iPhone 14Apple80,0002022
Galaxy S22Samsung70,0002022
OnePlus 10 ProOnePlus60,0002022
Pixel 6Google65,0002022

SQL Query to Copy Selected Columns

SELECT Gadget_Name, Price INTO Gadget_Pricing FROM Gadgets;  

Resulting Table: Gadget_Pricing

Gadget_NamePrice
iPhone 1480,000
Galaxy S2270,000
OnePlus 10 Pro60,000
Pixel 665,000

Example 3: Using WHERE Clause to Copy Data

Original Table: Books

Book_IDBook_TitleGenrePrice
101The AlchemistFiction400
102Rich Dad Poor DadSelf-help500
103The Lean StartupBusiness600
104Atomic HabitsSelf-help700

SQL Query to Copy Specific Rows

SELECT * INTO Self_Help_Books FROM Books WHERE Genre = 'Self-help';  

Resulting Table: Self_Help_Books

Book_IDBook_TitleGenrePrice
102Rich Dad Poor DadSelf-help500
104Atomic HabitsSelf-help700

Example 4: Adding a Condition to a New Table

Original Table: Laptops

Laptop_BrandProcessorRAMPrice
Delli716GB80,000
HPi58GB50,000
LenovoRyzen 516GB70,000
AppleM18GB1,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_BrandProcessorRAMPrice
Delli716GB80,000
AppleM18GB1,00,000

Syntax with WHERE Clause

You can also add conditions using the WHERE clause to filter the data being copied:

See also  SQL SELECT RANDOM: Fetching Random Rows from a Database

SELECT * INTO New_Table_Name FROM Old_Table_Name WHERE [condition];  

Download New Real Time Projects :-Click here

Conclusion

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

🎓 Need Complete Final Year Project?

Get Source Code + Report + PPT + Viva Questions (Instant Access)

🛒 Visit UpdateGadh Store →
💬 Chat Now