From the course: Intermediate SQL for Data Scientists
Unlock the full course today
Join today to access over 23,200 courses taught by industry experts.
Hierarchical tables
From the course: Intermediate SQL for Data Scientists
Hierarchical tables
- [Instructor] CTEs allow us to work with hierarchical recursive data. And so, let's take a look at an example. Here, we have a simple organization structure where we have three levels in the hierarchy, and we imagine that the top level, or the root of the tree is called CEO Office. And there are two subordinate departments, VP Sales, VP Operations. And then, we also have below those, we have a couple of other departments, so such as Southeast Sales and Northwest Sales under the VP Sales Office, and then under the Operations, we have Infrastructure Operations and Management Operations. Now, if we wanted to query over data that was hierarchical like this, we would use the with recursive statement. And, basically, the syntax is as follows. We use with recursive, specify a table name, and then a series of columns that are going to be in that table that we're going to be referencing. And then, we specify two select statements.…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
(Locked)
Introduction to common table expressions (CTEs)58s
-
(Locked)
Multiple table common table expressions4m 16s
-
(Locked)
Hierarchical tables2m 32s
-
(Locked)
Recursive common table expressions3m 54s
-
(Locked)
Challenge: Rewrite a complex query to use CTEs35s
-
(Locked)
Solution: Rewrite a complex query to use CTEs29s
-
(Locked)
-