Professional Documents
Culture Documents
BCIS 43012021 Lab Assignment 1 Spring 2023
BCIS 43012021 Lab Assignment 1 Spring 2023
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
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);
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';
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';
SELECT COUNT(*)
FROM Visits
JOIN Patients ON Visits.Patient_ID = Patients.Patient_ID
WHERE Patients.City = 'Scranton' AND YEAR(Visits.Visit_Date) = 2022;
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;
SELECT Diagnoses.Diagnosis
FROM Diagnoses
JOIN Diagnoses_Visits ON Diagnoses.Diagnosis_ID = Diagnoses_Visits.Diagnosis_ID
JOIN Visits ON Diagnoses_Vis