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",
pr.name AS "Procedure",
u.date,
pt.name AS "Patient"
FROM physician p,
undergoes u,
patient pt,
PROCEDURE pr
WHERE u.patient = pt.SSN
AND u.procedure = pr.Code
AND u.physician = p.EmployeeID
AND NOT EXISTS
( SELECT *
FROM trained_in t
WHERE t.treatment = u.procedure
AND t.physician = u.physician );
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:

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.
SQL: Tips of the Day
Grouped LIMIT in PostgreSQL: Show the first N rows for each group?
db=# SELECT * FROM xxx; id | section_id | name ----+------------+------ 1 | 1 | A 2 | 1 | B 3 | 1 | C 4 | 1 | D 5 | 2 | E 6 | 2 | F 7 | 3 | G 8 | 2 | H (8 rows)
I need the first 2 rows (ordered by name) for each section_id, i.e. a result similar to:
id | section_id | name ----+------------+------ 1 | 1 | A 2 | 1 | B 5 | 2 | E 6 | 2 | F 7 | 3 | G (5 rows)
PostgreSQL v9.3 you can do a lateral join
select distinct t_outer.section_id, t_top.id, t_top.name from t t_outer join lateral ( select * from t t_inner where t_inner.section_id = t_outer.section_id order by t_inner.name limit 2 ) t_top on true order by t_outer.section_id;
Database: PostgreSQL
Ref: https://bit.ly/3AfYwZI
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook