Skip to main content

Hierarchical and Recursive Queries in SQL Server

Learn how to write recursive queries and query hierarchical data structures.

Start Course for Free
4 Hours13 Videos47 Exercises7,705 Learners
3800 XP

Create Your Free Account



By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA. You confirm you are at least 16 years old (13 if you are an authorized Classrooms user).

Loved by learners at thousands of companies

Course Description

Do you want to query complex data structures in an iterative way? Do you have access to hierarchical data structures that need to be queried? This course will teach you the tools required to solve these questions. You will learn how to write recursive queries and query hierarchical data structures. To do this, you will use Common Table Expressions (CTE) and the recursion principle on a wide variety of datasets. You will, for example, dig into a flight plan dataset and learn how to find the best and cheapest connection between two airports. After completing this course, you will understand the principle of recursion, and be able to identify and create hierarchical data models.

  1. 1

    Recursion and Common Table Expression (CTE)


    In this chapter, you will learn about recursion and why it is beneficial to apply this technique. You will also refresh your knowledge about Common Expression Tables (CTE).

    Play Chapter Now
    Recap of Common Table Expressions (CTE)
    50 xp
    A CTE is ... Find the wrong fact!
    50 xp
    A CTE for IT-positions
    100 xp
    A CTE for high-paid IT-positions
    100 xp
    Introduction to recursion
    50 xp
    Facts about recursion.
    50 xp
    Calculate the factorial of 5
    100 xp
    How to query the factorial of 6 recursively
    100 xp
    Solve recursive maths problems
    50 xp
    Counting numbers recursively
    100 xp
    Calculate the sum of potencies
    100 xp
  2. 4

    Hierarchical Queries of Real-World Examples

    In this chapter, you will practice your learnings about hierarchical and recursive querying on real-world problems, such as finding possible flight routes, assembling a car, and modeling a power grid.

    Play Chapter Now


Power grid dataEmployee dataDepartment dataSalary dataBill of materials products dataBill of materials dataFlights data


Alex YaroshSara BillenMona Khalil
Dominik Egarter Headshot

Dominik Egarter

Data Engineering Enthusiast

Dominik Egarter is IT architecture manager & data engineering enthusiast at Kelag, a power utility company in Austria. He is responsible for the data and information architecture and leads several data science and data engineering projects in the fields of predictive maintenance, telecommunication networks, power grid networks, and customer insights. Dominik regularly uses R and SQL to implement algorithms and apps (R Shiny apps) which could be used to enforce new business insights and makes data easy to use for everyone.
See More

What do other learners have to say?

I've used other sites—Coursera, Udacity, things like that—but DataCamp's been the one that I've stuck with.

Devon Edwards Joseph
Lloyds Banking Group

DataCamp is the top resource I recommend for learning data science.

Louis Maiden
Harvard Business School

DataCamp is by far my favorite website to learn from.

Ronald Bowers
Decision Science Analytics, USAA