How can we use WHERE condition when creating a table with CTAS (Create Table as Selected) script?


As we know that we can copy the data and structure from an existing table by CTAS script. Use of WHERE clause is demonstrated in the example below

mysql> Create table EMP_BACKUP2 AS SELECT * from EMPLOYEE WHERE id = 300 AND Name = 'Mohan';
Query OK, 1 row affected (0.14 sec)

Records: 1 Duplicates: 0 Warnings: 0
mysql> Select * from EMP_BACKUP2;

+------+-------+
| Id   | Name  |
+------+-------+
| 300  | Mohan |
+------+-------+
1 row in set (0.00 sec)

In the example above, we have created a table named EMP_BACKUP1 from table ‘Employee’ with some conditions. MySQL creates the table with only one row based on those conditions.

Monica Mona
Monica Mona

Student of life, and a lifelong learner

Updated on: 29-Jan-2020

129 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements