w3resource
SQL exercises

SQL exercises on hospital Database: Find the name of the patients with their physicians by whom they got their preliminary treatement

SQL hospital Database: Exercise-12 with Solution

12. Write a query in SQL to obtain the name of the patients with their physicians by whom they got their preliminary treatement.

Sample table: patient


Sample table: physician


Sample Solution:

SELECT t.name AS "Patient",
       t.address AS "Address",
       p.name AS "Physician"
FROM patient t
JOIN physician p ON t.pcp=p.employeeid;

Sample Output:

      Patient      |      Address       |    Physician
-------------------+--------------------+------------------
 John Smith        | 42 Foobar Lane     | John Dorian
 Grace Ritchie     | 37 Snafu Drive     | Elliot Reid
 Random J. Patient | 101 Omgbbq Street  | Elliot Reid
 Dennis Doe        | 1100 Foobaz Avenue | Christopher Turk
(4 rows)

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: Write a query in SQL to obtain the name of the physicians who are not a specialized physician.
Next: Write a query in SQL to obtain the name of the patients with their physicians by whom they got their preliminary treatement.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming