You are on page 1of 4

Lab 1 Overview of Some Concepts We Will Study Throughout the Semester

Playing Pretend Database


Starting out, I want you to get a feel for how relational databases might work. We’ll come in through
the rest of the semester and talk about these concepts in a lot more detail but I want you to get a
rudimentary look at some database tables and how they are linked together. So I am going to give you
some basic rules and some basic explanations and then I am going to ask you to play the role of a
database using tables in the attached Excel file. Notice that there are four independent sheets in the
Excel file that are each going to represent one table in a patient database (bottom tabs labeled patient,
visit, diagnosis, and diagnosis_visit). This example is WAY oversimplified to represent a patient, the
visits they make to the doctor, and what diagnoses they receive on each visit.

Some basic things we will study throughout the semester and some brief explanations:

1. Each entry in a table must be unique so a lot of times an ID is assigned to identify each row in a
table. We’ll study more about PRIMARY KEYS later, but for now notice that Stanley Hudson has an ID of
6 to distinguish him from all other patients, even ones that also might be named Stanley Hudson.

2. We’ll work to avoid duplicating a lot of data in a database by breaking things into different tables and
then linking them through these unique identifiers. For example, we don’t want to enter Stanley
Hudson’s address for every visit he makes. If he visited the doctor 20 times in two years and we retyped
his address each time, not only would we waste a lot of time typing it over and over again, but we
would also take up a lot of space in our database storing the same info over and over again. So we move
his visit information to a separate table and link it via his patient ID.

3. Because each entry has to be unique, when we encounter things that have complex relationships,
we might have to break them down in to two or more entries. For example, if Stanley Hudson visits the
doctor on one day and has two things diagnosed, we would make two separate entries in the
visit_diagnoses table, one for each diagnosis tied to that visit. Then, the database would link everything
together through the shared fields to retrieve both diagnoses for that visit.

So in this assignment, you are going to play the role of the database and you are going to answer the
questions that I pose below. Here’s an example….

Practice question: Which patient(s) have been diagnosed with Arthritis? And here’s how I would attack
this problem. The only data I have is that I’m looking for Arthritis. So I go into the Diagnosis table and
see what the code is for Arthritis. In this case, it is 3. Now I need to know who was diagnosed with this
but I notice that diagnosis is tied to the visit, so I go in and scan the table to see that the diagnosis code
18 was associated with visits numbered 10 and 20. Now, I go into the visits table and look those visits
up. I see that visit 10 was patient #7 and visit 20 was patient #6. I can then use those codes to go into
the patient table and look to see that Stanley Hudson and Jim Halpert were the patients that were
diagnosed with Arthritis. Be sure and use the patient ID that is assigned to that row…..don’t confuse it
with the row number in Excel!
Use the tables provided to answer the following questions. Type your answers here and submit this
document by the due date on the assignment

1. List all patients that were diagnosed with allergies.

SELECT Patients.*
FROM Patients
JOIN Diagnoses_Visits ON Patients.Patient_ID = Diagnoses_Visits.Patient_ID
JOIN Diagn ON Diagn_Visits.Diagnosis_ID = Diagnosis.Diagnosis_ID
WHERE Diagn.Diagnosis = 'Allergies';

2. List all patients that visited during the months of June and July.

SELECT Patients.*
FROM Patients
JOIN Visits ON Patients.Patient_ID = Visits.Patient_ID
WHERE MONTH(Visits.Visit_Date) IN (6, 7);

3. What day(s) did a patient visit that had a heart attack?

SELECT Visit_Date
FROM Visits
JOIN Diagnoses_Visits ON Visits.Visit_ID = Diagnoses_Visits.Visit_ID
JOIN Diagnoses ON Diagnoses_Visits.Diagnosis_ID = Diagnoses.Diagnosis_ID
WHERE Diagnoses.Diagnosis = 'Heart Attack';

4. How many times did Sheldon Cooper visit the doctor?

SELECT COUNT(*)
FROM Visits
JOIN Patients ON Visits.Patient_ID = Patients.Patient_ID
WHERE Patients.First_Name = 'Sheldon' AND Patients.Last_Name = 'Cooper';
5. What was Dwight Schrute diagnosed with on each of his visits?

SELECT Diagnoses.Diagnosis
FROM Diagnoses
JOIN Diagnoses_Visits ON Diagnoses.Diagnosis_ID = Diagnoses_Visits.Diagnosis_ID
JOIN Visits ON Diagnoses_Visits.Visit_ID = Visits.Visit_ID
JOIN Patients ON Visits.Patient_ID = Patients.Patient_ID
WHERE Patients.First_Name = 'Dwight' AND Patients.Last_Name = 'Schrute';

6. How many visits did Scranton patients make in 2022?

SELECT COUNT(*)
FROM Visits
JOIN Patients ON Visits.Patient_ID = Patients.Patient_ID
WHERE Patients.City = 'Scranton' AND YEAR(Visits.Visit_Date) = 2022;

7. Who visited the doctor the most times?

WITH Patient_Visit_Count AS (
SELECT Patients.Patient_ID, COUNT(Visits.Visit_ID) AS Visit_Count
FROM Patients
JOIN Visits ON Patients.Patient_ID = Visits.Patient_ID
GROUP BY Patients.Patient_ID
)
SELECT First_Name, Last_Name
FROM Patients
JOIN Patient_Visit_Count ON Patients.Patient_ID = Patient_Visit_Count.Patient_ID
ORDER BY Patient_Visit_Count.Visit_Count DESC
LIMIT 1;

8. What diagnoses were made in the month of August?

SELECT Diagnoses.Diagnosis
FROM Diagnoses
JOIN Diagnoses_Visits ON Diagnoses.Diagnosis_ID = Diagnoses_Visits.Diagnosis_ID
JOIN Visits ON Diagnoses_Vis

9. Which female patients were diagnosed with anxiety?

SELECT Patients.First_Name, Patients.Last_Name F


ROM Patients JOIN Diagnosis_Visits ON Patients.Patient_ID = Diagnosis_Visits.Patient_ID
JOIN Diagnoses ON Diagnosis_Visits.Diagnosis_ID = Diagnoses.Diagnosis_ID
WHERE Diagnoses.Diagnosis = 'Anxiety' AND Patients.Gender = 'F';
10. Which patients visited the doctor more than two times?

SELECT Patients.First_Name, Patients.Last_Name


FROM Patients
JOIN Visits ON Patients.Patient_ID = Visits.Patient_ID
GROUP BY Patients.Patient_ID
HAVING COUNT(Visits.Visit_ID) > 2;

You might also like