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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:48:51+00:00 2026-06-05T23:48:51+00:00

Need help with a query that I wrote: I have three tables Company id

  • 0

Need help with a query that I wrote:

I have three tables

Company

id     name
1      Gary's

Employee

id     name         company_id
1      Tim Jones       1
2      Sam Adams       1

reports to

employee_id      reports_to_id
     1                 2    

My current query is:

select 
   temp.company.name as comp_name, 
   temp.employee.name as employee_name, 
   temp.employee.id as employee_id 
from temp.employee, temp.employee
where temp.company.id = temp.employee.company_id and temp.company.id = 1

Which gives me the output of:

comp_name    employee_name     employee_id
 Gary's       Tim Jones           1

I need something like this:

comp_name     employee_name    reports_to
Gary's         Tim Jones        Sam Adams

What’s a good way to modify my query to do this? I have a query and then I take those results and run a second query against that result set (which is excessively unnecessary).

  • 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-05T23:48:51+00:00Added an answer on June 5, 2026 at 11:48 pm

    Assuming an employee only reports to one person then we could have (no link table)

    Employee  (Id, Name, CompanyId, ReportsToId)
    Company (Id, Name)
    

    Then you could have a query similar to

    select e.Name EmployeeName, c.Name CompanyName, r.Name ReportsTo
    from
        Employee e
            inner join Company c on e.CompanyId = c.Id
            inner join Employee r on e.ReportsToId = r.Id
    where
        e.CompanyId = 1
    

    If the employee reports to multiple people then we would use a link table

    Employee  (Id, Name, CompanyId)
    EmployeeReportsTo (EmployeeId, ManagerId)
    Company (Id, Name)
    
    select e.Name EmployeeName, c.Name CompanyName, r.Name ReportsTo
    from
        Employee e
            inner join Company c on e.CompanyId = c.Id
            inner join EmployeeReportsTo ert on ert.EmployeeId = e.Id
            inner join Employee r on ert.ManagerId = r.Id
    where
        e.CompanyId = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need some help with a query.. I have three tables. Source id name 1
I need help with a MySQL query. I have three tables: `product_category` ( `id`
I need help extending a functional update query that performs calculations on one record
I need some help a query. For each movie in my database that has
need help/guide for sql select query, I have 2 table stock and stock_history, in
I need help writing a T-SQL query that will generate 52 rows of data
My goal is to get a query written. I have three tables, A, B
I'm somewhat new to SQL and need help with query syntax. My issue involves
I am new to SQL Server 2008, and I need some help in query
I need help with a specific LINQ query (I still suck at em'!) Background

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.