Last active
September 30, 2022 12:55
-
-
Save DominikStyp/4174e1ae1640c037359026a78cda0189 to your computer and use it in GitHub Desktop.
SQL CTE (Common Table Expressions) with recursion example in MariaDB/MySQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
USE test; | |
-- we use recursive CTE to find a tree of managers and count their place in hierarchy tree from top to bottom | |
-- remarks: | |
-- 1) you must add recursive word when using recursion in MariaDB | |
-- 2) you must SELECT from table, NOT FROM cte inside recursive part | |
-- https://mariadb.com/kb/en/recursive-common-table-expressions-overview/ | |
WITH RECURSIVE cte_employee AS | |
( | |
-- anchor part where we look for CEO who is on top of hierarchy | |
SELECT employee.*, 0 AS hierarchy_level FROM employee | |
WHERE manager_id = 0 | |
UNION ALL | |
SELECT e.*, cte_employee.hierarchy_level + 1 AS hierarchy_level | |
FROM employee AS e | |
-- here is where recursive part happens | |
-- you go down the hierarchy looking for eployees who's manager is the current person | |
-- so CEO (anchor) -> Marketing Directors (employee.manager_id = 7) -> Marketer (employee.manager_id = 8 OR employee.manager_id = 9) ... | |
INNER JOIN cte_employee ON (cte_employee.id = e.manager_id) | |
) | |
SELECT * FROM cte_employee; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP TABLE IF EXISTS `test`.`employee`; | |
CREATE TABLE `test`.`employee` ( | |
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT, | |
`name` VARCHAR(45) NULL, | |
`manager_id` INT UNSIGNED NOT NULL, | |
PRIMARY KEY (`id`)); | |
INSERT INTO `test`.`employee` (`name`, `manager_id`) VALUES ('Dev1', '10'); | |
INSERT INTO `test`.`employee` (`name`, `manager_id`) VALUES ('Dev2', '10'); | |
INSERT INTO `test`.`employee` (`name`, `manager_id`) VALUES ('Dev3', '11'); | |
INSERT INTO `test`.`employee` (`name`, `manager_id`) VALUES ('Dev4', '12'); | |
INSERT INTO `test`.`employee` (`name`, `manager_id`) VALUES ('Marketer1', '9'); | |
INSERT INTO `test`.`employee` (`name`, `manager_id`) VALUES ('Marketer2', '9'); | |
INSERT INTO `test`.`employee` (`name`, `manager_id`) VALUES ('CEO', '0'); | |
INSERT INTO `test`.`employee` (`name`, `manager_id`) VALUES ('Managers Director', '7'); | |
INSERT INTO `test`.`employee` (`name`, `manager_id`) VALUES ('Marketing Director', '7'); | |
INSERT INTO `test`.`employee` (`name`, `manager_id`) VALUES ('Manager1', '8'); | |
INSERT INTO `test`.`employee` (`name`, `manager_id`) VALUES ('Manager2', '8'); | |
INSERT INTO `test`.`employee` (`name`, `manager_id`) VALUES ('Manager3', '8'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
id | name | manager_id | hierarchy_level | |
---|---|---|---|---|
7 | CEO | 0 | 0 | |
8 | Managers Director | 7 | 1 | |
9 | Marketing Director | 7 | 1 | |
5 | Marketer1 | 9 | 2 | |
6 | Marketer2 | 9 | 2 | |
10 | Manager1 | 8 | 2 | |
11 | Manager2 | 8 | 2 | |
12 | Manager3 | 8 | 2 | |
1 | Dev1 | 10 | 3 | |
2 | Dev2 | 10 | 3 | |
3 | Dev3 | 11 | 3 | |
4 | Dev4 | 12 | 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment