{"id":5883,"date":"2017-07-02T23:42:17","date_gmt":"2017-07-03T06:42:17","guid":{"rendered":"http:\/\/www.mysqltutorial.org\/?page_id=5883"},"modified":"2023-12-28T18:45:18","modified_gmt":"2023-12-29T01:45:18","slug":"mysql-recursive-cte","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-recursive-cte\/","title":{"rendered":"MySQL Recursive CTE"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about MySQL recursive CTE and how to use it to traverse hierarchical data.<\/p>\n\n\n\n<p class=\"note\">Notice that a\u00a0<a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-cte\/\">common table expression<\/a>\u00a0(CTE) is only available in MySQL version 8.0 or later. Therefore, ensure that you have the right version of MySQL installed to use the statements in this tutorial.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to MySQL recursive CTE<\/h2>\n\n\n\n<p>In MySQL, a recursive Common Table Expression (CTE) is a named temporary result set that references itself in the recursive member, enabling the hierarchical traversal or iteration over data until a specified termination condition is met.<\/p>\n\n\n\n<p>The following illustrates the syntax of a recursive 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> <span class=\"hljs-keyword\">RECURSIVE<\/span> cte_name <span class=\"hljs-keyword\">AS<\/span> (\n    initial_query  <span class=\"hljs-comment\">-- anchor member<\/span>\n    <span class=\"hljs-keyword\">UNION<\/span> <span class=\"hljs-keyword\">ALL<\/span>\n    recursive_query <span class=\"hljs-comment\">-- recursive member that references to the CTE name<\/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>A recursive CTE consists of three main parts:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>An initial <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-select-from\/\">query<\/a> that forms the base result set of the CTE structure. The initial query part is referred to as an anchor member.<\/li>\n\n\n\n<li>A recursive query part is a query that references the CTE name, therefore, it is called a recursive member. The recursive member is joined with the anchor member by a <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-union\/\">UNION ALL<\/a><\/code> or <code>UNION DISTINCT<\/code> operator.<\/li>\n\n\n\n<li>A termination condition that ensures the recursion stops when the recursive member returns no row.<\/li>\n<\/ul>\n\n\n\n<p>The execution order of a recursive CTE is as follows:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>First, separate the members into two: anchor and recursive members.<\/li>\n\n\n\n<li>Next, execute the anchor member to form the base result set ( <code>R0<\/code>) and use this base result set for the next iteration.<\/li>\n\n\n\n<li>Then, execute the recursive member with <code>Ri<\/code> result set as an input and make <code>Ri+1<\/code> as an output.<\/li>\n\n\n\n<li>After that, repeat the third step until the recursive member returns an empty result set, in other words, the termination condition is met.<\/li>\n\n\n\n<li>Finally, combine result sets from <code>R0<\/code> to <code>Rn<\/code> using <code>UNION ALL<\/code> operator.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Recursive member restrictions<\/h2>\n\n\n\n<p>The recursive member must not contain the following constructs:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Aggregate functions e.g., <a href=\"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-max-function\/\">MAX<\/a>, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-min\/\">MIN<\/a>, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-sum\/\">SUM<\/a>, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-avg\/\">AVG<\/a>, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-aggregate-functions\/mysql-count\/\">COUNT<\/a>, etc.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-group-by\/\">GROUP BY<\/a> clause<\/li>\n\n\n\n<li><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-order-by\/\">ORDER BY<\/a> clause<\/li>\n\n\n\n<li><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-limit\/\">LIMIT <\/a>clause<\/li>\n\n\n\n<li><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-distinct\/\">DISTINCT<\/a><\/li>\n<\/ul>\n\n\n\n<p>Note that the above constraint does not apply to the anchor member. Furthermore, the restriction on using <code>DISTINCT<\/code> only applies when you use <code>UNION<\/code> operator. If you use the <code>UNION DISTINCT<\/code> operator, the <code>DISTINCT<\/code> is permitted.<\/p>\n\n\n\n<p>In addition, the recursive member can reference the CTE name only once in its <code>FROM<\/code> clause and not in any <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-subquery\/\">subquery<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Basic MySQL recursive CTE example<\/h2>\n\n\n\n<p>See the following simple recursive CTE example:<\/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> <span class=\"hljs-keyword\">RECURSIVE<\/span> cte_count (n) \n<span class=\"hljs-keyword\">AS<\/span> (\n      <span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-number\">1<\/span>\n      <span class=\"hljs-keyword\">UNION<\/span> <span class=\"hljs-keyword\">ALL<\/span>\n      <span class=\"hljs-keyword\">SELECT<\/span> n + <span class=\"hljs-number\">1<\/span> \n      <span class=\"hljs-keyword\">FROM<\/span> cte_count \n      <span class=\"hljs-keyword\">WHERE<\/span> n &lt; <span class=\"hljs-number\">3<\/span>\n    )\n<span class=\"hljs-keyword\">SELECT<\/span> n \n<span class=\"hljs-keyword\">FROM<\/span> cte_count;<\/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>In this example, the following query:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-number\">1<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><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>is the anchor member that returns 1 as the base result set.<\/p>\n\n\n\n<p>The following query<\/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\">SELECT<\/span> n + <span class=\"hljs-number\">1<\/span>\n<span class=\"hljs-keyword\">FROM<\/span> cte_count \n<span class=\"hljs-keyword\">WHERE<\/span> n &lt; <span class=\"hljs-number\">3<\/span><\/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>is the recursive member because it references the name of the CTE which is <code>cte_count<\/code>.<\/p>\n\n\n\n<p>The expression <code>n &lt; 3<\/code>&nbsp;in the recursive member is the termination condition. Once n equals 3, the recursive member returns an empty set that will stop the recursion.<\/p>\n\n\n\n<p>The following picture illustrates the elements of CTE above:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"702\" height=\"315\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2017\/07\/MySQL-Recursive-CTE.png\" alt=\"MySQL Recursive CTE\" class=\"wp-image-5884\" title=\"MySQL Recursive CTE\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2017\/07\/MySQL-Recursive-CTE.png 702w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2017\/07\/MySQL-Recursive-CTE-300x135.png 300w\" sizes=\"auto, (max-width: 702px) 100vw, 702px\" \/><\/figure>\n\n\n\n<p>The recursive CTE returns the following output:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"67\" height=\"80\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2017\/07\/MySQL-Recursive-CTE-Example.png\" alt=\"MySQL Recursive CTE Example\" class=\"wp-image-5885\" title=\"MySQL Recursive CTE Example\"\/><\/figure>\n\n\n\n<p>The execution steps of the recursive CTE are as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, separate the anchor and recursive members.<\/li>\n\n\n\n<li>Next, the anchor member forms the initial row ( <code>SELECT 1<\/code>) therefore the first iteration produces 1 + 1 = 2 with n = 1.<\/li>\n\n\n\n<li>Then, the second iteration operates on the output of the first iteration (2) and produces 2 + 1 = 3 with n = 2.<\/li>\n\n\n\n<li>After that, before the third operation ( n = 3), the termination condition ( <code>n &lt; 3<\/code>) is met therefore the query stops.<\/li>\n\n\n\n<li>Finally, combine all result sets 1, 2, and 3 using the <code>UNION ALL<\/code> operator<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Using MySQL recursive CTE to traverse the hierarchical data<\/h2>\n\n\n\n<p>First, create a new database called <code>mydb<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">CREATE DATABASE IF NOT EXIST mydb;<\/code><\/span><\/pre>\n\n\n<p>Second, change the current database to <code>mydb<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">CREATE TABLE employees (\r\n    employee_id INT PRIMARY KEY,\r\n    employee_name VARCHAR(50),\r\n    manager_id INT\r\n);<\/code><\/span><\/pre>\n\n\n<p>Third, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert-multiple-rows\/\">insert some rows<\/a> into the <code>employees<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">INSERT INTO employees VALUES\r\n    (<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-string\">'John Doe'<\/span>, <span class=\"hljs-keyword\">NULL<\/span>),         -- CEO, no manager\r\n    (<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-string\">'Jane Smith'<\/span>, <span class=\"hljs-number\">1<\/span>),          -- Manager, reports to CEO\r\n    (<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-string\">'Bob Johnson'<\/span>, <span class=\"hljs-number\">2<\/span>),         -- Employee, reports to Jane Smith\r\n    (<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-string\">'Alice Brown'<\/span>, <span class=\"hljs-number\">2<\/span>),         -- Employee, reports to Jane Smith\r\n    (<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-string\">'Charlie Davis'<\/span>, <span class=\"hljs-number\">3<\/span>);       -- Employee, reports to Bob Johnson\r\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, traverse the hierarchical data in the <code>employees<\/code> table using a recursive CTE:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">WITH RECURSIVE EmployeeHierarchy <span class=\"hljs-keyword\">AS<\/span> (\r\n    SELECT\r\n        employee_id,\r\n        employee_name,\r\n        manager_id,\r\n        <span class=\"hljs-number\">0<\/span> <span class=\"hljs-keyword\">AS<\/span> level\r\n    FROM\r\n        employees\r\n    WHERE\r\n        manager_id IS <span class=\"hljs-keyword\">NULL<\/span> -- Anchor member (root of the hierarchy)\r\n        \r\n    UNION ALL\r\n    \r\n    SELECT\r\n        e.employee_id,\r\n        e.employee_name,\r\n        e.manager_id,\r\n        eh.level + <span class=\"hljs-number\">1<\/span>\r\n    FROM\r\n        employees e\r\n    INNER JOIN\r\n        EmployeeHierarchy eh ON e.manager_id = eh.employee_id -- Recursive member\r\n)\r\n-- <span class=\"hljs-keyword\">Final<\/span> query to select from the CTE\r\nSELECT\r\n    employee_id,\r\n    employee_name,\r\n    manager_id,\r\n    level\r\nFROM\r\n    EmployeeHierarchy\r\nORDER BY\r\n    level, employee_id;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/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-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">+-------------+---------------+------------+-------+\r\n| employee_id | employee_name | manager_id | level |\r\n+-------------+---------------+------------+-------+\r\n|           <span class=\"hljs-number\">1<\/span> | John Doe      |       NULL |     <span class=\"hljs-number\">0<\/span> |\r\n|           <span class=\"hljs-number\">2<\/span> | Jane Smith    |          <span class=\"hljs-number\">1<\/span> |     <span class=\"hljs-number\">1<\/span> |\r\n|           <span class=\"hljs-number\">3<\/span> | Bob Johnson   |          <span class=\"hljs-number\">2<\/span> |     <span class=\"hljs-number\">2<\/span> |\r\n|           <span class=\"hljs-number\">4<\/span> | Alice Brown   |          <span class=\"hljs-number\">2<\/span> |     <span class=\"hljs-number\">2<\/span> |\r\n|           <span class=\"hljs-number\">5<\/span> | Charlie Davis |          <span class=\"hljs-number\">3<\/span> |     <span class=\"hljs-number\">3<\/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.01 sec)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><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>Define The CTE with the name <code>EmployeeHierarchy<\/code>.<\/li>\n\n\n\n<li>Define an anchor member that selects employees who do not have a manager (<code>manager_id IS NULL<\/code>), starting with the root of the hierarchy (CEO).<\/li>\n\n\n\n<li>Use a recursive member to join the <code>employees<\/code> table with the CTE on the condition that the <code>manager_id<\/code> in the <code>employees<\/code> table matches the <code>employee_id<\/code> in the CTE, effectively traversing the hierarchy.<\/li>\n\n\n\n<li>Select information from the CTE, including the employee&#8217;s ID, name, manager&#8217;s ID, and the level in the hierarchy in the final query. And sort the result set by level and employee ID.<\/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 recursive CTE to traverse hierarchical data.<\/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=\"5883\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-recursive-cte\/\"\n\t\t\t\tdata-post-title=\"MySQL Recursive 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=\"5883\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-recursive-cte\/\"\n\t\t\t\tdata-post-title=\"MySQL Recursive 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>In this tutorial, you will learn about MySQL recursive CTE and how to use it to traverse hierarchical data in the MySQL database.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":174,"menu_order":31,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-5883","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>MySQL Recursive CTE<\/title>\n<meta name=\"description\" content=\"In this tutorial, you will learn about MySQL recursive CTE and how to use it to traverse hierarchical data in the MySQL database.\" \/>\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-recursive-cte\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL Recursive CTE\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you will learn about MySQL recursive CTE and how to use it to traverse hierarchical data in the MySQL database.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-recursive-cte\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-29T01:45:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2017\/07\/MySQL-Recursive-CTE.png\" \/>\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-recursive-cte\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-recursive-cte\\\/\",\"name\":\"MySQL Recursive CTE\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-recursive-cte\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-recursive-cte\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2017\\\/07\\\/MySQL-Recursive-CTE.png\",\"datePublished\":\"2017-07-03T06:42:17+00:00\",\"dateModified\":\"2023-12-29T01:45:18+00:00\",\"description\":\"In this tutorial, you will learn about MySQL recursive CTE and how to use it to traverse hierarchical data in the MySQL database.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-recursive-cte\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-recursive-cte\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-recursive-cte\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2017\\\/07\\\/MySQL-Recursive-CTE.png\",\"contentUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2017\\\/07\\\/MySQL-Recursive-CTE.png\",\"width\":702,\"height\":315},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-recursive-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 Recursive 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":"MySQL Recursive CTE","description":"In this tutorial, you will learn about MySQL recursive CTE and how to use it to traverse hierarchical data in the MySQL database.","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-recursive-cte\/","og_locale":"en_US","og_type":"article","og_title":"MySQL Recursive CTE","og_description":"In this tutorial, you will learn about MySQL recursive CTE and how to use it to traverse hierarchical data in the MySQL database.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-recursive-cte\/","og_site_name":"MySQL Tutorial","article_modified_time":"2023-12-29T01:45:18+00:00","og_image":[{"url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2017\/07\/MySQL-Recursive-CTE.png","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-recursive-cte\/","url":"https:\/\/www.mysqltutorial.org\/mysql-recursive-cte\/","name":"MySQL Recursive CTE","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-recursive-cte\/#primaryimage"},"image":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-recursive-cte\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2017\/07\/MySQL-Recursive-CTE.png","datePublished":"2017-07-03T06:42:17+00:00","dateModified":"2023-12-29T01:45:18+00:00","description":"In this tutorial, you will learn about MySQL recursive CTE and how to use it to traverse hierarchical data in the MySQL database.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-recursive-cte\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-recursive-cte\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mysqltutorial.org\/mysql-recursive-cte\/#primaryimage","url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2017\/07\/MySQL-Recursive-CTE.png","contentUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2017\/07\/MySQL-Recursive-CTE.png","width":702,"height":315},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-recursive-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 Recursive 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\/5883","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=5883"}],"version-history":[{"count":5,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/5883\/revisions"}],"predecessor-version":[{"id":13679,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/5883\/revisions\/13679"}],"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=5883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}