Jump to content

Easy Query Return


programmerwannab

Recommended Posts

Hi Guys,

 

I'm completely new to MySQL but an assignment just popped on my desk.  Can you someone pleeeease help me out with this one.  This should be easy for the gurus I can't give the real data but here is my example:

 

 

Employees table emp (below) contains rows for regular employees as well as managers. Write the query to return the manager’s name for each employee name.

Table emp:

 

Emp_id Mgr_id Emp_name

    1     2         Alex     

    2     3         Bob     

    3   NULL       Rick     

    4     2         Tom     

    5     2         Laura   

 

 

Link to comment
Share on other sites

SELECT emp.Emp_name AS Employee, mgr.Emp_name AS Manager
FROM emp LEFT JOIN emp AS mgr
ON mgr.Emp_id=emp.Mgr_id

 

This will return the name of every employee with the name of their manager (except the manager name for Rick will obviously be a NULL value - this is why LEFT JOIN is used instead of INNER JOIN; if INNER JOIN is used, Rick would be omitted from the result set). E.g.

 

|--------------------|
| Employee | Manager |
|--------------------|
| Alex     | Bob     |
| Bob      | Rick    |
| Rick     | NULL    |
| Tom      | Bob     |
| Laura    | Bob     |
|--------------------|

Link to comment
Share on other sites

Hi Guys,

 

I'm completely new to MySQL but an assignment just popped on my desk.  Can you someone pleeeease help me out with this one.  This should be easy for the gurus I can't give the real data but here is my example:

 

 

Employees table emp (below) contains rows for regular employees as well as managers. Write the query to return the manager’s name for each employee name.

Table emp:

 

Emp_id Mgr_id Emp_name

    1     2         Alex     

    2     3         Bob     

    3   NULL       Rick     

    4     2         Tom     

    5     2         Laura   

 

 

 

 

But no manager’s name field in the given table...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.