What CONNECT BY does and which MySQL versions have it

CONNECT BY is not a standard MySQL feature. It is a clause from Oracle Database that lets you query hierarchical data — like an organizational chart or a file system — by following parent-child relationships in a single table. MySQL does not include CONNECT BY in any version, including the latest releases.

If you are working with MySQL and need to query hierarchical data, you have two main paths: use a different syntax that MySQL actually supports, or switch to a database system that includes CONNECT BY. The choice depends on whether you can change your database or need to adapt your queries to work with MySQL's existing tools.

Key Takeaways

  • MySQL has never included CONNECT BY; it is an Oracle Database feature that does not exist in any MySQL version.
  • MySQL supports hierarchical queries using Common Table Expressions (CTEs) with the WITH clause, which can replace most CONNECT BY use cases.
  • If you are migrating from Oracle to MySQL, you will need to rewrite CONNECT BY queries using recursive CTEs or self-joins.
  • MariaDB, a MySQL fork, also does not include CONNECT BY but supports the same CTE approach for hierarchical data.

Why MySQL does not have CONNECT BY

CONNECT BY was designed specifically for Oracle Database and reflects how Oracle handles tree-structured data. MySQL took a different architectural approach and instead uses recursive Common Table Expressions (CTEs) to solve the same problem. Both methods work, but they use different syntax and logic.

This is not a limitation that will be fixed in a future MySQL release — it is a deliberate design choice. The MySQL team considers recursive CTEs to be the standard SQL way to handle hierarchies, and CONNECT BY to be Oracle-specific syntax. If you are stuck with CONNECT BY code, you will need to translate it.

Using recursive CTEs instead of CONNECT BY

A recursive CTE is a WITH clause that references itself. It has two parts: a base query that finds the starting rows, and a recursive part that finds the next level by joining back to the CTE. Here is a simple example that shows an organizational hierarchy:

If you had an Oracle query like this:

SELECT employee_id, name, manager_id FROM employees START WITH manager_id IS NULL CONNECT BY PRIOR employee_id = manager_id;

In MySQL, you would write:

WITH RECURSIVE org_tree AS ( SELECT employee_id, name, manager_id, 1 AS level FROM employees WHERE manager_id IS NULL UNION ALL SELECT e.employee_id, e.name, e.manager_id, ot.level + 1 FROM employees e JOIN org_tree ot ON e.manager_id = ot.employee_id ) SELECT * FROM org_tree;

The recursive CTE starts with employees who have no manager (the root of the tree), then repeatedly joins the employees table to itself to find the next level down. The result is the same hierarchical view you would get from CONNECT BY.

When you might think MySQL has CONNECT BY

Some developers confuse CONNECT BY with other MySQL features. The most common mix-up is with self-joins, which let you join a table to itself to find related rows. A self-join can show parent-child pairs but does not automatically traverse the full hierarchy the way CONNECT BY does — you have to write the join logic yourself for each level.

Another source of confusion is stored procedures. You can write a MySQL stored procedure that mimics CONNECT BY behavior by looping through levels and building the result set step by step. This works but is slower than a recursive CTE and harder to maintain.

Checking your MySQL version and documentation

You can check your MySQL version by running this command in any MySQL client:

SELECT VERSION();

The output will show your version number — for example, 8.0.32 or 5.7.40. You can then look up that version in the official MySQL documentation at dev.mysql.com. Search for "recursive common table expressions" or "WITH clause" to see the exact syntax your version supports. Recursive CTEs have been available since MySQL 8.0.0, so if you are on version 8.0 or later, you can use them without any issues.

If you are on MySQL 5.7 or earlier, recursive CTEs are not available, and you will need to use self-joins or a stored procedure instead. Upgrading to MySQL 8.0 or later is the simplest solution if you have the option.

Migrating CONNECT BY queries from Oracle

If you are moving code from Oracle Database to MySQL, you will need to rewrite every CONNECT BY query. The translation is usually straightforward: identify the table, the column that links parent to child, and the starting condition (the WHERE clause in the START WITH part). Then build a recursive CTE that does the same thing.

The main difference in behavior is that CONNECT BY can use PRIOR to reference the parent row in the recursive step, while CTEs use explicit column names. CONNECT BY also has built-in functions like LEVEL (which shows the depth in the tree) and CONNECT_BY_ROOT (which shows the root ancestor). In MySQL, you add a level column to the base query and increment it in the recursive part, and you can store the root value in a separate column if you need it.

Test your recursive CTE with a small dataset first to make sure it produces the right hierarchy. Recursive CTEs can be slow on large tables if the tree is very deep, so you may also want to add a depth limit to prevent runaway queries.

Frequently Asked Questions

Does MariaDB support CONNECT BY?

No. MariaDB is a fork of MySQL and uses the same approach — recursive CTEs instead of CONNECT BY. If you are using MariaDB, the recursive CTE syntax is the same as MySQL 8.0 and later.

Can I use a stored procedure to replace CONNECT BY?

Yes, but it is slower and more complex. A stored procedure can loop through levels and build a temporary table, which works for small hierarchies. For most use cases, a recursive CTE is cleaner and faster.

What if my MySQL version is older than 8.0?

Recursive CTEs are not available in MySQL 5.7 and earlier. You can use self-joins to find one level at a time, or use a stored procedure. The best long-term solution is to upgrade to MySQL 8.0 or later.

Will MySQL ever add CONNECT BY support?

It is unlikely. The MySQL team considers recursive CTEs to be the standard SQL approach, and CONNECT BY to be Oracle-specific syntax. Adding CONNECT BY would mean maintaining two ways to do the same thing.

How do I handle CONNECT_BY_ROOT in MySQL?

Add a column to your base query that stores the root value, then carry it through the recursive part without changing it. For example, add employee_id AS root_id in the base query, and include ot.root_id in the recursive SELECT.