×

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 Aggregate Operators

In SQL, the aggregate operators perform a calculation on multiple rows or multiple values of a single column and give the output a single value. Here, multiple rows of data are...

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.

SET Operators in SQL

The operator used to join or combine two queries is none other than SET operators. Operators categorized into SET operators are as follows: UNION Operator.UNION ALL’ Operator.INTERSECT Operator.MINUS Operator. Rules to be...

8 minutes read.

SQL Scalar Functions

TEACHER Table LCASE() Function The LCASE() function is used to return lower case values of specified column. Syntax: Select Lcase(column_name) from table_name; Example: Select Lcase(teacher_name) from teacher; Syntax: Select Lcase(column_name) from table_name [where condition]; Example: Select Lcase(teacher_name) from teacher where teacher_id=1; UCASE() Function The UCASE()...

1 minute read.

DDL Commands in SQL

DDL stands for Data Definition Language. Data Definition Language is a bunch of SQL commands used to create, modify and delete Database schema but not the records or data. Data Definition...

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.

Update Query in SQL

Update is an SQL command that is used to modify the data that in already present in database. Update is a command of DML. DML means Data Manipulation Language. Basically,...

3 minutes read.

TCL Commands in SQL

In Structured Query Language, TCL is an abbreviation for Transaction Control Language. A single unit of work in a database is formed after the consecutive execution of commands is known...

6 minutes read.

Difference between SQL and NoSQL

SQL vs. NoSQL | Difference between SQL and NoSQL Choosing a database is the most fundamental decision that needs to be decided before starting a task. Relational and non-relational databases are...

3 minutes read.

Pattern Matching in SQL

The LIKE in the Structured Query Language is a Logical Operator. The SQL LIKE is used within the WHERE clause in the SQL. This Like Operator is used with the...

5 minutes read.

SQL Queries

In a database, queries are used to request the result set of data from the table or action on the records. A Query can answer your simple or complicated question, perform...

5 minutes read.

SQL GROUP BY CLAUSE

The GROUP BY clause is used to arrange similar records into the groups in the Structured Query Language queries. The records are arranged with the help of functions which is...

4 minutes read.

SQL Formatter

As a beginner, one does not focus much on Formatting the queries while writing their code. This leads to a great confusion while referring the code in future. For a...

5 minutes read.

SQL Data Definition Language

Data definition language directly effects on the structure/schema of the database. CREATE, ALTER, DROP are the commands of DDL.CREATE: Creates new database, table, or view of table.ALTER: Modifies the database or...

3 minutes read.

SQL Data Types

In SQL DATA TYPES, each column holds value. Data types can be an integer type, character type, etc., and such data is stored in the database table. The data type is...

4 minutes read.

SQL SELECT AND Operator

This SQL tutorial explains and helps us understand how to use the AND Operator in the SELECT query with examples. The AND Operator is used to fetch the table’s records if...

2 minutes read.

SQL Commands

SQL commands are classified into four groups on the basis of their nature. DDL (Data Definition Language) DML (Data Manipulation Language) DCL (Data Control Language) DQL (Data Query Language) NOTE: This...

1 minute read.

How to Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

2 minutes read.

SQL DROP Database

This tutorial will help us understand how to drop a database from the SQL Server with a few examples. The DROP command removes all the objects stored in the database and...

4 minutes read.

How to use COUNT in SQL?

How to use COUNT in SQL Introduction COUNT( ) is an aggregate function in SQL.This function counts the number of records in a table if the condition is not specified.If the condition...

4 minutes read.