Use WHERE Condition in CTAS for Table Creation



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.

Updated on: 2020-01-29T06:39:37+05:30

200 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements