Skip to content
Computer Science · 10th Grade · Advanced Data Structures and Management · Weeks 10-18

SQL Fundamentals: Querying Data

Students gain hands-on experience with SQL to query and retrieve data from relational databases.

Common Core State StandardsCSTA: 3A-DA-09

About This Topic

SQL (Structured Query Language) is the standard language for communicating with relational databases, and learning to write queries is one of the most practically transferable skills in a high school computer science curriculum. Students begin with SELECT statements to retrieve data, then build up to filtering with WHERE, grouping with GROUP BY, sorting with ORDER BY, and joining multiple tables. This topic aligns with CSTA standard 3A-DA-09 and connects directly to careers in data analysis, software engineering, and business intelligence.

Students often understand individual clauses in isolation but struggle to chain them together correctly or reason about execution order. The database processes a query in a specific logical sequence (FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY), and understanding this order explains many behaviors that otherwise seem arbitrary.

Hands-on query writing with real or realistic datasets is essential for building fluency. Active learning formats where students compare queries and explain their reasoning to peers accelerate understanding far more than watching demonstrations.

Key Questions

  1. Construct SQL queries to extract specific information from a database.
  2. Explain the purpose of WHERE, GROUP BY, and ORDER BY clauses.
  3. Evaluate the efficiency of different SQL queries for the same data retrieval task.

Learning Objectives

  • Construct SQL queries to retrieve specific data sets from a multi-table relational database.
  • Explain the logical execution order of SQL clauses (FROM, WHERE, GROUP BY, SELECT, ORDER BY) and its impact on query results.
  • Compare the performance and readability of different SQL query approaches to achieve the same data retrieval objective.
  • Critique SQL query syntax for correctness and adherence to best practices.
  • Design a simple database schema and populate it with data to answer specific business questions.

Before You Start

Introduction to Databases and Data Organization

Why: Students need a basic understanding of what a database is and how data is structured in tables before learning to query it.

Basic Programming Concepts (Variables, Data Types, Operators)

Why: Familiarity with data types and logical operators is helpful for understanding filtering conditions in WHERE clauses.

Key Vocabulary

SELECTThe SQL clause used to specify which columns to retrieve from a database table.
WHEREThe SQL clause used to filter records, returning only those that meet specified criteria.
GROUP BYThe SQL clause used to group rows that have the same values in specified columns into summary rows.
ORDER BYThe SQL clause used to sort the result set in ascending or descending order based on one or more columns.
JOINThe SQL clause used to combine rows from two or more tables based on a related column between them.

Watch Out for These Misconceptions

Common MisconceptionSQL reads left to right and top to bottom like regular code.

What to Teach Instead

SQL has a defined logical execution order that does not match the written order. SELECT is written first but evaluated near last; FROM and WHERE execute first. Students who understand execution order can predict results confidently and troubleshoot errors much faster. Query-tracing exercises make this sequence explicit.

Common MisconceptionGROUP BY and ORDER BY do the same thing.

What to Teach Instead

GROUP BY collapses multiple rows sharing a common value into a single summary row and enables aggregate functions like COUNT or SUM. ORDER BY sorts the output rows without changing their number or content. Presenting both against the same dataset side by side in a think-pair-share makes the distinction clear.

Active Learning Ideas

See all activities

Real-World Connections

  • Data analysts at Netflix use SQL to query user viewing habits, identifying popular genres and recommending new content to subscribers.
  • Software engineers at Amazon utilize SQL to manage product inventories, process customer orders, and track shipping logistics across their global fulfillment centers.
  • Business intelligence professionals at financial institutions query customer transaction data using SQL to detect fraudulent activity and analyze market trends.

Assessment Ideas

Quick Check

Provide students with a small, pre-defined database schema (e.g., students, courses, grades). Ask them to write a SQL query to find all students enrolled in a specific course and list their names in alphabetical order. Review queries for correct SELECT, FROM, WHERE, and ORDER BY clause usage.

Peer Assessment

Present students with a complex data retrieval task (e.g., 'Find the average grade for each subject, but only for students who have passed at least two courses'). Have students write two different SQL queries to solve it. Students then swap queries with a partner and evaluate: Which query is more efficient? Which is easier to read? Provide written feedback on clarity and correctness.

Exit Ticket

Ask students to write down the logical order in which a database processes the following SQL clauses: FROM, WHERE, GROUP BY, SELECT, ORDER BY. Then, have them briefly explain why understanding this order is important for writing effective queries.

Frequently Asked Questions

What does the WHERE clause do in SQL?
The WHERE clause filters rows before they are returned, keeping only rows where the specified condition is true. For example, WHERE grade > 90 returns only records with a grade above 90. It is applied early in the logical execution order, which is why you cannot reference column aliases defined in SELECT within a WHERE clause.
What is the difference between WHERE and HAVING in SQL?
WHERE filters individual rows before grouping occurs. HAVING filters groups after GROUP BY has been applied and aggregate functions have been calculated. Use WHERE to filter raw data and HAVING to filter summary results -- for example, HAVING COUNT(*) > 5 returns only groups containing more than five rows.
How do you retrieve data from two tables in SQL?
Use a JOIN clause to combine rows from two tables based on a matching column, typically a primary key and foreign key pair. An INNER JOIN returns only rows where the match exists in both tables. A LEFT JOIN returns all rows from the left table and matching rows from the right, with NULLs where no match exists.
How does active learning help students learn SQL?
Writing queries against real data and comparing results with peers exposes misconceptions that solo practice misses. When students explain why two queries produce different outputs or critique each other's solutions for efficiency, they build the analytical habits that make SQL fluency stick. Passive observation of demonstrated queries rarely produces the same depth of understanding.