Open In App

How to Copy Rows from One Table to Another in SQL?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we learn How to copy rows from one table to another table in SQL. For better understanding, we will Implement this Query with the Help of an Example. This query is Very Help Whenever we need two or more same Column in a Different Table. That time we do need to insert the record manually in the table with the help of another table We can directly Copy the record from another table. First of all, We will Create a Database name as a Sample after that we will create two tables inside of the Database Sample. The first Table Name is EMPOLYEET and the Second one is the ATTENDANCE Table.

We will use here two Statements 

  • INSERT STATEMENT
  • SELECT STATEMENT

We will follow the below steps to Implement How to Copy rows from one table to another table in SQL:

Step 1: Create A Database 

For database creation, there is the query we will use in the SQL Platform. this is the query.

Syntax:

Create database database_name; 

Query:

CREATE DATABASE Sample; // query will create a database in SQL Platform 

Step 2: Use Database  

For using the database we will use another query in SQL Platform like Mysql, oracle, etc. 

Query:

use Sample;  

Step 3: Creation of table

For the creation Data table, we will use this below 

Query:

create table table_name(
column1 type(size),
column2 type(size),
.
columnN type(size)
); 

This will create a new table in the existing Database.

Query:

CREATE TABLE EMPLOYEE
(
EMPNAME VARCHAR(25),
GENDER VARCHAR(6),
DEPT VARCHAR(20),
CONTACTNO BIGINT NOT NULL,
CITY VARCHAR(15)
); 

and now we will Create another Table name ATTENDANCE

Query:

CREATE TABLE ATTENDANCE
( EMPNAME VARCHAR(25),
GENDER VARCHAR(6),
DEPT VARCHAR(20),
ATTENDATE DATE DEFAULT GETDATE()
); 

Step 4: Insert Data Into Table EMPOLYEET 

Query:

INSERT INTO EMPLOYEET
VALUES ('VISHAL','MALE','SALES',9193458625,'GHAZIABAD'),
('DIVYA','FEMALE','MANAGER',7352158944,'BAREILLY'),
('REKHA','FEMALE','IT',7830246946,'KOLKATA'),
('RAHUL','MALE','MARKETING',7906334516,'MEERUT'),
('SANJAY','MALE','SALES',9149335694,'MORADABAD'),
('RAJKUMAR','MALE','MANAGER',9675274391,'BENGALURU'),
('RAJSHREE','FEMALE','SALES',9193458625,'VODODARA'),
('HAIM','MALE','IT',7088573213,'SAMBHAL'),
('RAKESH','MALE','MARKETING',9645956441,'BOKARO'),
('MOHINI','FEMALE','SALES',9147844694,'Delhi'); 

Step 5: VERIFY OR VIEW INSERTED DATA IN EMPLOYEET TABLE 

After inserting data into the table We can justify or confirm which data we have to insert correctly or not. With the help of the Below Query.

Query:

 SELECT * FROM EMPLOYEET; 

Output:

Step 6: Insert Data Into Table ATTENDANCE

In this table we will not Insert record Manually because the same data exist in another table EMPLOYEET so, therefore, we will copy data from EMPLOYEET table to ATTENDANCE Table. With the below of below query

Query:

INSERT INTO ATTENDANCE (EMPNAME,GENDER,DEPT)
SELECT EMPNAME,GENDER,DEPT FROM EMPLOYEET; 

After Inserting data in the table We can justify or confirm which data we have copied record from EMPLOYEET have inserted correctly or not. With the help of the Below Query.

Query:

SELECT * FROM ATTENDANCE;

Now We can see in below Snapshots record Copied Correctly 

Output:


Last Updated : 25 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads