Learning Objectives
By the end of this topic, you should be able to:
- Identify the purpose of the SELECT, FROM, and WHERE clauses in a SQL query.
- Given a SQL query operating on a described database, state in plain English the question the query is answering.
- Identify the JOIN condition in a multi-relation SQL query and explain what it does.
- Distinguish between JOIN conditions and filter conditions in the WHERE clause of a SQL query.
- Explain how SQL querying connects to Stage 4 (analyze and explore) of the data investigation cycle.
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.
- Reading 1 – Selecting and Filtering — the SELECT, FROM, and WHERE clauses and how to read single-relation queries
- Reading 2 – Joining Relations — combining multiple relations with JOIN to answer questions that span tables
- Reading 3 – Reading Queries in Practice — extended practice with a new database scenario and a closing connection to the investigation cycle
Lesson Videos
These videos support the readings above and may present the material with some deeper connections and worked examples.
- VIDEO: Database Queries in Action
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.
| PatientID | LastName | FirstName | City |
|---|---|---|---|
| P-001 | Osei | Kwame | Cedar Falls |
| P-002 | Torres | Elena | Waterloo |
| P-003 | Johansson | Erik | Cedar Falls |
| ⋮ | |||
| ProviderID | LastName | FirstName | Specialty |
|---|---|---|---|
| D-10 | Abramowitz | Rachel | Pediatrics |
| D-11 | Mensah | Kofi | Family Medicine |
| D-12 | Watanabe | Hana | Pediatrics |
| ⋮ | |||
| PatientID | ProviderID | AppDate | Reason |
|---|---|---|---|
| P-001 | D-10 | 2024-11-04 | Annual checkup |
| P-002 | D-11 | 2024-11-05 | Follow-up |
| P-003 | D-10 | 2024-11-04 | Annual checkup |
| P-001 | D-12 | 2024-12-01 | Sick visit |
| ⋮ | |||
Reading SQL Queries
-
What question does this query answer?
SELECT LastName, FirstName FROM Provider WHERE Specialty = 'Pediatrics' -
What question does this query answer?
SELECT Patient.LastName, Patient.FirstName FROM Patient, Appointment WHERE Patient.PatientID = Appointment.PatientID AND Appointment.ProviderID = 'D-10' -
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' - 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.
- A hospital administrator wants to know the names of all patients who had a sick visit appointment. She is handed this query:
This query will not return the correct results. Explain why not, and describe what change would be needed to fix it.SELECT LastName, FirstName FROM Patient WHERE Reason = 'Sick visit'
Checking for Understanding, Answers
You can compare your answers to the following answer key.
Show Answer Key
Reading SQL Queries
- "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').
-
"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). - "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.
-
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.
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. -
The query will fail because
Reasonis an attribute of the APPOINTMENT relation, not the PATIENT relation. The FROM clause specifies onlyPatient, 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