1

Working on an assignment for school, was able to figure out all but this one.

I need to display the employee name and employee identification number along with their HOD's name and identification number. The table is structured like this:

(TABLE NAME): EMPLOYEES

EMPLOYEE_ID  
EMPLOYEE_NAME  
HOD (which is an EMPLOYEE_ID)

1 Answer 1

3

In SQL, you can join a table to itself. Treat the two sides of the join as two separate tables and you're on your way to solving it.

3
  • I've done a few joins with two different tables. My brain isn't quite getting the joining to itself.
    – R Buckley
    Commented Dec 19, 2013 at 21:26
  • Do you talk about the specific join of this task or joins in general? Commented Dec 20, 2013 at 9:22
  • Also You can use a correlated subquery in the select-list. SELECT EMPLOYEE_ID, EMPLOYEE_NAME, HOD, (SELECT EMPLOYEE_NAME FROM EMPLOYEES WHERE EMPLOYEE_ID = A.EMPLOYEE_ID) HOD_NAME FROM EMPLOYEE A; But I'm sure your teacher it's trying to teach you the simple self-join way
    – vegatripy
    Commented Dec 23, 2013 at 15:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.