Common table expressions in MySQL
This article aims to understand how to use common table expressions in MySQL.
Most data analysts need to store the results of different queries in order to merge them with a separate query. With the help of common tables, expressions can make this possible. These are sometimes also called WITH
clauses.
Let us try to understand this a little deeper.
However, before we start, we create two virtual tables to work in. Here, we create a table student_dates
and a few rows.
-- create the table student_dates
CREATE TABLE student_dates(
stu_id int,
stu_firstName varchar(255) DEFAULT NULL,
stu_date date,
primary key(stu_id)
);
Similarly, we can create a table student_details
and some defined rows using the following query.
-- create the table student_details
CREATE TABLE student_details(
stu_id int,
stu_firstName varchar(255) DEFAULT NULL,
stu_lastName varchar(255) DEFAULT NULL,
primary key(stu_id)
);
Use INSERT
the statement to insert entries into the student_dates
and student_details
tables
student_dates
Create a table with that name to create a table with that name.
Now with INSERT
the help of statement, let us try to add data for some students. This can be done as follows:
-- insert rows to the table student_dates
INSERT INTO student_dates(stu_firstName,stu_date)
VALUES("Preet",STR_TO_DATE('24-May-2005', '%d-%M-%Y')),
("Dhruv",STR_TO_DATE('14-June-2001', '%d-%M-%Y')),
("Mathew",STR_TO_DATE('13-December-2020', '%d-%M-%Y')),
("Jeet",STR_TO_DATE('14-May-2003', '%d-%M-%Y')),
("Steyn",STR_TO_DATE('19-July-2002', '%d-%M-%Y'));
The above code can be used student_dates
to input student data in . The following command can be used to visualize this table using:
SELECT * from student_dates;
The above code block will generate the following output:
stu_id stu_firstName stu_date
1 Preet 2005-05-24
2 Dhruv 2001-06-14
3 Mathew 2020-12-13
4 Jeet 2003-05-14
5 Steyn 2002-07-19
Similarly, let us student_details
insert values into the table which can be done with the help of the following query.
-- insert rows to the table student_details
INSERT INTO student_details(stu_id,stu_firstName,stu_lastName)
VALUES(1,"Preet","Sanghavi"),
(2,"Rich","John"),
(3,"Veron","Brow"),
(4,"Geo","Jos"),
(5,"Hash","Shah"),
(6,"Sachin","Parker"),
(7,"David","Miller");
student_details
The table can be visualized with the help of the following query.
SELECT * from student_details;
Output:
stu_id stu_firstName stu_lastName
1 Preet Sanghavi
2 Rich John
3 Veron Brow
4 Geo Jos
5 Hash Shah
6 Sachin Parker
7 David Miller
Common table expressions in MySQL
Now let us try to understand WITH
the clause.
stu_id
We can use this clause to join the two tables and get the name and date of the students. The two tables can be matched with the help of which
acts as the primary key .
You can do this using the following query.
WITH
cte1 AS (SELECT stu_id, stu_firstName FROM student_details),
cte2 AS (SELECT stu_id, stu_date FROM student_dates)
SELECT stu_firstName, stu_date FROM cte1 JOIN cte2
WHERE cte1.stu_id = cte2.stu_id;
The previous query will give us the following output.
stu_firstName stu_date
Preet 2005-05-24
Rich 2001-06-14
Veron 2020-12-13
Geo 2003-05-14
Hash 2002-07-19
From the above code block, stu_firstName
the columns are matched stu_id
with the related with the help of column stu_date
.
Thus, with WITH
the help of the clause, we can effectively write common table expressions to store the query in a specific variable for later use in MySQL.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Get the version in MySQL
Publish Date:2025/04/24 Views:169 Category:MySQL
-
In this article, we will learn how to get the current version of MySQL on Windows systems. It is necessary for businesses to maintain versions of different tools and software used to run their business in order to keep systems compatible. T
Full Join in MySQL
Publish Date:2025/04/24 Views:70 Category:MySQL
-
This article aims to explore how to perform a full join or full outer join in MySQL. Full outer join is used to merge or combine the entire data from two separate tables. For example, suppose we have two tables named student_id and student_
Grouping by month in MySQL
Publish Date:2025/04/24 Views:166 Category:MySQL
-
In this article, we will learn how to group values by month in MySQL database. Businesses and organizations have to find user or customer data based on purchase or usage trends over a few months. If a particular business achieves its
Enabling the slow query log in MySQL
Publish Date:2025/04/24 Views:177 Category:MySQL
-
Today, we will enable MySQL in MySQL using MySQL shell on Windows and Ubuntu 20.04 slow_query_log . For this tutorial, we are using MySQL version 8.0 and Ubuntu 20.04. MySQL slow_query_log MySQL slow_query_log contains SQL statements that t
Update multiple tables in MySQL with one query
Publish Date:2025/04/24 Views:65 Category:MySQL
-
In some cases, users want to update logically related tables at the same time. These logically related tables are linked to each other through some attributes. Advantages of updating multiple tables in one MySQL query Similar attributes in
Checking MySQL version in macOS
Publish Date:2025/04/24 Views:60 Category:MySQL
-
In this article, we aim to explore how to check the current version of MySQL on macOS. Checking MySQL version in macOS When trying to figure out the version, you must follow these steps. Each time a person logs into the MySQL server, the ve
How to drop all tables in MySQL
Publish Date:2025/04/24 Views:196 Category:MySQL
-
This article demonstrates several ways for users to delete all tables in MySQL and lists sample scripts to implement this method. The reason you can't just drop all the tables in one line of code is that in a large and well-designed databas
Display tables and database structure in MySQL
Publish Date:2025/04/23 Views:97 Category:MySQL
-
Today, we will learn about queries in MySQL that can display the table and database structure. We will use the mysqldump utility, DESCRIBE the , SHOW TABLES and SHOW CREATE TABLE the statements. We are using MySQL version 8.0.28 while writi
Select first row from MySQL table
Publish Date:2025/04/23 Views:112 Category:MySQL
-
Today, we will explore three scenarios and their solutions where we want to select the first row from a MySQL table. In the first scenario, we will learn to get the first row from a MySQL table where there are multiple instances of a partic