How to Create One Table From Another Table in SQL
Last Updated :
13 Jan, 2025
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, data or both. In this article, we will explore various techniques with examples and output in detail.
How to Create One Table From Another Table in SQL?
Creating a new table with only the necessary data can improve performance for specific queries. We will use the below two methods that explain How to Create One Table From Another Table in SQL are defined below:
- Using CREATE TABLE ... AS
- Using INSERT INTO ... SELECT with a Predefined Table
Let's setup an environment:
We will use the below employees table to perform all the methods as shown below:
employees TableMethod 1: Using CREATE TABLE ... AS
The CREATE TABLE ... AS statement allows us to create a new table by copying the structure and data from an existing table.
This method is supported in most relational database systems like PostgreSQL, SQLite, and MySQL (with some variations).
Query:
CREATE TABLE it_employees AS
SELECT employee_id, name, salary
FROM employees
WHERE department = 'IT';
SELECT * FROM it_employees;
Output
outputExplanation:
- The CREATE TABLE ... AS statement creates a new table called it_employees which contains the employee_id, name and salary columns from the employees table.
- It only includes rows where the department is 'IT'.
- The SELECT * FROM it_employees statement then retrieves all the data from the newly created it_employees table.
Method 2: Using INSERT INTO ... SELECT with a Predefined Table
If we need more control over the structure of the new table, we can first create the table with the desired schema and then populate it with data from the existing table using the INSERT INTO ... SELECT statement.
Query:
Suppose we want to create a new table finance_employees with specific columns and data types and then populate it with data from the employees table:
CREATE TABLE finance_employees (
employee_id INT,
name VARCHAR(100),
salary DECIMAL(10, 2)
);
INSERT INTO finance_employees (employee_id, name, salary)
SELECT employee_id, name, salary
FROM employees
WHERE department = 'Finance';
Output:
outputExplanation:
- The CREATE TABLE statement creates a new table called finance_employees with three columns: employee_id (integer), name (string), and salary (decimal).
- The INSERT INTO ... SELECT statement then populates this new table with data from the employees table, selecting only the employees who belong to the 'Finance' department.
- It copies the employee_id, name, and salary values into the finance_employees table.
Conclusion
Overall, Creating one table from another table in SQL is a versatile operation that can be customize to meet various requirements. Whether you need to copy both structure and data, structure only or a subset of data, SQL provides powerful and efficient tools to achieve your goals. By mastering these techniques, you can enhance your database management and data analysis workflows.
Similar Reads
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 Copy Table to Another Table in SQL 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 th
3 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
How to Create a Table With a Foreign Key in SQL? A foreign key is a column or a set of columns in one table that references the primary key of another table. Foreign keys are used to establish and enforce a link between the data in two tables, ensuring referential integrity in the relational database system. In this article, we will explain how to
5 min read
How to Fetch Data From Two Tables Based on Date in SQL? In SQL, fetching data from multiple tables based on a specific date range is a common task. By using certain SQL operations, we can combine data from two different tables and filter the results based on a date condition. This method is particularly useful when working with multiple datasets and we n
3 min read