﻿ SQL: Names of all patients who had at least 2 appointments

# SQL Exercise: Names of all patients who had at least 2 appointments

## SQL hospital Database: Exercise-38 with Solution

38. From the following table, write a SQL query to find those patients with at least two appointments in which the nurse who prepared the appointment was a registered nurse and the physician who provided primary care should be identified. Return Patient name as "Patient", Physician name as "Primary Physician", and Nurse Name as "Nurse".

Sample table: appointment

Sample table: patient

Sample table: nurse

Sample table: physician

Sample Solution:

``````SELECT pt.name AS "Patient",
p.name AS "Primary Physician",
n.name AS "Nurse"
FROM appointment a
JOIN patient pt ON a.patient=pt.ssn
JOIN nurse n ON a.prepnurse=n.employeeid
JOIN physician p ON pt.pcp=p.employeeid
WHERE a.patient IN
(SELECT patient
FROM appointment a
GROUP BY a.patient
HAVING count(*)>=2)
AND n.registered='true'
ORDER BY pt.name;
``````

Sample Output:

```    Patient    | Primary Physician |      Nurse
---------------+-------------------+-----------------
Dennis Doe    | Christopher Turk  | Laverne Roberts
Grace Ritchie | Elliot Reid       | Carla Espinosa
Grace Ritchie | Elliot Reid       | Carla Espinosa
John Smith    | John Dorian       | Carla Espinosa
John Smith    | John Dorian       | Laverne Roberts
(5 rows)
```

Explanation:

The said query in SQL that selects the names of patients, their primary care physicians, and the names of the nurses who prepared for their appointments. The query only includes patients who have had at least two appointments and whose nurses are registered.

The query uses JOIN statements to link the tables based on the relevant columns. The JOIN statement combines the 'appointment' and 'patient' tables based on the patient and ssn columns. The second JOIN statement combines the 'appointment' and 'nurse' tables based on the presnurse and employeeid columns and the third JOIN statement links the 'patient' and 'physician' tables based on the pcp and employeeid columns.

With the first condition in WHERE clause, only patients with at least two appointments are included in the output, based on a subquery that groups and filters appointments by patient and the second condition ensures that only nurses who are registered are included in the output.

## 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: Patients who have had a procedure costing over \$5,000.
Next SQL Exercise: Providers of primary care who are not department heads.

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

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