TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2511 2.27K
๐Ÿ”ฅ Now, Letโ€™s move to the next topic:

โœ… Recursive CTEs

One of the most advanced and impressive SQL topics for interviews ๐Ÿ’ฏ

๐Ÿง  1. What is a Recursive CTE?

A Recursive CTE is a CTE that refers to itself.

๐Ÿ‘‰ Used for hierarchical or recursive data

Examples:

โœ” Employee-Manager hierarchy

โœ” Organization chart

โœ” Folder structure

โœ” Category trees

โšก 2. Structure of Recursive CTE

A Recursive CTE has two parts:

1๏ธโƒฃ Anchor Query

Starting point

2๏ธโƒฃ Recursive Query

Repeats until condition is met

๐Ÿ”ฅ 3. Basic Example โ€“ Generate Numbers 1 to 5

WITH RECURSIVE Numbers AS (
SELECT 1 AS num
UNION ALL
SELECT num + 1
FROM Numbers
WHERE num < 5
)

SELECT * FROM Numbers;


โœ… Output

num

1

2

3

4

5

๐Ÿ”ฅ 4. Employee Hierarchy Example

Employees Table

emp_id | name | manager_id

1 | CEO | NULL

2 | Amit | 1

3 | Neha | 2

4 | Ravi | 2

Recursive Query

WITH RECURSIVE EmployeeHierarchy AS (
SELECT
emp_id,
name,
manager_id,
1 AS level
FROM employees
WHERE manager_id IS NULL
UNION ALL
SELECT
e.emp_id,
e.name,
e.manager_id,
eh.level + 1
FROM employees e
JOIN EmployeeHierarchy eh
ON e.manager_id = eh.emp_id
)

SELECT * FROM EmployeeHierarchy;


๐ŸŽฏ 5. Real-World Uses

Organizational Charts

CEO โ†’ Manager โ†’ Employee

Product Categories

Electronics โ†’ Laptop โ†’ Gaming Laptop

Folder Structures

Root โ†’ Folder โ†’ Subfolder

โšก 6. Important Rule

Every recursive CTE needs:

โœ” Anchor Query

โœ” Recursive Query

โœ” Stopping Condition

Without stopping condition โŒ Infinite loop

๐ŸŽฏ 7. Practice Tasks

1. Generate numbers 1โ€“10

2. Generate even numbers

3. Build employee hierarchy

4. Find reporting levels

5. Create category tree

โšก Mini Challenge ๐Ÿ”ฅ

๐Ÿ‘‰ Generate multiplication table of 5 (5 to 50) using Recursive CTE

Example:

5

10

15

20

...

50

Most asked question:

๐Ÿ‘‰ Difference between CTE and Recursive CTE?

โœ… CTE = Temporary result set

โœ… Recursive CTE = Temporary result set that references itself

Double Tap โค๏ธ For More
  • โค 7
  • ๐Ÿ‘ 1
More from @sqlanalyst
  1. Oct 9, 2026SQL Interview Series โ€” Part 5 ๐Ÿ“Œ Question 5: Find Employees Who Earn More Than Their Managโ€ฆ
  2. Oct 9, 2026๐Ÿ‡ฎ๐Ÿ‡ณ ๐—š๐—ข๐—ฉ๐—˜๐—ฅ๐—ก๐— ๐—˜๐—ก๐—ง ๐—ข๐—™ ๐—œ๐—ก๐——๐—œ๐—” โ€” ๐—”๐—œ๐—–๐—ง๐—˜ ๐—œ๐—ก๐—ง๐—˜๐—ฅ๐—ก๐—ฆ๐—›๐—œ๐—ฃ๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€โ€ฆ
  3. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  4. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  5. Oct 7, 2026SQL Interview Series โ€” Part 4 ๐Ÿ“Œ Question 4: Find the Highest Salary in Each Department Suโ€ฆ
  6. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’