×

SQL COPY Table

In this tutorial, we will learn how to copy tables in SQL with the help of examples.

Using the SELECT INTO statement in the SQL we can copy one table into the other in the Structured Query Language.

The SELECT INTO Statement query is used to copy the entire table or selected data from the existing table to the newly created table in the SQL.

There are two ways to copy tables in the SQL:

1 First, create the new table and then use the select into the statement to copy data from the existing table.

2 Use the create table aliasing the existing table select statement.

Let's use learn both the ways to copy the table in the SQL with the help of examples.

1. SELECT INTO Statement

In the SELECT INTO statement, we have to create a new table. Then we have to use the SELECT INTO statement to copy the data from the existing table into the newly created table.

The Syntax of the SELECT INTO Statement is as follows:

SELECT * INTO New_Table FROM Old_Table;

Example of SELECT INTO statement in the SQL

Let's create the Tourist table and add 10 records to the tourist table.

Create Table Tourist (Tourist_Id int not null, Tourist_Name varchar(40) not null, Tourist_Country varchar(40) not null,  Gender varchar(1) not null, primary key(Tourist_Id));

Let's add 10 records to the table:

INSERT INTO Tourist Values(101, 'Prakash', 'India', 'M'); 
INSERT INTO Tourist Values(102, 'Amol', 'India', 'M'); 
INSERT INTO Tourist Values(103, 'Genifer', 'Australia', 'F'); 
INSERT INTO Tourist Values(104, 'Meghann', 'Australia', 'F');
INSERT INTO Tourist Values(105, 'Temba', 'South Africa', 'M'); 
INSERT INTO Tourist Values(106, 'Dane', 'South Africa', 'F’); 
INSERT INTO Tourist Values(107, 'Odean', 'West Indies', 'M'); 
INSERT INTO Tourist Values(108, 'Ross', 'NewZealand', 'M'); 
INSERT INTO Tourist Values(109, 'Hayley', 'West Indies', 'F'); 
INSERT INTO Tourist Values(110,'Sophie', 'NewZealand', 'F');    

Table 1: Tourist

Tourist_IdTourist_NameTourist_CountryGender
101PrakashIndiaM
102AmolIndiaM
103GeniferAustraliaF
104MeghannAustraliaF
105TembaSouth AfricaM
106DaneSouth AfricaF
107OdeanWest IndiesM
108RossNewZealandM
109HayleyWest IndiesF
110SophieNewZealandF

Let’s create other table the Tourist_Place table and add 4 records in the tourist table.

Create Table Tourist_Place (Tpid integer(20), History varchar(30),  Kilometer integer(30), State varchar(15), Tpname varchar(30) );

Let’s add 4 records in the table:

INSERT INTO Tourist_Place values(11,'beauty',160,'karnataka','beluru');
INSERT INTO Tourist_Place values(12,'monuments',270,'kerala','kochi');
INSERT INTO Tourist_Place values(13,'beach',360,'tamilnadu','marina');
INSERT INTO Tourist_Place values(14,'history',300,'karnataka','chikmagalur');

Table 2: Tourist_Place

TpidHistoryKilometerStateTpname
11Beauty160KarnatakaBeluru
12Monuments270KeralaKochi
13Beach360TamilNaduMarina
14History300KarnatakaChikmagalur

Example 1: Write a query to copy the Tourist table data into the new table Tourists.

SELECT * INTO Tourists FROM Tourist;

In the above example, we copied the entire tourist table into the new table Tourists.

We will execute the select query on the Tourists table to check whether the table is successfully created and data is copied into the table or not.

SELECT * FROM Tourists;
Tourist_IdTourist_NameTourist_CountryGender
101PrakashIndiaM
102AmolIndiaM
103GeniferAustraliaF
104MeghannAustraliaF
105TembaSouth AfricaM
106DaneSouth AfricaF
107OdeanWest IndiesM
108RossNewZealandM
109HayleyWest IndiesF
110SophieNewZealandF
SQL COPY TABLE

Example 2: Write a query to copy the Tourist_Place table data into the new table Tourists_Place.

SELECT * INTO Tourists_Place FROM Tourist_Place;

In the above example, we copied the entire tourists_place table into the new table Tourists_Place.

We will execute the select query on the Tourists_Place table to check whether the table is successfully created and data is copied into the table or not.

SELECT * FROM Tourists_Place;
TpidHistoryKilometerStateTpname
11Beauty160KarnatakaBeluru
12Monuments270KeralaKochi
13Beach360TamilNaduMarina
14History300KarnatakaChikmagalur
SQL COPY TABLE

2. CREATE A TABLE using AS

We will use the same table we used in the above examples.

Example 1: Write a query to copy tourists' table data into the newly created table Tour using the below query.

CREATE TABLE Tour AS SELECT * FROM Tourists;

In the above query, we copied the tourist's table record in the Tour table.

We will execute the select query on the Tour table to check whether the table is successfully created and data is copied into the table or not.

SELECT * FROM Tour;
Tourist_IdTourist_NameTourist_CountryGender
101PrakashIndiaM
102AmolIndiaM
103GeniferAustraliaF
104MeghannAustraliaF
105TembaSouth AfricaM
106DaneSouth AfricaF
107OdeanWest IndiesM
108RossNewZealandM
109HayleyWest IndiesF
110SophieNewZealandF
SQL COPY TABLE

Example 2: Write a query to copy the Tourist_Places table data into the new table TP.

CREATE TABLE TP AS SELECT * FROM Tourists_Place;

In the above example, we copied the entire tourists_places table into the new table TP.

We will execute the select query on the TP table to check whether the table is successfully created and data is copied into the table or not.

SELECT * FROM TP;
TpidHistoryKilometerStateTpname
11Beauty160KarnatakaBeluru
12Monuments270KeralaKochi
13Beach360TamilNaduMarina
14History300KarnatakaChikmagalur
SQL COPY TABLE

Related Topics

SQL CASE

This page contains all the information about SQL CASE. The CASE is an If-Else type of logical query used in the statement. The CASE in Structured Query Language is similar...

5 minutes read.

SQL SELECT Database

In this tutorial, we will help you to understand and learn how to select the database in SQL with the help of examples. The database user or the administrator wants to...

3 minutes read.

SQL CROSS Join

In this tutorial, we will help you understand the concept of the SQL CROSS Join clause with the few examples. The SQL CROSS Join query is used to join one or...

5 minutes read.

Types of SQL JOIN

The SQL JOIN combines one or more than one tables based on their relationship. The SQL JOIN involves a parent table and a child table relationship. There are different types of...

10 minutes read.

Grant Command in SQL

What is DCL (Data Control Language)? Data Control Language (DCL) is a computer programming language with syntax intended to manage access to data kept in databases. It is a part of...

3 minutes read.

SQL Insert multiple rows

This article will deal with a new insertion method in SQL which inserts multiple rows at a time. Multiple records can be inserted at a time into a specific table with...

4 minutes read.

Nth highest salary

The most common and important question asked in interviews that how we can find the Nth highest salary in a table (2nd highest salary, 3rd highest salary, or Nth highest...

6 minutes read.

How to Update Table in SQL?

How to Update Table in SQL Introduction UPDATE query is used to update a record in a table. UPDATE is a DML command, which operates on the data of the table and not...

4 minutes read.

SQL ORDER BY

SQL ORDER BY The SQL ORDER BY clause is used to sort the data stored in tables in the database. The sorting can be done in an ascending way, descending way,...

5 minutes read.

SQL Wildcards

SQL WILDCARD CHARACTERS: SQL wild card character is used to substitute zero or more characters of a string. These characters are used with the “LIKE” operator. Wild Card Characters in SQL: %_[]^- “%” : It is...

3 minutes read.

SQL CRUD Operation

In any computer language, CRUD operation is a foundation. CRUD operation rules are applied in databases as well. These operations are the basic operations used to perform with any database...

7 minutes read.

DML Commands in SQL

DML is an abbreviation of Data Manipulation Language. Data Manipulation Language commands in Structured Query Language manipulate the data in the database. DML commands are used to retrieve records, add records,...

4 minutes read.

DELETE VS DROP in SQL

This page contains all the information about the Delete command and Drop command concept and the difference between the DELETE and DROP commands in SQL. What is the DELETE command in...

3 minutes read.

SQL INTERSECT

SQL intersect operator is used to combine two or more SELECT statements, but it only displays the data similar to the SELECT statement. The syntax for the INTERSECT operation: SELECT COLUMN_NAME1, COLUMN_NAME2,...

3 minutes read.

How to use INNER JOIN in SQL

In this article, we will learn about the INNER JOIN concept and how to use it in SQL with the WHERE clause. What is INNER JOIN in SQL? Inner Join is a...

6 minutes read.

SQL DELETE

In this tutorial, you will learn about the SQL DELETE concept by using examples. In Structured Query Language (SQL), we fetch the data from the database table using SELECT Statement and...

3 minutes read.

SQL WHERE Multiple Conditions

In this topic, we will learn how to add multiple conditions using the WHERE clause. First, let's understand the concept of WHERE clause. WHERE clause is used to specify a condition while...

5 minutes read.

SQL Syntax

In this tutorial, we will help you understand about the different SQL Syntax concepts with the help of an example. Every Structured Query Language starts with Keywords like select, insert, update,...

5 minutes read.

SQL LOGICAL Operator

In this tutorial, we will understand the operator who falls under the logical operator in SQL with the help of examples. The SQL Logical Operator displays the query result in one...

5 minutes read.

SQL Cloning Tables

Cloning a Table: To create a copy of the table. To perform the operations, without affecting the actual table. Steps for creating a Cloning Table: Step 1: Empty Table Creation The syntax for Creating...

3 minutes read.