Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
Copy a Table from One Schema to Another in SQL?
Next article icon

How to Copy Table to Another Table in SQL

Last Updated : 09 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Copying data from one table to another is a common task in SQL whether we are migrating data by creating backups or simply duplicating a table's structure and content for further use.

In this article, we'll explore several ways to copy a table to another table in SQL Server including copying both the structure and data, just the data and how to handle specific conditions.

Ways to Copy a Table to another Table in SQL

Let's learn various ways to copy a table in SQL and the method we choose depends on our requirements such as whether we need to copy both the structure and the data or just the data are defined below:

Method 1: Copying Both Structure and Data

To copy both the structure (columns and data types) and the data we can use the SELECT INTO statement. This will create a new table and copy all data from the source table.

Example: Suppose we have an Employees table and we want to copy the Employees table to Employees_Copy Table:

SELECT * INTO Employees_Copy
FROM Employees;

Output:

method1
output

Explanation: This query creates a new table called Employees_Copy that mirrors the structure and data of the existing Employees table. The new table will have the same columns, data types, and data.

Method 2: Copying Data into an Existing Table

If we want to copy data into an existing table, we can use the INSERT INTO statement. This works when the destination table already exists.

Code Example:

INSERT INTO Employees_Copy
SELECT * FROM Employees;

Output (After Execution):

method2
output

Explanation: This query copies all rows from the Employees table into the already existing Employees_Copy table. The columns of both tables should match in number and type.

Method 3: Copying Data with Conditions (WHERE Clause)

We can copy only specific rows using a WHERE clause in the SELECT statement. This helps you filter the rows to copy based on conditions.

Code Example:

INSERT INTO Employees_Copy
SELECT * FROM Employees
WHERE Age > 40;

Output:

method3
output


Explanation: This query copies only the rows where Age is greater than 40 from the Employees table into the Employees_Copy table. For example, if Bob (Age 45) is the only person who matches the condition, only Bob will be copied.

Method 4: Copying Specific Columns

If we don't want to copy all columns, you can specify which columns to copy. This is useful when we want only certain data from the source table.

Code Example:

INSERT INTO Employees_Copy (EmployeeID, FirstName, LastName)
SELECT EmployeeID, FirstName, LastName
FROM Employees;

Output:

method4
output

Explanation: In this example, only the EmployeeID, FirstName, and LastName columns are copied from the Employees table into the Employees_Copy table. The Age column is excluded.

Method 5: Copying Table Structure Without Data

To copy only the structure of a table (without any data), you can use a WHERE clause that evaluates to false, ensuring that no rows are copied.

Code Example:

SELECT * INTO Employees_Copy
FROM Employees
WHERE 1 = 0;

Output:

method5
output

Explanation:

  • The condition WHERE 1 = 0 is always false, so no data is copied. However, the structure of the Employees table is copied, including columns and data types, to Employees_Copy.
  • The new Employees_Copy table will have the same structure as Employees but without any data.

Conclusion

In SQL Server, there are multiple ways to copy a table depending on the task at hand. The `SELECT INTO` statement can be used to copy both the structure and data of a table, while the `INSERT INTO` statement allows copying data into an existing table. For more specific copying, conditions can be applied using the `WHERE` clause by enabling the selection of particular rows or columns to copy. Additionally, copying only the table structure (without data) is possible by using a condition that always evaluates to false. These methods provide flexibility for data migration, backup or table duplication.


Next Article
Copy a Table from One Schema to Another in SQL?

S

satyamguptboxl
Improve
Article Tags :
  • SQL
  • Databases

Similar Reads

  • How to Create One Table From Another Table in SQL
    Creating a table based on the structure and data of an existing table is a common task in database management. This process allows us to replicate a table for backup, testing or data transformation purposes. SQL provides efficient methods to create one table from another while preserving the schema,
    3 min read
  • How to Copy Rows from One Table to Another in SQL?
    In SQL, copying rows from one table to another is a common operation that simplifies data migration and duplication tasks. Whether creating backup tables, transferring data for analysis, or setting up a new schema, the ability to efficiently replicate data across tables is invaluable. This operation
    3 min read
  • How to Update a Table Data From Another Table in SQLite
    SQLite is an embedded database that doesn't use a database like Oracle in the background to operate. The SQLite offers some features which are that it is a serverless architecture, quick, self-contained, reliable, full-featured SQL database engine. SQLite does not require any server to perform queri
    3 min read
  • How to Copy Data From One Column to Another in the Same Table in SQL?
    Efficiency in data manipulation is crucial while using Structured Query Language (SQL). To manage a wide range of tasks, including organizing, retrieving, updating, and deleting data, SQL provides a comprehensive set of instructions. Among these, copying data between columns in the same table is a c
    4 min read
  • Copy a Table from One Schema to Another in SQL?
    When working with MySQL, there are several scenarios where we may need to copy tables (including their structure and data) from one schema (database) to another. This is a common task during database migrations, backups, or when testing with different environments. In this article, we will go throug
    5 min read
  • SQL Query to Filter a Table using Another Table
    In this article, we will see, how to filter a table using another table. We can perform the function by using a subquery in place of the condition in WHERE Clause. A query inside another query is called subquery. It can also be called a nested query. One SQL code can have one or more than one nested
    2 min read
  • How to Append Two Tables and Put the Result in a Table in SQL?
    SQL provides the UNION and UNION ALL operators to combine data from two tables into a new table. These operators are used to merge rows from multiple tables into a single result set. The UNION operator removes duplicate rows from the combined result, while UNION ALL includes all rows, including dupl
    4 min read
  • How to Get the Names of the Table in SQL
    Retrieving table names in SQL is a common task that aids in effective database management and exploration. Whether we are dealing with a single database or multiple databases, knowing how to retrieve table names helps streamline operations. SQL provides the INFORMATION_SCHEMA.TABLES view, which offe
    3 min read
  • How to Export a Table Data to a PDF File in SQL?
    SQL Server is a versatile database. It is used across many industries. We can use AZURE data studio as well as SQL Server Management Studio for doing various operations (DDL, DML, Stored Procedure, Trigger preparations) etc., SQL Server supports the portability of data using the EXPORT option. By de
    2 min read
  • How to list the Tables in a SQLite Database File ?
    SQLite is a database engine which is written in C programming language. SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. SQLite is the most widely deployed SQL database engine in the world. The source code for SQLite is
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences