Spec & Goals 3 min
AQA Spec 3.7.2 — Retrieving data with SELECT … FROM
By the end of this lesson you can:
- Use
SELECTandFROMto retrieve chosen columns. - Use
SELECT *to retrieve all fields. - Predict the result of a simple query against a table.
Warm-Up 5 min
You can now design tables. Next: SQL (Structured Query Language) — the language used to ask a database questions and get data back.
Quick starter
If you wanted just the names of every student, which would you ask the database for — all the columns, or only the Name column?
Reveal the idea
Only the Name column. In SQL you SELECT the fields you want, FROM the table they live in.
Key Concept — asking for columns 14 min
SQL (Structured Query Language) is the standard language for retrieving and managing data in a relational database. The most common command retrieves data:
SELECT field1, field2 FROM tableName;
SELECT— the fields (columns) you want.FROM— the table they come from.*means all fields:SELECT * FROM Student;- End each statement with a semicolon
;.
Our Student table
| StudentID | Name | Form | Town | Mark |
|---|---|---|---|---|
| 101 | Aisyah | 10A | Ipoh | 78 |
| 102 | Wei Jie | 10B | Penang | 65 |
| 103 | Priya | 10A | Ipoh | 91 |
| 104 | Arjun | 11C | Johor Bahru | 54 |
| 105 | Mei Ling | 11C | Penang | 88 |
| 106 | Hafiz | 10B | Kuala Lumpur | 72 |
Example queries
SELECT Name, Town FROM Student; returns two columns for every row:
| Name | Town |
|---|---|
| Aisyah | Ipoh |
| Wei Jie | Penang |
| Priya | Ipoh |
| Arjun | Johor Bahru |
| Mei Ling | Penang |
| Hafiz | Kuala Lumpur |
Worked Example — write and predict 12 min
Problem: Write a query to list the StudentID and Mark of every student, then state the result.
SELECT StudentID, Mark FROM Student;
Result — two columns, all six rows:
| StudentID | Mark |
|---|---|
| 101 | 78 |
| 102 | 65 |
| 103 | 91 |
| 104 | 54 |
| 105 | 88 |
| 106 | 72 |
To return every field instead, write SELECT * FROM Student; — all five columns, all six rows.
Try It Yourself 12 min
Goal: Write a query to return only the Name of every student.
Goal: Write a query to return the Name, Form and Mark of every student.
Goal: State the difference between SELECT * and SELECT Name, and when you'd use each.
📝 Exam Practice 10 min
State what SELECT * returns.
Mark scheme
- All fields/columns (of the selected records) (1).
Using the Student table, write an SQL query to display the Name and Town of every student.
Mark scheme
SELECT Name, Town(1).FROM Student;(1).
Complete the query to return all fields of every student: SELECT ___ FROM Student;
Mark scheme
*(1).
Recap & Key Terms 3 min
SQL retrieves data with SELECT fields FROM table;. SELECT chooses the columns; FROM names the table; * returns all columns. With no filter, you get every row. The next lesson adds WHERE to pick rows.
- SQL
- Structured Query Language — used to retrieve and manage data in a relational database.
- SELECT
- The SQL keyword choosing which fields (columns) to return.
- FROM
- The SQL keyword naming the table to retrieve data from.
- SELECT *
- Returns all fields of the selected records.
Homework 1 min
Task (≤ 15 min): For a Product(ProductID, ItemName, Price) table, write three queries: (a) all fields; (b) just ItemName; (c) ItemName and Price.
Model answer
-- (a) SELECT * FROM Product; -- (b) SELECT ItemName FROM Product; -- (c) SELECT ItemName, Price FROM Product;
Award marks for: correct SELECT/FROM structure with semicolons (1); correct fields in each (2).