Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6535343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:21:44+00:00 2026-05-25T10:21:44+00:00

I have a SQL table with the following structure. id – int par –

  • 0

I have a SQL table with the following structure.

id   - int
par  - int (relational to id)
name - varchar

Column par contains references to id or NULL if no reference, this table is meant to build an hierarchical tree.

Then, given the data:

id par  name

1  NULL John
2  NULL Mario
3  1    George
4  3    Alfred
5  4    Nicole
6  2    Margaret

I want to retrieve a hierarchical tree, up to the last parent, from a given single id.

Example, I want to know the tree from Nicole to the last parent. So the query result will be:

id par  name

5  4    Nicole
4  3    Alfred
3  1    George
1  NULL John

I would normally do this with a SQL query repeating over and over and building the tree server side but I do not want that now.

Is there any way to achieve this with a single SQL query?
I need this for either MySQL or PgSQL.

And I want to know also, if possible, is it also widely supported? In which versions of either MySQL or PgSQL can I expect support?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:21:44+00:00Added an answer on May 25, 2026 at 10:21 am

    It is possible with a single query in Postgres using a recursive common table expression. This is not possible in MySQL as it is one of the few database to not support recursive CTEs.

    It would look something like this (not tested)

    WITH RECURSIVE tree (id, par, name) AS (
        SELECT id, par, name 
        FROM the_table
        WHERE name = 'Nicole'
    
        UNION ALL
    
        SELECT id, par, name 
        FROM the_table tt
          JOIN tree tr ON tr.id = tt.par
    )
    SELECT *
    FROM tree
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ms-sql table with the following structure: Name nvarchar; Sign nvarchar; Value
I have an SQL table with the following structure: Code1 - int Code2 -
I have an SQL table with basically the following structure: PK (int, primary key),
I have the following pseudo-SQL schema: table flight id int primary key date timestamp
I have the following table structure in my database: create table Encargado( ID int
I have a organization name table with the following structure given below: CREATE TABLE
I have a SQL database table with the following structure: create table category_value (
I have following table structure ID Name Parent_ID 1 abc 0 2 efg 1
I have the following table structure: CREATE TABLE a ( a_id int(10) unsigned NOT
I have a Tickets table with somewhat following structure (removed unnecessary columns) int |

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.