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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:11:03+00:00 2026-06-03T03:11:03+00:00

Have 2 tables for example: In 1st: object & parent columns object | parent

  • 0

Have 2 tables for example:

In 1st: object & parent columns

object | parent 
-------+---------
object1| null       
object2| object1  
object3| null  

2nd has: object & reference columns

object | reference
-------+---------
object1| null       
object2| null       
object3| object1       

Need to query tables to order like following: parent is first, then – child(s), objects which have reference(s) to parent.

object1
object2
object3

Is it possible to do in one SQL query or need to sort manually in an array? Seems it is a classical task, probably solution already exists somewhere?

  • 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-03T03:11:05+00:00Added an answer on June 3, 2026 at 3:11 am

    Following recursive query works:

    WITH RECURSIVE tables(object, rank) AS (
        SELECT DISTINCT o.object, 1 AS rank FROM oref o
        WHERE o.ref IS NULL
        UNION
        SELECT o.object, t.rank + 1 AS rank
        FROM (SELECT DISTINCT o.object, o.ref FROM oref o
                WHERE ref IS NOT NULL) o, tables t
        WHERE o.ref = t.object AND rank <= t.rank
    ),
    ordered AS (
        SELECT * FROM tables
    )
    SELECT * FROM tables 
    WHERE tables.rank = (SELECT MAX(rank) FROM ordered WHERE ordered.object = tables.object)
    ORDER BY rank;
    

    Any comments, questions, objections, propositions? 😉

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

Sidebar

Related Questions

I have 2 tables. 1st,cardb ID,car,engine,gear 2nd,labels ID,label,value what i need to do is,
In my database I have tables that define types for example Table: Publication Types
For example I have 2 tables, Users and UserRelations , and it is a
I have two tables, for example: Table A Table B ======= ======= Name |
I have 3 tables in my database, for example with such names: TableA, TableB,
Simplified for example, I have two tables, groups and items . items ( id,
I have some tables that benefit from many-to-many tables. For example the team table.
In my model, I have two tables with a 1:0..1 relationship between them: (Example
Rails noob here. I have a rails application with (in this example) three tables.
I have an example database, it contains tables for Movies, People and Credits. The

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.