{"id":5871,"date":"2017-07-01T01:51:20","date_gmt":"2017-07-01T08:51:20","guid":{"rendered":"http:\/\/www.mysqltutorial.org\/?page_id=5871"},"modified":"2023-12-28T18:21:57","modified_gmt":"2023-12-29T01:21:57","slug":"mysql-cte","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-cte\/","title":{"rendered":"MySQL CTE"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use MySQL CTE or common table expression to construct complex queries in a more readable manner.<\/p>\n\n\n\n<p class=\"note\">MySQL introduced the common table expression or CTE&nbsp; feature since version 8.0 so you should have MySQL 8.0+ to practice with the statements in this tutorial.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the common table expression (CTE)<\/h2>\n\n\n\n<p>A common table expression is a named temporary result set that exists solely within the execution scope of a single SQL statement, such as <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-select-from\/\"><code>SELECT<\/code><\/a>, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/\"><code>INSERT<\/code><\/a>, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-update\/\"><code>UPDATE<\/code><\/a>, or <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-delete\/\"><code>DELETE<\/code><\/a>.<\/p>\n\n\n\n<p>Similar to a <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-derived-table\/\">derived table<\/a>, a common table expression (CTE) is not stored as an object and lasts only during the query execution.<\/p>\n\n\n\n<p>Unlike a derived table, a common table expression (CTE) can be self-referencing (in the case of a <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-recursive-cte\/\">recursive CTE<\/a>) or referenced multiple times within the same query. Moreover, a CTE offers enhanced readability and performance compared to a derived table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL CTE syntax<\/h2>\n\n\n\n<p>The structure of a CTE includes the name, an optional column list, and a query that defines the CTE. After you define a CTE, you can use like a view in the <code>SELECT<\/code>, <code>INSERT<\/code>, <code>UPDATE<\/code>, <code>DELETE<\/code>, or <code>CREATE VIEW<\/code> statement.<\/p>\n\n\n\n<p>The following illustrates the basic syntax of a CTE:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">WITH<\/span> cte_name (column_list) <span class=\"hljs-keyword\">AS<\/span> (\n    <span class=\"hljs-keyword\">query<\/span>\n) \n<span class=\"hljs-keyword\">SELECT<\/span> * <span class=\"hljs-keyword\">FROM<\/span> cte_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>WITH cte_name (column_list) AS<\/code>: define a CTE with the name cte_name and a list of columns (column_list) that the CTE will have. The column_list is optional if you don&#8217;t specify column_list, the CTE will inherit the column names from the result of the query.<\/li>\n\n\n\n<li><code>query<\/code>: This is the query that defines the CTE. MySQL will store the result of the query in the CTE.<\/li>\n\n\n\n<li><code>SELECT * FROM cte_name<\/code>: This is an example of how you can use the CTE. In this case, it is a simple SELECT statement that retrieves all columns from the CTE.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL CTE examples<\/h2>\n\n\n\n<p>Let&#8217;s explore some examples of using MySQL CTE.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) Basic MySQL CTE example<\/h3>\n\n\n\n<p>We&#8217;ll use the <code>customers<\/code> table from the <a href=\"https:\/\/www.mysqltutorial.org\/getting-started-with-mysql\/mysql-sample-database\/\">sample database<\/a> for demonstration:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2023\/10\/customers.svg\" alt=\"\" class=\"wp-image-10765\"\/><\/figure>\n\n\n\n<p>The following example illustrates how to use a CTE for querying data from the <code>customers<\/code> table in the <a href=\"https:\/\/www.mysqltutorial.org\/getting-started-with-mysql\/mysql-sample-database\/\">sample database<\/a>.<\/p>\n\n\n\n<p class=\"note\">Note that this example is only for demonstration purposes to make it easy for you to understand the CTE concept.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">WITH<\/span> customers_in_usa <span class=\"hljs-keyword\">AS<\/span> (\n    <span class=\"hljs-keyword\">SELECT<\/span> \n        customerName, state\n    <span class=\"hljs-keyword\">FROM<\/span>\n        customers\n    <span class=\"hljs-keyword\">WHERE<\/span>\n        country = <span class=\"hljs-string\">'USA'<\/span>\n) <span class=\"hljs-keyword\">SELECT<\/span> \n    customerName\n <span class=\"hljs-keyword\">FROM<\/span>\n    customers_in_usa\n <span class=\"hljs-keyword\">WHERE<\/span>\n    state = <span class=\"hljs-string\">'CA'<\/span>\n <span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> customerName;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">+------------------------------+\r\n| customerName                 |\r\n+------------------------------+\r\n| Boards &amp; Toys Co.            |\r\n| Collectable Mini Designs Co. |\r\n| Corporate Gift Ideas Co.     |\r\n| Men <span class=\"hljs-string\">'R'<\/span> US Retailers, Ltd.   |\r\n| Mini Gifts Distributors Ltd. |\r\n| Mini Wheels Co.              |\r\n| Signal Collectibles Ltd.     |\r\n| Technics Stores Inc.         |\r\n| The Sharp Gifts Warehouse    |\r\n| Toys4GrownUps.com            |\r\n| West Coast Collectables Co.  |\r\n+------------------------------+\r\n<span class=\"hljs-number\">11<\/span> rows <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-keyword\">set<\/span> (0.00 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, define a CTE with the name customers_in_usa that stores the customer name and state of customers in the USA. The defining query retrieves data from the <code>customers<\/code> table.<\/li>\n\n\n\n<li>Second, select the customers located in California from the CTE.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2) Getting top sales using a CTE<\/h3>\n\n\n\n<p>We&#8217;ll use the <code>orders<\/code>, <code>orderdetails<\/code>, and <code>employees<\/code> from the sample database:<\/p>\n\n\n\n<p>The following example uses a CTE to retrieve the top 5 sales representatives based on their total sales in the year 2003:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">WITH<\/span> topsales2003 <span class=\"hljs-keyword\">AS<\/span> (\n    <span class=\"hljs-keyword\">SELECT<\/span> \n        salesRepEmployeeNumber employeeNumber,\n        <span class=\"hljs-keyword\">SUM<\/span>(quantityOrdered * priceEach) sales\n    <span class=\"hljs-keyword\">FROM<\/span>\n        orders\n            <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span>\n        orderdetails <span class=\"hljs-keyword\">USING<\/span> (orderNumber)\n            <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span>\n        customers <span class=\"hljs-keyword\">USING<\/span> (customerNumber)\n    <span class=\"hljs-keyword\">WHERE<\/span>\n        <span class=\"hljs-keyword\">YEAR<\/span>(shippedDate) = <span class=\"hljs-number\">2003<\/span>\n            <span class=\"hljs-keyword\">AND<\/span> <span class=\"hljs-keyword\">status<\/span> = <span class=\"hljs-string\">'Shipped'<\/span>\n    <span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span> salesRepEmployeeNumber\n    <span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> sales <span class=\"hljs-keyword\">DESC<\/span>\n    <span class=\"hljs-keyword\">LIMIT<\/span> <span class=\"hljs-number\">5<\/span>\n)\n<span class=\"hljs-keyword\">SELECT<\/span> \n    employeeNumber, \n    firstName, \n    lastName, \n    sales\n<span class=\"hljs-keyword\">FROM<\/span>\n    employees\n        <span class=\"hljs-keyword\">JOIN<\/span>\n    topsales2003 <span class=\"hljs-keyword\">USING<\/span> (employeeNumber);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">+----------------+-----------+-----------+-----------+\r\n| employeeNumber | firstName | lastName  | sales     |\r\n+----------------+-----------+-----------+-----------+\r\n|           <span class=\"hljs-number\">1165<\/span> | Leslie    | Jennings  | <span class=\"hljs-number\">413219.85<\/span> |\r\n|           <span class=\"hljs-number\">1370<\/span> | Gerard    | Hernandez | <span class=\"hljs-number\">295246.44<\/span> |\r\n|           <span class=\"hljs-number\">1401<\/span> | Pamela    | Castillo  | <span class=\"hljs-number\">289982.88<\/span> |\r\n|           <span class=\"hljs-number\">1621<\/span> | Mami      | Nishi     | <span class=\"hljs-number\">267249.40<\/span> |\r\n|           <span class=\"hljs-number\">1501<\/span> | Larry     | Bott      | <span class=\"hljs-number\">261536.95<\/span> |\r\n+----------------+-----------+-----------+-----------+\r\n<span class=\"hljs-number\">5<\/span> rows <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-keyword\">set<\/span> (0.02 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, define a CTE that retrieves the top 5 employees with their total sales in 2003. <\/li>\n\n\n\n<li>Second, join the CTE with the <code>employees<\/code> table to include the first and last names of the sales representatives.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3) Using multiple CTEs<\/h3>\n\n\n\n<p>We&#8217;ll use the customers and employees from the sample database:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"433\" height=\"304\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/customers-employees.png\" alt=\"\" class=\"wp-image-8163\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/customers-employees.png 433w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/customers-employees-200x140.png 200w\" sizes=\"auto, (max-width: 433px) 100vw, 433px\" \/><\/figure>\n\n\n\n<p>The following example uses multiple CTEs to map the customers with their respective sales representatives:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">WITH<\/span> salesrep <span class=\"hljs-keyword\">AS<\/span> (\n    <span class=\"hljs-keyword\">SELECT<\/span> \n        employeeNumber,\n        <span class=\"hljs-keyword\">CONCAT<\/span>(firstName, <span class=\"hljs-string\">' '<\/span>, lastName) <span class=\"hljs-keyword\">AS<\/span> salesrepName\n    <span class=\"hljs-keyword\">FROM<\/span>\n        employees\n    <span class=\"hljs-keyword\">WHERE<\/span>\n        jobTitle = <span class=\"hljs-string\">'Sales Rep'<\/span>\n),\ncustomer_salesrep <span class=\"hljs-keyword\">AS<\/span> (\n    <span class=\"hljs-keyword\">SELECT<\/span> \n        customerName, salesrepName\n    <span class=\"hljs-keyword\">FROM<\/span>\n        customers\n            <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span>\n        salesrep <span class=\"hljs-keyword\">ON<\/span> employeeNumber = salesrepEmployeeNumber\n)\n<span class=\"hljs-keyword\">SELECT<\/span> \n    *\n<span class=\"hljs-keyword\">FROM<\/span>\n    customer_salesrep\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> customerName;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">+------------------------------------+------------------+\r\n| customerName                       | salesrepName     |\r\n+------------------------------------+------------------+\r\n| Alpha Cognac                       | Gerard Hernandez |\r\n| American Souvenirs Inc             | Foon Yue Tseng   |\r\n| Amica Models &amp; Co.                 | Pamela Castillo  |\r\n| Anna's Decorations, Ltd            | Andy Fixter      |\r\n| Atelier graphique                  | Gerard Hernandez |\r\n| Australian Collectables, Ltd       | Andy Fixter      |\r\n| Australian Collectors, Co.         | Andy Fixter      |\r\n| Australian Gift Network, Co        | Andy Fixter      |\n...<\/code><\/span><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>CTE  <code>salesrep<\/code>: Select <code>employeeNumber<\/code> and concatenate the <code>firstName<\/code> and <code>lastName<\/code> columns to create a column named <code>salesrepName<\/code>, and include only employees with the job title <code>'Sales Rep'<\/code>.<\/li>\n\n\n\n<li>CTE  <code>customer_salesrep<\/code>: selects <code>customerName<\/code> and <code>salesrepName<\/code> by joining the <code>customers<\/code> table with the <code>salesrep<\/code> CTE based on the common column <code>employeeNumber<\/code>.<\/li>\n\n\n\n<li>Main query: Select all columns from the <code>customer_salesrep<\/code> CTE.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4) Joining two CTEs example<\/h3>\n\n\n\n<p>We&#8217;ll use the <code>offices<\/code> and <code>employees<\/code> tables from the sample database:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"403\" height=\"224\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/offices-employees.png\" alt=\"\" class=\"wp-image-7904\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/offices-employees.png 403w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/offices-employees-200x111.png 200w\" sizes=\"auto, (max-width: 403px) 100vw, 403px\" \/><\/figure>\n\n\n\n<p>The following example is creating two CTEs and joining them to get the Sales Representatives located in the USA, including their office information:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">WITH<\/span> e <span class=\"hljs-keyword\">AS<\/span> (\n  <span class=\"hljs-keyword\">SELECT<\/span> \n    * \n  <span class=\"hljs-keyword\">FROM<\/span> \n    employees \n  <span class=\"hljs-keyword\">WHERE<\/span> \n    jobTitle = <span class=\"hljs-string\">'Sales Rep'<\/span>\n), \no <span class=\"hljs-keyword\">AS<\/span> (\n  <span class=\"hljs-keyword\">SELECT<\/span> \n    * \n  <span class=\"hljs-keyword\">FROM<\/span> \n    offices \n  <span class=\"hljs-keyword\">WHERE<\/span> \n    country = <span class=\"hljs-string\">'USA'<\/span>\n) \n<span class=\"hljs-keyword\">SELECT<\/span> \n  firstName, \n  lastName, \n  city, \n  state, \n  postalCode \n<span class=\"hljs-keyword\">FROM<\/span> \n  e \n  <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> o <span class=\"hljs-keyword\">USING<\/span> (officeCode);\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">+-----------+-----------+---------------+-------+------------+\r\n| firstName | lastName  | city          | state | postalCode |\r\n+-----------+-----------+---------------+-------+------------+\r\n| Leslie    | Jennings  | San Francisco | CA    | <span class=\"hljs-number\">94080<\/span>      |\r\n| Leslie    | Thompson  | San Francisco | CA    | <span class=\"hljs-number\">94080<\/span>      |\r\n| Julie     | Firrelli  | Boston        | MA    | <span class=\"hljs-number\">02107<\/span>      |\r\n| Steve     | Patterson | Boston        | MA    | <span class=\"hljs-number\">02107<\/span>      |\r\n| Foon Yue  | Tseng     | NYC           | NY    | <span class=\"hljs-number\">10022<\/span>      |\r\n| George    | Vanauf    | NYC           | NY    | <span class=\"hljs-number\">10022<\/span>      |\r\n+-----------+-----------+---------------+-------+------------+\r\n<span class=\"hljs-number\">6<\/span> rows <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-keyword\">set<\/span> (0.00 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>CTE e: Retrieve employees whose job title is <code>Sales Rep<\/code>.<\/li>\n\n\n\n<li>CTE o: Retrieve offices located in the USA.<\/li>\n\n\n\n<li>Main query: Joins the CTE e and o using the <code>officeCode<\/code> column.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use MySQL CTEs to break down complex queries into simpler, more manageable queries. Each CTE represents a temporary result set that can be referenced within the main query.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful? <\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"5871\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-cte\/\"\n\t\t\t\tdata-post-title=\"MySQL CTE\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"5871\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-cte\/\"\n\t\t\t\tdata-post-title=\"MySQL CTE\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows you how to use MySQL CTE or common table expression feature to construct complex queries in a more readable manner.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":174,"menu_order":30,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-5871","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>An Introduction to MySQL CTE<\/title>\n<meta name=\"description\" content=\"You will learn how to use MySQL CTE or common table expression feature to construct complex queries in a more readable manner.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mysqltutorial.org\/mysql-cte\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"An Introduction to MySQL CTE\" \/>\n<meta property=\"og:description\" content=\"You will learn how to use MySQL CTE or common table expression feature to construct complex queries in a more readable manner.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-cte\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-29T01:21:57+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2023\/10\/customers.svg\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-cte\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-cte\\\/\",\"name\":\"An Introduction to MySQL CTE\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-cte\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-cte\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/customers.svg\",\"datePublished\":\"2017-07-01T08:51:20+00:00\",\"dateModified\":\"2023-12-29T01:21:57+00:00\",\"description\":\"You will learn how to use MySQL CTE or common table expression feature to construct complex queries in a more readable manner.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-cte\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-cte\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-cte\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/customers.svg\",\"contentUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2023\\\/10\\\/customers.svg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-cte\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL Basics\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MySQL CTE\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\",\"name\":\"MySQL Tutorial\",\"description\":\"A comprehensive MySQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mysqltutorial.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"An Introduction to MySQL CTE","description":"You will learn how to use MySQL CTE or common table expression feature to construct complex queries in a more readable manner.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mysqltutorial.org\/mysql-cte\/","og_locale":"en_US","og_type":"article","og_title":"An Introduction to MySQL CTE","og_description":"You will learn how to use MySQL CTE or common table expression feature to construct complex queries in a more readable manner.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-cte\/","og_site_name":"MySQL Tutorial","article_modified_time":"2023-12-29T01:21:57+00:00","og_image":[{"url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2023\/10\/customers.svg","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-cte\/","url":"https:\/\/www.mysqltutorial.org\/mysql-cte\/","name":"An Introduction to MySQL CTE","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-cte\/#primaryimage"},"image":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-cte\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2023\/10\/customers.svg","datePublished":"2017-07-01T08:51:20+00:00","dateModified":"2023-12-29T01:21:57+00:00","description":"You will learn how to use MySQL CTE or common table expression feature to construct complex queries in a more readable manner.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-cte\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-cte\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mysqltutorial.org\/mysql-cte\/#primaryimage","url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2023\/10\/customers.svg","contentUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2023\/10\/customers.svg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-cte\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"MySQL Basics","item":"https:\/\/www.mysqltutorial.org\/mysql-basics\/"},{"@type":"ListItem","position":3,"name":"MySQL CTE"}]},{"@type":"WebSite","@id":"https:\/\/www.mysqltutorial.org\/#website","url":"https:\/\/www.mysqltutorial.org\/","name":"MySQL Tutorial","description":"A comprehensive MySQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mysqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/5871","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/comments?post=5871"}],"version-history":[{"count":4,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/5871\/revisions"}],"predecessor-version":[{"id":13673,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/5871\/revisions\/13673"}],"up":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/174"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=5871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}