SQL Tutorial

SQL Tutorial SQL Introduction SQL Syntax SQL Data Types SQL OPERATORS SQL COMMANDS SQL Queries

SQL Database

SQL Create Database SQL DROP Database SQL SELECT Database

SQL Table

SQL TABLE SQL CREATE TABLE SQL COPY TABLE SQL ALTER TABLE SQL DELETE SQL TRUNCATE TABLE SQL DROP TABLE SQL UPDATE TABLE SQL INSERT TABLE

SQL SELECT

SQL SELECT Statement SQL SELECT WHERE Clause SQL SELECT IN Operator SQL BETWEEN Operator SQL SELECT BETWEEN Operator SQL SELECT AND Operator SQL SELECT OR Operator SQL SELECT LIKE Operator SQL SELECT DISTINCT SQL SELECT SUM SQL SELECT MAX SQL SELECT MIN SQL SELECT AVG

SQL Clause

SQL WHERE Clause SQL GROUP BY CLAUSE SQL ORDER BY Clause SQL HAVING Clause

SQL INSERT

SQL INSERT Statement SQL INSERT INTO Statement SQL INSERT INTO Values SQL INSERT INTO SELECT SQL Insert multiple rows

SQL JOIN

SQL JOIN SQL Inner Join SQL Left Join SQL Right Join SQL Full Join SQL CROSS Join

SQL OPERATOR

SQL Comparison SQL LOGICAL Operator SQL Cast Operator SQL Arithmetic

Difference

SQL vs NOSQL WHERE vs HAVING DELETE vs DROP GROUP BY vs ORDER BY DROP vs TRUNCATE SQL IN vs SQL EXISTS Difference between Delete, Drop and Truncate in SQL

MISC

SQL SubQuery SQL CASE Commit and Rollback in SQL Pattern Matching in SQL DDL Commands in SQL DML Commands in SQL Types of SQL Commands SQL COUNT SQL Primary Key SQL FOREIGN KEY SET Operators in SQL Check Constraint in SQL SQL EXCEPT SQL VIEW SQL WHERE Statement SQL CRUD Operation Where Condition in SQL TCL Commands in SQL Types of SQL JOINS SQL Nth Highest Salary SQL NOT OPERATOR SQL UNION ALL SQL INTERSECT SQL Data Definition Language SQL Data Manipulation Language SQL Data Control Language SQL CONSTRAINTS SQL Aggregate Operators SQL KEYS Codd’s Rules in SQL What is SQL Injection? Trigger In SQL SQL WHERE Multiple Conditions Truncate function in SQL SQL Formatter WEB SQL SQL Auto Increment Save Point in SQL space() function in SQL SQL Aggregate Functions SQL Topological Sorting SQL Injection SQL Cloning Tables SQL Aliases SQL Handling Duplicate Update Query in SQL Grant Command in SQL SQL SET Keyword SQL Order BY LIMIT SQL Order BY RANDOM

How To

How to use the BETWEEN operator in SQL How To Use INNER JOIN In SQL How to use LIKE in SQL How to use HAVING Clause in SQL How to use GROUP BY Clause in SQL How To Remove Duplicates In SQL How To Delete A Row In SQL How to add column in table in SQL ? How to drop a column in SQL? How to create a database in SQL? How to use COUNT in SQL? How to Create Temporary Table in SQL? How to Add Foreign Key in SQL? How to Add Comments in SQL? How To Use Group By Clause In SQL How To Use Having Clause In SQL How To Delete Column In Table How To Compare Date In SQL How index works in SQL How to calculate age from Date of Birth in SQL How to Rename Column name in SQL What are single row and multiple row subqueries?

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 an empty table:

Create table new_table_name Like Old_table;

Example

Code:

Create table Student1 Like Student;

Output:

Table Created

Step 2: Inserting the data into a table

Insert into new_table_name select * from old_table

Example

Code:

Create table Student1 Like Student;

Output:

Table Created

2. Inserting the values of data into a new table i.e, Student1

Code:

Insert into Student1 select * from Student;

Output:

Rows are inserted

Display the records of the new table i.e, Student1

Code:

Select * from Student1;

Output:

Student1 Table

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
4Nethranand21Male7675643423
5Naveen23Male6567784532
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839
9Hashish22Male9453215052
10Rahul21Male9998989898

Display the id’s of all students which are copied into Student1 Table

Code:

Select sid from Student1;

Output:

Student1 Table

sid
1
2
3
4
5
6
7
8
9
10

Display the names  of all students which are copied into Student1 Table

Code:

Select sname from Student1;

Output:

Student1 Table

sname
Abhinav
Ramya
Preetham
Nethranand
Naveen
Harshita
Bindu
Nandhini
Hashish
Rahul

Display the age  of all students which are copied into Student1 Table

Code:

Select sage from Student1;

Output:

Student1 Table

sage
22
24
21
21
23
22
26
23
22
21

Display the Phonenumber  of all students which are copied into Student1 Table

Code:

Select Phonenumber from Student1;

Output:

Student1 Table

Phonenumber
9895678909
6687654634
9867546453
7675643423
6567784532
9867546231
6563412768
6785674839
9453215052
9998989898

Display the id and names of all students which are copied into Student1 Table

Code:

Select sid, sname from Student1;

Output:

Student1 Table

sidsname
1Abhinav
2Ramya
3Preetham
4Nethranand
5Naveen
6Harshita
7Bindu
8Nandhini
9Hashish
10Rahul

Delete the copied table i.e, Student1

Delete the row of the table whose id is 3

Code:

delete from Student1 where sid = 3;

Output:

Rows deleted

Display the Student1 table

Code:

Select * from Student1;

Output:

Student1

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
4Nethranand21Male7675643423
5Naveen23Male6567784532
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839
9Hashish22Male9453215052
10Rahul21Male9998989898

Delete the total table Student1;

Code:

delete from Student1

Output:

Rows deleted

Display the Student1 table

Code:

Select * from Student1;

Output:

No rows selected

Example 2:

Create or copy of Student table i.e, Student2

Code:

Create table Student2 Like Student;

Output:

Table Created

2. Inserting the values of sname data into a new table i.e, Student2

Code:

Insert into Student2 select sname from Student;

Output:

Rows are inserted

Display the table Student2;

Code:

Select sname from Student2;

Output:

Student2 Table

sname
Abhinav
Ramya
Preetham
Nethranand
Naveen
Harshita
Bindu
Nandhini
Hashish
Rahul

Delete all rows of the Student2 table to insert the id of the Student table

Code:

delete from Student2

Output:

Rows deleted

Display the Student1 table

Code:

Select * from Student2;

Output:

No rows selected

2. Inserting the values of sid data into a new table i.e, Student2

Code:

Insert into Student2 select sid from Student;

Output:

Rows are inserted

Display the table Student2;

Code:

Select sid from Student2;

Output:

Student2 Table

sage
22
24
21
21
23
22
26
23
22
21

Delete all rows of the Student2 table to insert the sgender of the Student table

Code:

delete from Student2

Output:

Rows deleted

Display the Student2 table

Code:

Select * from Student2;

Output:

No rows selected

2. Inserting the values of sgender data into a new table i.e, Student2

Code:

Insert into Student2 select sgender from Student;

Output:

Rows are inserted

Display the table Student2;

Code:

Select sid from Student2;

Output:

Student2 Table

sgender
Male
Female
Male
Male
Female
Female
Female
Female
Male
Male

Delete all rows of the Student2 table to insert the data of the Student table

Code:

delete from Student2

Output:

Rows deleted

Display the Student2 table

Code:

Select * from Student2;

Output:

No rows selected

2. Inserting the total data into new table i.e, Student2

Code:

Insert into Student2 select * from Student;

Output:

Rows are inserted

Display the table Student2;

Code:

Select * from Student2;

Output:

Student2 Table

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
4Nethranand21Male7675643423
5Naveen23Male6567784532
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839
9Hashish22Male9453215052
10Rahul21Male9998989898