DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How Trustworthy Is Big Data?
  • Fixing Common Oracle Database Problems
  • How to Restore a Transaction Log Backup in SQL Server
  • Why I Built the Ultimate Text Comparison Tool (And Why You Should Try It)

Trending

  • It’s Not About Control — It’s About Collaboration Between Architecture and Security
  • How to Configure and Customize the Go SDK for Azure Cosmos DB
  • Recurrent Workflows With Cloud Native Dapr Jobs
  • A Modern Stack for Building Scalable Systems
  1. DZone
  2. Data Engineering
  3. Databases
  4. Understanding Recursive Queries in Postgres

Understanding Recursive Queries in Postgres

Learn more about recursive queries in Postgres.

By 
Prasanna Venkataraman user avatar
Prasanna Venkataraman
·
Aug. 15, 19 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
23.4K Views

Join the DZone community and get the full member experience.

Join For Free

Postgres provides us a way to write queries that are recursive in nature and fetch the data until the termination condition is met. This can be achieved using Common Table Expressions (CTEs).

Using the WITH clause, we can create a temporary table scope of which is bound to that query itself. If we can combine this with the RECURSIVE clause, we can write some pretty cool stuff like finding the topmost parent, traveling salesman problem or as simple as printing the numbers recursively, as shown below:

Basic Recursive Query

First, we will see how this basic example works. This query has 3 parts,

SELECT 1 as n #line 2

This is the initial value, and our working table would be populated with this.

SELECT n + 1 FROM print_numbers WHERE n < 5 #line 4

This is the recursive part. As you can see, we are referring to the print_numbers itself and this would get values from the working table, which I mentioned. This query would continue running till the termination condition is met ie. n should be less than 5 and it is important to avoid infinite loops.

SELECT n FROM print_numbers #line 6

This is the actual query, which is doing nothing but selecting all the values of column n from print_numbers CTE.

Ok so now that we have the basics, I will show you 2 places where we are using these recursive queries.

1. Given an entity name, find all its ancestors

Schema Structure

| id |                     name | parent |
|----|--------------------------|--------|
|  1 |                Apple Inc | (null) |
|  2 |               Google Inc | (null) |
|  3 |              Apple India |      1 |
|  4 |             Google India |      2 |
|  5 |            Apple Chennai |      3 |
|  6 |          Apple Bangalore |      3 |
|  7 | Apple Chennai - Ascendas |      5 |

Ok. Now to the query. Given “Apple Chennai — Ascendas”, we have to fetch all its ancestors,

Output:

| id |                     name | parent |
|----|--------------------------|--------|
|  7 | Apple Chennai - Ascendas |      5 |
|  5 |            Apple Chennai |      3 |
|  3 |              Apple India |      1 |
|  1 |                Apple Inc | (null) |

Again, this should be straightforward with the initial value, termination condition, and the final select query. The only catch is about the recursive part, which I will explain. To understand it, we will start from the base query.

Base query result:

| id |                     name | parent |
|----|--------------------------|--------|
|  7 | Apple Chennai - Ascendas |      5 |

Refer to this SQL Fiddle to play with the query:

http://sqlfiddle.com/#!17/85aaa/3

But our use case was slightly more complicated than this, which we will see next.

2. Fetch all the entities with the topmost parent

This means our output should be something like entity_id and its corresponding topmost parent's id. So it would be:

| id | parent_id |
|----|-----------|
|  1 |         1 |
|  2 |         2 |
|  3 |         1 |
|  4 |         2 |
|  5 |         1 |
|  6 |         1 |
|  7 |         1 |

As I mentioned, we are appending the id at each level to path column (#line 5), and finally, we are getting the minimum element from the path column (#line 8) as the topmost parent id.

I hope this is useful in learning about common table expressions.

Thanks!

Database

Published at DZone with permission of Prasanna Venkataraman. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How Trustworthy Is Big Data?
  • Fixing Common Oracle Database Problems
  • How to Restore a Transaction Log Backup in SQL Server
  • Why I Built the Ultimate Text Comparison Tool (And Why You Should Try It)

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: