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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:33:56+00:00 2026-05-18T01:33:56+00:00

Someone I know went to an interview and was given the following problem to

  • 0

Someone I know went to an interview and was given the following problem to solve. I’ve thought about it for a few hours and believe that it’s not possible to do without using some database-specific extensions or features from recent standards that don’t have wide support yet.

I don’t remember the story behind what is being represented, but it’s not relevant. In simple terms, you’re trying to represent chains of unique numbers:

chain 1: 1  -> 2  -> 3
chain 2: 42 -> 78
chain 3: 4
chain 4: 7  -> 8  -> 9
...

This information is already stored for you in the following table structure:

id | parent
---+-------
1  | NULL
2  | 1
3  | 2
42 | NULL
78 | 42
4  | NULL
7  | NULL
8  | 7
9  | 8

There could be millions of such chains and each chain can have an unlimited number of entries. The goal is to create a second table that would contain the exact same information, but with a third column that contains the starting point of the chain:

id | parent | start
---+--------+------
1  | NULL   | 1
2  | 1      | 1
3  | 2      | 1
42 | NULL   | 42
78 | 42     | 42
4  | NULL   | 4
7  | NULL   | 7
8  | 7      | 7
9  | 8      | 7

The claim (made by the interviewers) is that this can be achieved with just 2 SQL queries. The hint they provide is to first populate the destination table (I’ll call it dst) with the root elements, like so:

INSERT INTO dst SELECT id, parent, id FROM src WHERE parent IS NULL

This will give you the following content:

id | parent | start
---+--------+------
1  | NULL   | 1
42 | NULL   | 42
4  | NULL   | 4
7  | NULL   | 7

They say that you can now execute just one more query to get to the goal shown above.

In my opinion, you can do one of two things. Use recursion in the source table to get to the front of each chain, or continuously execute some version of SELECT start FROM dst WHERE dst.id = src.parent after each update to dst (i.e. can’t cache the results).

I don’t think either of these situations is supported by common databases like MySQL, PostgreSQL, SQLite, etc. I do know that in PostgreSQL 8.4 you can achieve recursion using WITH RECURSIVE query, and in Oracle you have START WITH and CONNECT BY clauses. The point is that these things are specific to database type and version.

Is there any way to achieve the desired result using regular SQL92 in just one query? The best I could do is fill-in the start column for the first child with the following (can also use a LEFT JOIN to achieve the same result):

INSERT INTO dst
    SELECT s.id, s.parent,
        (SELECT start FROM dst AS d WHERE d.id = s.parent) AS start 
    FROM src AS s
    WHERE s.parent IS NOT NULL

If there was some way to re-execute the inner select statement after each insert into dst, then the problem would be solved.

  • 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-18T01:33:57+00:00Added an answer on May 18, 2026 at 1:33 am

    It can not be implemented in any static SQL that follows ANSI SQL 92.

    But as you said it can be easy implemented with oracle’s CONNECT BY

        SELECT id,
               parent,
               CONNECT_BY_ROOT id
          FROM table
    START WITH parent IS NULL
    CONNECT BY PRIOR id = parent
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Someone know some interesting and complete tutorial about how to create a Parent-Child dimension
Does someone know of a Sqlite manager that I can put on my site,
Long before I knew anything - not that I know much even now -
I know that for me I first got started following the waterfall method of
Someone know waht this address means ? i know that is somthing with remote
Does someone know if it is possible to modify the JVM settings at runtime
does someone know how to draw 3D surfaces and hide the invisible lines? I
What should someone know as an Entry Level, Mid-level, and Senior Developer working with
This StackOverflow answer has an image of KDiff3 highlighting intra-line differences. Does someone know
In jQuery ajax function there is xhr option. Does someone know more details, usability

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.