AQA GCSE CSPaper 2 · Unit 7Lesson 4

Paper 2 · Unit 7 · CS-L7-04

SQL: SELECT & FROM

60 minutes · AQA 8525 · Paper 2 — Relational databases & SQL

Spec & Goals 3 min

AQA Spec 3.7.2 — Retrieving data with SELECT … FROM

By the end of this lesson you can:

  1. Use SELECT and FROM to retrieve chosen columns.
  2. Use SELECT * to retrieve all fields.
  3. 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

StudentIDNameFormTownMark
101Aisyah10AIpoh78
102Wei Jie10BPenang65
103Priya10AIpoh91
104Arjun11CJohor Bahru54
105Mei Ling11CPenang88
106Hafiz10BKuala Lumpur72

Example queries

SELECT Name, Town FROM Student; returns two columns for every row:

NameTown
AisyahIpoh
Wei JiePenang
PriyaIpoh
ArjunJohor Bahru
Mei LingPenang
HafizKuala 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:

StudentIDMark
10178
10265
10391
10454
10588
10672

To return every field instead, write SELECT * FROM Student; — all five columns, all six rows.

Try It Yourself 12 min

🟢 Easy

Goal: Write a query to return only the Name of every student.

🟡 Medium

Goal: Write a query to return the Name, Form and Mark of every student.

🔴 Stretch

Goal: State the difference between SELECT * and SELECT Name, and when you'd use each.

📝 Exam Practice 10 min

State[1 mark]

State what SELECT * returns.

Mark scheme
  • All fields/columns (of the selected records) (1).
Write a program[2 marks]

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[1 mark]

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).