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 1085395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:41:01+00:00 2026-05-16T22:41:01+00:00

Suppose a table with two columns: From and To. Example: From To 1 2

  • 0

Suppose a table with two columns: From and To. Example:

From To
1    2
2    3
2    4
4    5

I would like to know the most effective way to locate all nodes that are reachable from a node using a SQL Query. Example: given 1 it would return 2,3,4 and 5. It is possible to use several queries united by UNION clauses but it would limit the number of levels that can be reached. Perhaps a different data structure would make the problem more tractable but this is what is available.

I am using Firebird but I would like have a solution that only uses standard SQL.

  • 1 1 Answer
  • 2 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-16T22:41:02+00:00Added an answer on May 16, 2026 at 10:41 pm

    You can use a recursive common table expression if you use most brands of database — except for MySQL and SQLite and a few other obscure ones (sorry, I do consider Firebird obscure). This syntax is ANSI SQL standard, but Firebird doesn’t support it yet.

    Correction: Firebird 2.1 does support recursive CTE’s, as @Hugues Van Landeghem comments.

    Otherwise see my presentation Models for Hierarchical Data with SQL for several different approaches.

    For example, you could store additional rows for every path in your tree, not just the immediate parent/child paths. I call this design Closure Table.

    From To   Length
    1    1    0
    1    2    1
    1    3    2
    1    4    2
    1    5    3
    2    2    0
    2    3    1
    2    4    1
    3    3    0
    4    4    0
    4    5    1
    5    5    0
    

    Now you can query SELECT * FROM MyTable WHERE From = 1 and get all the descendants of that node.

    PS: I’d avoid naming a column From, because that’s an SQL reserved word.

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

Sidebar

Related Questions

Suppose a simple ActiveRecord model corresponding to a table with just two columns, id
Suppose I have two columns in a table that represents a graph, the first
Suppose I have an oracle table with two columns: type varchar2 and data varchar2.
Suppose we have a PostgreSQL database with two tables A, B. table A columns:
Suppose I have a tags table with two columns: tagid and contentid . Each
I am working with SQLite. Suppose I have a table sales with two columns,
suppose i have two tables. articles and comments. when i am selecting columns from
Suppose there are two tables. Table X-- Columns: id x_value Table Y-- Columns: id
First of all I would like to know what is the actual root cause
Suppose that a table named SuplProd has the columns supplier and product and two

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.