How to Create One Table From Another Table in SQL

Are you looking for a way to create one table from another table in SQL? If so, you've come to the right place! In this post, we'll discuss how to use the CREATE TABLE statement to create a new table from an existing table in SQL.

The Solution

The solution is to use the CREATE TABLE statement. This statement allows you to create a new table from an existing table. The syntax for this statement is as follows:

CREATE TABLE new_table_name AS SELECT * FROM existing_table_name;

This statement will create a new table with the same columns and data as the existing table. The new table will be named new_table_name.

Examples

Let's look at a few examples of how this statement can be used. Suppose you have a table called users with the following data:

users
nameage
John25
Jane30
John25
Bob20

If you wanted to create a new table called users_copy with the same data as the users table, you would use the following query:

CREATE TABLE users_copy AS SELECT * FROM users;

This query would create a new table called users_copy with the same columns and data as the users table.

Additional Info

The CREATE TABLE statement is supported by most major databases, including MySQL, PostgreSQL, and SQL Server. However, the syntax may vary slightly depending on the database you are using. For more information, check out the documentation for your particular database. 🤓

Want to build your own LLM Apps with AirOps👇👇