w3resource

SQL Exercise: Find the name and department of the physician

SQL hospital Database: Exercise-8 with Solution

8. From the following tables, write a SQL query to identify the physician and the department with which he or she is affiliated. Return Physician name as "Physician", and department name as "Department".

Sample table: physician
 employeeid |       name        |           position           |    ssn
------------+-------------------+------------------------------+-----------
          1 | John Dorian       | Staff Internist              | 111111111
          2 | Elliot Reid       | Attending Physician          | 222222222
          3 | Christopher Turk  | Surgical Attending Physician | 333333333
          4 | Percival Cox      | Senior Attending Physician   | 444444444
          5 | Bob Kelso         | Head Chief of Medicine       | 555555555
          6 | Todd Quinlan      | Surgical Attending Physician | 666666666
          7 | John Wen          | Surgical Attending Physician | 777777777
          8 | Keith Dudemeister | MD Resident                  | 888888888
          9 | Molly Clock       | Attending Psychiatrist       | 999999999
Sample table: department
 departmentid |       name       | head
--------------+------------------+------
            1 | General Medicine |    4
            2 | Surgery          |    7
            3 | Psychiatry       |    9

Sample table: affiliated_with
 physician | department | primaryaffiliation
-----------+------------+--------------------
         1 |          1 | t
         2 |          1 | t
         3 |          1 | f
         3 |          2 | t
         4 |          1 | t
         5 |          1 | t
         6 |          2 | t
         7 |          1 | f
         7 |          2 | t
         8 |          1 | t
         9 |          3 | t

Sample Solution:

SELECT p.name AS "Physician",
       d.name AS "Department"
FROM physician p,
     department d,
     affiliated_with a
WHERE p.employeeid=a.physician
  AND a.department=d.departmentid;

Sample Output:

     Physician     |    Department
-------------------+------------------
 John Dorian       | General Medicine
 Elliot Reid       | General Medicine
 Christopher Turk  | General Medicine
 Christopher Turk  | Surgery
 Percival Cox      | General Medicine
 Bob Kelso         | General Medicine
 Todd Quinlan      | Surgery
 John Wen          | General Medicine
 John Wen          | Surgery
 Keith Dudemeister | General Medicine
 Molly Clock       | Psychiatry
(11 rows)

Explanation:

The said query in SQL that selects the name of physicians and the department they are affiliated with from the physician table, department table, and affiliated_with table.

The query uses a join to combine data from the three tables. It selects the physician name from the physician table, department name from the department table, and uses the affiliated_with table to link the two. The affiliated_with table contains foreign keys to the physician and department tables, which are used to join the tables on their respective IDs.

The column headers will be labeled "Physician" and "Department", respectively.

Pictorial presentation:

Find the name of the physician and the departments they are affiliated with

Alternative Solutions:

Using INNER JOIN Syntax:


SELECT p.name AS "Physician",
       d.name AS "Department"
FROM physician p
JOIN affiliated_with a ON p.employeeid = a.physician
JOIN department d ON a.department = d.departmentid;

Explanation:

This query uses the INNER JOIN syntax to join the physician, affiliated_with, and department tables based on their respective IDs. It selects the physician name (p.name) and department name (d.name) in the result.

Using Explicit JOIN with ON Clause:


SELECT p.name AS "Physician",
       d.name AS "Department"
FROM physician p
INNER JOIN affiliated_with a ON p.employeeid = a.physician
INNER JOIN department d ON a.department = d.departmentid;

Explanation:

This query uses the INNER JOIN syntax to join the physician, affiliated_with, and department tables based on their respective IDs. It selects the physician name and department name in the result.

Practice Online


E R Diagram of Hospital Database:

E R Diagram: SQL Hospital Database.

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous SQL Exercise: Count the number of unavailable rooms.
Next SQL Exercise: Physicians who are trained in a special treatment.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.