Topic 5C – Database Operations

A well-designed database is ready to answer questions.
SQL is how you ask them.

Learning Objectives

By the end of this topic, you should be able to:

Learning Activities

To help you meet the learning objectives, we have prepared a combination of readings, activities, and videos.

Course Readings

These reading were designed to introduce the course topics to an audience of educators. They should be considered "required" and read in order.

Lesson Videos

These videos support the readings above and may present the material with some deeper connections and worked examples.

Activity

The material in this unit is much more understandable if you take some time to interact with a simple database and write some basic queries for yourself.  The w3schools website has a nice tool that allows you to interact with a SQL database right in your web browser.   

Checking for Understanding, Questions

The questions below use the following database. Read through the relations carefully before answering.

PATIENT relation
PatientIDLastNameFirstNameCity
P-001OseiKwameCedar Falls
P-002TorresElenaWaterloo
P-003JohanssonErikCedar Falls
PROVIDER relation
ProviderIDLastNameFirstNameSpecialty
D-10AbramowitzRachelPediatrics
D-11MensahKofiFamily Medicine
D-12WatanabeHanaPediatrics
APPOINTMENT relation
PatientIDProviderIDAppDateReason
P-001D-102024-11-04Annual checkup
P-002D-112024-11-05Follow-up
P-003D-102024-11-04Annual checkup
P-001D-122024-12-01Sick visit

Reading SQL Queries

  1. What question does this query answer?
    SELECT  LastName, FirstName
    FROM    Provider
    WHERE   Specialty = 'Pediatrics'
  2. What question does this query answer?
    SELECT  Patient.LastName, Patient.FirstName
    FROM    Patient, Appointment
    WHERE   Patient.PatientID = Appointment.PatientID
            AND Appointment.ProviderID = 'D-10'
  3. What question does this query answer?
    SELECT  Provider.LastName, Provider.FirstName, Provider.Specialty
    FROM    Patient, Appointment, Provider
    WHERE   Patient.PatientID = Appointment.PatientID
            AND Appointment.ProviderID = Provider.ProviderID
            AND Patient.City = 'Cedar Falls'
  4. In question 3's query, identify the two JOIN conditions in the WHERE clause and the one filter condition. Explain what each JOIN condition does.
  5. A hospital administrator wants to know the names of all patients who had a sick visit appointment. She is handed this query:
    SELECT  LastName, FirstName
    FROM    Patient
    WHERE   Reason = 'Sick visit'
    This query will not return the correct results. Explain why not, and describe what change would be needed to fix it.

Checking for Understanding, Answers

You can compare your answers to the following answer key.

Show Answer Key

Reading SQL Queries

  1. "What are the last and first names of all providers whose specialty is Pediatrics?" Using the sample data: the result would include Abramowitz/Rachel and Watanabe/Hana (both have Specialty = 'Pediatrics').
  2. "What are the last and first names of all patients who have had an appointment with provider D-10?" The WHERE clause has two parts: the JOIN condition (Patient.PatientID = Appointment.PatientID) links the two relations together, and the filter condition (Appointment.ProviderID = 'D-10') restricts the result to appointments with that specific provider. Using the sample data: the result would include Osei/Kwame (P-001, who saw D-10 on 2024-11-04) and Johansson/Erik (P-003, who also saw D-10 on 2024-11-04).
  3. "What are the names and specialties of all providers who have seen patients from Cedar Falls?" The query joins all three relations together and then filters to only those rows where the patient's city is Cedar Falls. Using the sample data: patients P-001 (Osei) and P-003 (Johansson) are from Cedar Falls. P-001 saw D-10 (Abramowitz, Pediatrics) and D-12 (Watanabe, Pediatrics). P-003 saw D-10 (Abramowitz, Pediatrics). The result would include Abramowitz and Watanabe (Pediatrics). Note: Abramowitz may appear twice in the raw result if not deduplicated.
  4. The two JOIN conditions:
    • Patient.PatientID = Appointment.PatientID — this links the PATIENT and APPOINTMENT relations by matching each appointment record to the patient it belongs to, using the shared PatientID key.
    • Appointment.ProviderID = Provider.ProviderID — this links the APPOINTMENT and PROVIDER relations by matching each appointment record to the provider who conducted it, using the shared ProviderID key.
    The one filter condition: Patient.City = 'Cedar Falls' — this restricts the result to only those rows (after joining) where the patient's city is Cedar Falls. It does not connect two relations; it filters out rows that do not meet the criterion.
  5. The query will fail because Reason is an attribute of the APPOINTMENT relation, not the PATIENT relation. The FROM clause specifies only Patient, so the database has no access to the Reason column — the query would produce an error (unknown column) or return no results. To fix it, the query must also include APPOINTMENT in the FROM clause and join the two relations on PatientID:
    SELECT  Patient.LastName, Patient.FirstName
    FROM    Patient, Appointment
    WHERE   Patient.PatientID = Appointment.PatientID
            AND Appointment.Reason = 'Sick visit'

Extend Your Learning

The following resources go a little deeper on topics we touched on but did not fully explore in the readings. These are entirely optional — none of this material appears on the Competency Demo — but each one is a natural "next question" from something covered this week.

  • Trying SQL yourself
    The best way to solidify SQL reading skills is to try writing a few queries yourself. SQLZoo offers an interactive browser-based SQL environment where you can run real queries against sample databases without installing anything. The first few exercises use SELECT, WHERE, and JOIN — exactly what this topic covers.
    SQL Basics — SQLZoo
  • The PROJECT and SELECT operations from relational algebra
    SQL's SELECT and WHERE clauses correspond to two formal operations from relational algebra: PROJECT (choosing columns) and SELECT (choosing rows). The readings introduced these ideas informally through SQL. This Khan Academy article explains the formal operations and how they map to SQL, which deepens understanding of why queries are structured the way they are.
    Splitting Data into Related Tables — Khan Academy
  • How schools actually use their student information systems
    Most school districts run a Student Information System (SIS) — a database that stores everything from enrollment and grades to attendance and bus assignments. This overview from CoSN (Consortium for School Networking) explains what SIS systems do and how they connect to the broader data ecosystem in a district, including interoperability challenges when systems do not share a common schema.
    Student Information Systems — CoSN