Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a table + populate it using another table in One Query

Tags:

sql

I'm using Access 2000 and I have 2 queries:

The first one is:

CREATE TABLE first_table_name (....)

The second is:

INSERT INTO first_table_name [(column1, column2, ... columnN)] 
   SELECT column1, column2, ...columnN 
   FROM second_table_name
   [WHERE condition];

Is it possible to do the same thing (create a table and immediately fill it using another table) with just one query?

Thank you !

like image 727
ponponke Avatar asked May 02 '13 13:05

ponponke


People also ask

How do I populate one table from another in SQL?

The SQL INSERT INTO SELECT Statement The INSERT INTO SELECT statement copies data from one table and inserts it into another table. The INSERT INTO SELECT statement requires that the data types in source and target tables match. Note: The existing records in the target table are unaffected.

How can you create a new table with existing data from another table?

A copy of an existing table can be created using a combination of the CREATE TABLE statement and the SELECT statement. The new table has the same column definitions. All columns or specific columns can be selected.

How do you create a table and copy data from another table in SQL?

Answer: To do this, the SQL CREATE TABLE syntax is: CREATE TABLE new_table AS (SELECT * FROM old_table WHERE 1=2); For example: CREATE TABLE suppliers AS (SELECT * FROM companies WHERE 1=2);

How can I put two table data in one query?

In SQL, to fetch data from multiple tables, the join operator is used. The join operator adds or removes rows in the virtual table that is used by SQL server to process data before the other steps of the query consume the data.


1 Answers

try

SELECT column1, column2, ...columnN 
   into first_table_name 
   FROM second_table_name
   [WHERE condition];

first_table_name will have same table column as second table name

like image 185
Amit Rai Sharma Avatar answered Oct 14 '22 22:10

Amit Rai Sharma