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

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.…

Contents