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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:15:42+00:00 2026-05-31T03:15:42+00:00

I have a table named as employee employee table fields and sample values ——————————–

  • 0

I have a table named as employee

employee table fields and sample values

--------------------------------
id      name     parentid
--------------------------------
1       suresh    null
--------------------------------
2       ram       1
--------------------------------
3       ravi      2
--------------------------------
4       raju      3
--------------------------------

Here I need to take the name field value of current, parent and grand parent values for given id value. I need the output as below

Assume the input id is 4 then my expected output is

name    parentName    grandParentName
-----------------------------------------
raju    ravi           ram
-----------------------------------------

How can I write query for 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-05-31T03:15:43+00:00Added an answer on May 31, 2026 at 3:15 am

    Good question! This is tested and works.

    select my.`name`, p.`name` as 'parent name', gp.`name` as 'grandparent name'
    from employee my 
    left outer join employee p on p.id=my.parentid 
    left outer join employee gp on gp.id = p.parentid
    where my.`id`=4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table called Employee with the following fields: EmpID Salary Name I
I have a table named categories, which contains ID(long), Name(varchar(50)), parentID(long), and shownByDefault(boolean) columns.
I have a table called employee with following fields Id | Name | PrimaryEmail1
i have an employee table with fields like id, name, age and salary. Iam
I have a table which has employee relationship defined within itself. i.e. EmpID Name
Say I have a table named EMPLOYEE, and the table has a field called
I have a mysql table EMPLOYEE +-------------------+--------------+------+-----+---------+-------+ | Field | Type | Null |
I have a legacy table containing employee name and employee id. Now the employee
I have a table (EmpInfo) where employee info stored. Here are the fields: EMPLID
I have two tables, both named as say, Employee in two different schema HR

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.