Open In App
Related Articles

SQL | Create Table Extension

Improve Article
Improve
Save Article
Save
Like Article
Like

SQL provides an extension for CREATE TABLE clause that creates a new table with the same schema of some existing table in the database.

  • It is used to store the result of complex queries temporarily in a new table.
  • The new table created has the same schema as the referencing table.
  • By default, the new table has the same column names and the data type of the referencing table.

Syntax:

CREATE TABLE newTable LIKE pets

Example:

CREATE TABLE newTable as
            (SELECT * 
             FROM pets
             WHERE pets.BREED = 'German Shepherd')

Queries

pets table:

IDNameBreedGender
11441TommyGerman ShepherdMale
11442MaxBeagleMale
11443CharliePugMale
11444DaisyPoodleFemale
11445ZoeLabradorFemale
11446TobyBulldogMale


Query 1:

CREATE TABLE newTable LIKE pets;
SELECT * 
FROM newTable 
where newTable.GENDER = 'Female';

Output:

IDNameBreedGender
11444DaisyPoodleFemale
11445ZoeLabradorFemale

Explanation: The newTable created is a copy of pets table. So, selecting female pets from newTable returns only two rows in which the pet is a female.
Query 2 :

CREATE TABLE newTable as
            (SELECT * 
             FROM pets
             WHERE pets.BREED = 'German Shepherd');
SELECT * from newTable;

Output:

IDNameBreedGender
11441TommyGerman ShepherdMale

Explanation: First the inner query is evaluated and the results are stored in a new temporary relation. Following this, the outer query is evaluated which create newTable as add the output of inner query to newTable.

References : Database System Concepts 6th Edition by Silberschatz

This article is contributed by Mayank Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Last Updated : 21 Mar, 2018
Like Article
Save Article
Similar Reads