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

  • Home
  • SEARCH
  • 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 8338577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:37:55+00:00 2026-06-09T04:37:55+00:00

I am trying to write a query which loops through a database starting at

  • 0

I am trying to write a query which “loops” through a database starting at a specified value until a condition is true. For example, suppose I have the following entries in TABLE example:

id, parent, cond
1,        , True
2, 1      , False
3, 1      , False
4, 2      , False
... ... ...

I want a query which takes as input (for instance) 4, and will return the values of 2 and 1. The process being that the query matches the id, and if cond==False, will look at the parent (id = 2). Since cond = False in the second row, the “parent” id will be selected (1). Looking at the first row now, since cond=True, the LOOP ends and returns 1 and 2.

I know that the query

SELECT parent FROM example WHERE id = 4;

will produce the parent id 2.

So my futile attempt at creating a LOOP:

WHILE (SELECT cond FROM example) = False
LOOP SELECT parent FROM example WHERE id = 4 
END LOOP;

First, this produces an error (“syntax error at or near ‘while'”). Second, I don’t know how to update the “id” after each iteration.

In a programming language like Python, I might use a variable initialized to 4 and then update it with each iteration…not sure how to do the equivalent in Postgres.

Let me know if you have any questions or require additional information. Thanks!

  • 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-06-09T04:37:58+00:00Added an answer on June 9, 2026 at 4:37 am

    Your thinking is wrong for SQL. Don’t think in terms of loops and conditions and variables; instead, think about how to describe the data you want. The tricky part is that you want the query to refer to its own results and that’s what recursive CTEs are for:

    The optional RECURSIVE modifier changes WITH from a mere syntactic convenience into a feature that accomplishes things not otherwise possible in standard SQL. Using RECURSIVE, a WITH query can refer to its own output.

    You’re looking for something like this:

    with recursive path as (
        select id, parent from T where id = 4
        union all
        select t.id, t.parent from T t join path p on t.id = p.parent
    )
    select id, parent
    from path
    

    That will give you this:

     id | parent 
    ----+--------
      4 |      2
      2 |      1
      1 |       
    

    and then you can put that back together in a path that would be more linked-listy (or whatever is appropriate in your client language) outside the database. You don’t have to include parent of course but including it will help you fix up the “pointers”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to write a query which will emit : the date/s which Overlapped
I'm pretty new to MYSQL. I'm trying to write a query which will search
I'm trying to write an HQL query which will calculate an average rating for
I am trying to write an hql query which gives me the number of
I'm trying to write a single query which will include one of the two
I'm trying to write a query to tell me which orders have valid promocodes.
I am trying to write an TUI bandwidth trace application which on query can
I am trying to write a Dynamic Query which uses a CTE. But I
I am trying to write a HQL Query which selectes rows from a table
I'm trying to write this query, that would calculate the average value of all

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.