w3resource

SQL Exercise: Doctors do the same procedure but are not certified

SQL hospital Database: Exercise-32 with Solution

32. From the following tables, write a SQL query to find all physicians, their procedures, the date when the procedure was performed, and the name of the patient on whom the procedure was performed, but the physicians are not certified to perform that procedure. Return Physician Name as "Physician", Procedure Name as "Procedure", date, and Patient. Name as "Patient".

Sample table: physician


Sample table: undergoes


Sample table: patient


Sample table: procedure


Sample table: trained_in


Sample Solution:


SELECT p.name AS "Physician", -- Selects the name column from the physician table and aliases it as "Physician"
       pr.name AS "Procedure", -- Selects the name column from the procedure table and aliases it as "Procedure"
       u.date, -- Selects the date column from the undergoes table
       pt.name AS "Patient" -- Selects the name column from the patient table and aliases it as "Patient"
FROM physician p, -- Specifies the physician table
     undergoes u, -- Specifies the undergoes table
     patient pt, -- Specifies the patient table
     procedure pr -- Specifies the procedure table
WHERE u.patient = pt.SSN -- Filters the rows where the patient SSN in the undergoes table matches the patient SSN in the patient table
  AND u.procedure = pr.Code -- Filters the rows where the procedure code in the undergoes table matches the procedure code in the procedure table
  AND u.physician = p.EmployeeID -- Filters the rows where the physician employee ID in the undergoes table matches the employee ID in the physician table
  AND NOT EXISTS -- Checks for the absence of a row in the subquery result
    ( SELECT * -- Subquery: Selects all columns
     FROM trained_in t -- Subquery: Specifies the trained_in table
     WHERE t.treatment = u.procedure -- Subquery: Filters the rows where the treatment in the trained_in table matches the procedure in the undergoes table
       AND t.physician = u.physician ); -- Subquery: Filters the rows where the physician in the trained_in table matches the physician in the undergoes table

Sample Output:

    Physician     |       Procedure       |        date         |  Patient
------------------+-----------------------+---------------------+------------
 Christopher Turk | Complete Walletectomy | 2008-05-13 00:00:00 | Dennis Doe
(1 row)	

Explanation:

The said query in SQL that retrieves the name of the physician, the procedure name and the date, and the name of the patient about procedures performed by physicians who have not been trained in that procedure.

The WHERE clause uses to join the tables 'undergoes' and 'patient' based on the patient and SSN columns, and the tables 'undergoes' and 'procedure' based on the procedure and code columns, and the tables 'undergoes' and 'physician' based on the physician and employee ID columns.

The NOT EXISTS keyword filters out the result of a subquery. The subquery searches the 'trained_in' table for a record with the same procedure code and physician ID as the current record in the 'undergoes' table. If a record is found, the subquery returns true and the current record is excluded from the result set.

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: Physicians, a medical procedure without certification.
Next SQL Exercise: Find all physicians who completed a medical procedure.

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.