How to Build a Location-Based Hierarchy in SQL | Recursive Query Tutorial

Опубликовано: 21 Май 2026
на канале: Analytics Hub
72
3

"In this video, we'll dive deep into SQL to build a hierarchy based on location data. We'll start by creating a table to store locations from continents to cities and then write a recursive query to display this hierarchy in a clear, ordered format. Whether you're managing a global retail business or just learning SQL, this tutorial will guide you through handling hierarchical data in your database. Don't forget to like, share, and subscribe for more SQL tutorials!"

This approach ensures that the viewer gets a clear understanding of how to manage and display hierarchical data using SQL. The recursive CTE method is particularly powerful for handling such data, and the video will provide a practical demonstration.

--You are employed by a retail company that has stores across India and overseas.
Your manager has now requested that you present the hierarchy according to location,
beginning with the name of the continent, as an ad hoc task. Please refine the query and show the outcomes.

CREATE TABLE Locations (
ID INT PRIMARY KEY,
Name VARCHAR(100),
ParentID INT
);
INSERT INTO Locations (ID, Name, ParentID)
VALUES
(1, 'World', NULL), -- Top-level node
(2, 'Asia', 1), -- Continent under 'World'
(3, 'Europe', 1), -- Continent under 'World'
(4, 'India', 2), -- Country under 'Asia'
(5, 'Japan', 2), -- Country under 'Asia'
(6, 'Germany', 3), -- Country under 'Europe'
(7, 'France', 3), -- Country under 'Europe'
(8, 'Delhi', 4), -- City under 'India'
(9, 'Tokyo', 5), -- City under 'Japan'
(10, 'Berlin', 6), -- City under 'Germany'
(11, 'Paris', 7); -- City under 'France'

select * from Locations

with CTE as (
select id,name,parentid, 1 As Level,cast((Name) as varchar(max)) as hierarchy
from Locations t1
where parentid is null

union all

select t2.id,t2.name,t2.parentid, cte.level+1 as level, cast((cte.hierarchy +'*** '+ t2.name) as varchar(max)) as hierarchy
from Locations t2 join cte on t2.parentid=cte.id)
select * from cte

SQL Interview Problem: Splitting Full Names into First, Middle, and Last Names
   • SQL Interview Problem: Splitting Full Name...  

Top SQL Interview Questions: RANK, DENSE_RANK, ROW_NUMBER Explained
   • Top SQL Interview Questions: RANK, DENSE_R...  

Top 3 Products by Sales & Employees by Salary | SQL Ranking Functions Explained
   • Top 3 Products by Sales & Employees by Sal...  

SQL Interview Challenge: Find Hidden Likes in Your Friends' Favorites!
   • SQL Interview Challenge: Find Hidden Likes...  

SQL Magic: Aggregating Marks & Delivery Dates with Advanced Queries!
   • SQL Magic: Aggregating Marks & Delivery Da...  

Top 20% vs Bottom 20% of Students in SQL: NTILE vs TOP PERCENT
   • Top 20% vs Bottom 20% of Students in SQL: ...  

SQL Tricks: Find the 3rd Lowest Salary in Each Department with Two Powerful Methods!
   • SQL Tricks: Find the 3rd Lowest Salary in ...  

SQL Tutorial: Find Employees Who Joined Before Their Managers
   • SQL Tutorial: Find Employees Who Joined Be...  

SQL Tutorial: Retrieve Specific Rows with OFFSET & ROW_NUMBER - Top 2 Methods Compared!
   • SQL Tutorial: Retrieve Specific Rows with ...  

#SQLTutorial
#HierarchicalData
#RecursiveCTE
#SQLQuery
#SQLfor Beginners
#SQLHierarchy
#DatabaseManagement
#SQLRecursiveQuery