SQL (Structured Query Language) is the standard language used to create, query, and manage relational databases. For GCSE Computer Science, you need to write SELECT queries using WHERE, ORDER BY, and wildcard operators to retrieve specific data from one or more tables.
What is a relational database?
A relational database organises data into tables (also called relations). Each table has:
- Rows (records) — one entry for each item (e.g. one student, one book, one order).
- Columns (fields) — the attributes stored for each item (e.g. Name, Age, Subject).
- Primary key — a unique identifier for each row (e.g. StudentID, BookID).
- Foreign key — a field that links a row in one table to a row in another table.
Consider this sample table used in the examples below:
Students table:
| StudentID | FirstName | LastName | YearGroup | Grade |
|---|---|---|---|---|
| 1 | Amara | Osei | 10 | 7 |
| 2 | Ben | Cartwright | 11 | 5 |
| 3 | Cleo | Patel | 10 | 8 |
| 4 | Dan | Kim | 11 | 4 |
| 5 | Effy | Walsh | 10 | 6 |
How to write a basic SELECT query
The basic structure of a SQL SELECT query:
SELECT column1, column2
FROM tablename
WHERE condition
ORDER BY column;
Step 1 — SELECT specifies which columns to retrieve (use * to retrieve all columns).
Step 2 — FROM specifies the table.
Step 3 — WHERE (optional) filters rows matching a condition.
Step 4 — ORDER BY (optional) sorts the results.
Worked examples
Example 1: Retrieve all records
SELECT *
FROM Students;
Returns every row and every column from the Students table — all five records above.
Example 2: Retrieve specific columns
SELECT FirstName, LastName, Grade
FROM Students;
Returns only the name and grade columns for every student.
Example 3: Filter with WHERE
SELECT FirstName, LastName, Grade
FROM Students
WHERE YearGroup = 10;
Result:
| FirstName | LastName | Grade |
|---|---|---|
| Amara | Osei | 7 |
| Cleo | Patel | 8 |
| Effy | Walsh | 6 |
Only Year 10 students are returned.
Example 4: Multiple conditions with AND / OR
SELECT FirstName, Grade
FROM Students
WHERE YearGroup = 10 AND Grade >= 7;
Result:
| FirstName | Grade |
|---|---|
| Amara | 7 |
| Cleo | 8 |
Both conditions must be true (AND). If you used OR instead, a row matches if either condition is true.
Example 5: Wildcard search with LIKE
The % wildcard matches any sequence of characters:
SELECT FirstName, LastName
FROM Students
WHERE LastName LIKE 'P%';
Returns students whose last name begins with P:
| FirstName | LastName |
|---|---|
| Cleo | Patel |
'%on' would match names ending in "on"; '%art%' would match names containing "art" anywhere (e.g. Cartwright).
Example 6: Sorting results
SELECT FirstName, LastName, Grade
FROM Students
ORDER BY Grade DESC;
Result (highest grade first):
| FirstName | LastName | Grade |
|---|---|---|
| Cleo | Patel | 8 |
| Amara | Osei | 7 |
| Effy | Walsh | 6 |
| Ben | Cartwright | 5 |
| Dan | Kim | 4 |
Use ASC for ascending (lowest first — the default), DESC for descending (highest first).
Querying multiple tables with JOIN
When data is split across tables, a JOIN combines them. Suppose there is a second table:
Subjects table:
| StudentID | Subject |
|---|---|
| 1 | Computing |
| 3 | Computing |
| 2 | Maths |
SELECT Students.FirstName, Subjects.Subject
FROM Students
JOIN Subjects ON Students.StudentID = Subjects.StudentID
WHERE Subjects.Subject = 'Computing';
Result:
| FirstName | Subject |
|---|---|
| Amara | Computing |
| Cleo | Computing |
ON Students.StudentID = Subjects.StudentID is the join condition — it links matching rows across the two tables using the shared primary/foreign key.
Common SQL operators and keywords summary
| Keyword/Operator | Purpose | Example |
|---|---|---|
SELECT * |
Retrieve all columns | SELECT * FROM Students |
WHERE = |
Exact match | WHERE Grade = 7 |
WHERE > / >= |
Greater than / or equal | WHERE Grade >= 6 |
WHERE < / <= |
Less than / or equal | WHERE YearGroup <= 10 |
WHERE LIKE |
Pattern match | WHERE Name LIKE 'A%' |
AND |
Both conditions true | WHERE YG = 10 AND Grade > 5 |
OR |
Either condition true | WHERE YG = 10 OR YG = 11 |
ORDER BY ASC |
Sort ascending | ORDER BY Grade ASC |
ORDER BY DESC |
Sort descending | ORDER BY Grade DESC |
Frequently asked questions
What is the difference between WHERE and HAVING in SQL?
WHERE filters rows before any grouping occurs and is used with individual row conditions. HAVING filters groups after a GROUP BY clause — for example, finding subjects with more than 10 students enrolled. At GCSE, you are primarily expected to use WHERE. HAVING may appear in higher-tier contexts but is not the main focus of the standard specification.
Is SQL case-sensitive?
SQL keywords (SELECT, FROM, WHERE, etc.) are case-insensitive — you can write them in uppercase, lowercase, or mixed case and the database will behave the same. By convention, SQL keywords are written in UPPERCASE to make queries easier to read. The case-sensitivity of the data values themselves depends on the database system; string comparisons in some systems are case-sensitive (so 'computing' ≠ 'Computing'), while others are not.
What is a primary key and why is it important?
A primary key is a field (or combination of fields) that uniquely identifies each record in a table. No two rows can have the same primary key value, and a primary key cannot be NULL (empty). Primary keys are essential for maintaining data integrity and for linking tables together via foreign keys. In a Students table, using StudentID as the primary key is better than using Name, because two students could share the same name.
What does NULL mean in SQL?
NULL represents a missing or unknown value — it is not the same as zero or an empty string. A field is NULL when data has not been entered for that record. Checking for NULL uses IS NULL or IS NOT NULL (not = NULL): for example, WHERE Grade IS NULL finds students for whom no grade has been recorded. NULL values are excluded from aggregate functions like SUM and COUNT unless handled explicitly.
For Socratic GCSE Computer Science tutoring on databases, SQL, and data structures, visit aitutors.me.