MySQL - WITH (Common Table Expressions)

A common table expression in MySQL is a temporary result whose scope is confined to a single statement. You can refer this expression multiple times with in the statement.
The WITH clause in MySQL is used to specify a Common Table Expression, a with clause can have one or more comms-separated subclauses.
Following is the syntax of the WITH clause −
WITH
   name_for_summary_data AS (
      SELECT Statement)
   SELECT columns
   FROM name_for_summary_data
   WHERE conditions <=> (
      SELECT column
      FROM name_for_summary_data)
   [ORDER BY columns]

Assume we have created a table named data and populated it as shown below −
mysql> CREATE TABLE data(
   ID INT,
   NAME CHAR(20),
   AGE INT,
   SALARY INT
);
INSERT INTO data values (101, 'Raja', 25, 55452);
INSERT INTO data values (102, 'Roja', 29, 66458);
INSERT INTO data values (103, 'Roja', 35, 36944);
Following query demonstrates the usage of the WITH clause −
(Select ID, NAME, AGE, SALARYFROM data)
Select * From CTE; 
+------+------+------+--------+
| ID   | NAME | AGE  | SALARY |
+------+------+------+--------+
| 101  | Raja | 25   | 55452  |
| 102  | Roja | 29   | 66458  |
| 103  | Roja | 35   | 36944  |
+------+------+------+--------+
3 rows in set (0.00 sec)
You can also create a Common Table Expression from multiple tables.
Suppose we have created a table with name EMPLOYEE and populated data into it as shown below −
mysql> CREATE TABLE EMPLOYEE(
   ID INT NOT NULL,
   FIRST_NAME CHAR(20) NOT NULL,
   LAST_NAME CHAR(20),
   AGE INT,
   SEX CHAR(1),
   INCOME FLOAT,
   CONTACT INT
);
Query OK, 0 rows affected (0.36 sec)
mysql> INSERT INTO Employee VALUES
   (101, 'Ramya', 'Rama Priya', 27, 'F', 9000, 101),
   (102, 'Vinay', 'Bhattacharya', 20, 'M', 6000, 102);
And, if we have created another table and populated it as −
mysql> CREATE TABLE CONTACT(
   ID INT NOT NULL,
   EMAIL CHAR(20) NOT NULL,
   PHONE LONG,
   CITY CHAR(20)
);

mysql> INSERT INTO CONTACT (ID, EMAIL, CITY) VALUES
   (101, 'ramya@mymail.com', 'Hyderabad'),
   (102, 'vinay@mymail.com', 'Vishakhapatnam');
Following query create a Common Table Expression from the above two tables −
mysql> WITH
   exp1 AS (SELECT ID, FIRST_NAME, LAST_NAME FROM EMPLOYEE),
   exp2 AS (SELECT EMAIL, PHONE FROM CONTACT)
SELECT * FROM exp1 JOIN exp2;
+-----+------------+--------------+------------------+-------+
| ID  | FIRST_NAME | LAST_NAME    | EMAIL            | PHONE |
+-----+------------+--------------+------------------+-------+
| 101 | Ramya      | Rama Priya   | ramya@mymail.com | NULL  |
| 102 | Vinay      | Bhattacharya | ramya@mymail.com | NULL  |
| 101 | Ramya      | Rama Priya   | vinay@mymail.com | NULL  |
| 102 | Vinay      | Bhattacharya | vinay@mymail.com | NULL  |
+-----+------------+--------------+------------------+-------+
4 rows in set (0.00 sec)
Recursive WITH or Hierarchical queries, is a form of CTE where a CTE can reference to itself, i.e., a WITH query can refer to its own output, hence the name recursive.
Following query demonstrates the usage of the Recursive WITH statement −
WITH RECURSIVE t AS (
   SELECT SALARY FROM data
   UNION ALL
   SELECT SALARY FROM data WHERE SALARY < 50000
)
SELECT * FROM t;
+--------+
| SALARY |
+--------+
| 55452  |
| 66458  |
| 36944  |
| 36944  |
+--------+
4 rows in set (0.03 sec)

    31 Lectures 
     6 hours 

 Eduonix Learning Solutions

    84 Lectures 
     5.5 hours 

 Frahaan Hussain

    6 Lectures 
     3.5 hours 

 DATAhill Solutions Srinivas Reddy

    60 Lectures 
     10 hours 

 Vijay Kumar Parvatha Reddy

    10 Lectures 
     1 hours 

 Harshit Srivastava

    25 Lectures 
     4 hours 

 Trevoir Williams
 Print
 Add Notes
 Bookmark this page