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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:24:32+00:00 2026-06-15T03:24:32+00:00

This was a question which I was asked in an interview recently: You have

  • 0

This was a question which I was asked in an interview recently:

You have various types of users in an organization : Junior (is bossed by) Supervisor (is bossed by) Manager (is bossed by) CEO.

We agreed upon this single simplified table schema. Users : {userId, userName, UserType(J, S, M, C), bossUserId}

Q : Write a single query to determine the complete organizational hierarchy of a Junior employee with userId = 11.

Sample Data :

enter image description here

The answer would be => PQR2 --> GHI2 --> DEF1 --> ABC1

This was my solution :

select e1.userName, e2.userName, e3.userName, e4.userName from 
abc e1 inner join users e2 on e1.bossUserId = e2.userId
inner join users e3 on e2.bossUserId = e3.userId
inner join users e4 on e3.bossUserId = e4.userId
where e1.userId = 11;

I do realize that self joining 4 times is horrible, but I was not able to think of anything else. The interviewer told me that there is a better way and that the data will show up columnwise. (using a maximum of 2 self joins, if at all)

Another possibility was to write a stored procedure, but again it is not a single query.

Can anyone help me with this?

  • 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-15T03:24:33+00:00Added an answer on June 15, 2026 at 3:24 am

    Following your comments, in MS SQL (and others, similarly) you can do this

    ;with cte as (
        select *, 0 as level from yourtable
        union all 
        select cte.id, t2.name, t2.ut, t2.bossid, level+1
        from cte
            inner join yourtable t2 on cte.bossid = t2.id
    )
        select name, ut from cte
        where id=11
        order by level
    

    which gives you

    pqr2    j
    ghi2    s
    def1    m
    abc1    c
    

    But MySQL doesn’t support this construct.

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

Sidebar

Related Questions

This question was recently asked to me in an interview for which i went
This is a question I was asked at my interview recently: Which 'Random' object(s)
This question was asked to me in an interview: Lets say you have a
This is the interview question asked. Which is the most important factor while drawing
Recently, I have been asked a question in an interview what's the difference between
This is a interview Question which was asked and wanted to find an efficient
This question was asked to me during company interview - Which data-structure is efficient
In an interview this is the question that is asked: You have to write
Recently i had an interview in which the question was asked as How would
I was recently asked this question in an interview. Even though I was able

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.