Intermediate

SQL Basics

AicademyAicademy
·OCR GCSE Computer Science·OCR J277·6 min
2.2.3 Additional programming techniques

What Is SQL?

SQL (Structured Query Language) is a language used to search and retrieve data from a database. Rather than writing a loop in Python to scan every record, you write a single SQL statement that describes the data you want — the database engine finds it.

OCR J277 requires knowledge of three SQL keywords: SELECT, FROM, and WHERE.

OCR J277 scope: you need SELECT, FROM, and WHERE only. Other SQL commands (INSERT, UPDATE, DELETE, ORDER BY, JOIN) are not required.

All examples in this lesson use this Students table:

StudentIDNameAgeSubjectGrade
1Alice16MathsA
2Bob17EnglishB
3Carol16MathsB
4Dan17ScienceA
5Eve16EnglishA

SELECT and FROM — Choosing What and Where

SELECT specifies which columns to retrieve. FROM specifies which table to search.

Retrieve all columns from a table:

SELECT * FROM Students;

* means "all columns." This returns every field for every row in the Students table.

Retrieve specific columns only:

SELECT Name, Grade FROM Students;

Result:

NameGrade
AliceA
BobB
CarolB
DanA
EveA

Retrieve one column:

SELECT Name FROM Students;

Returns only the Name column for all five students.

SELECT * is useful for exploring data. For real applications, listing specific columns (SELECT Name, Grade) is preferred — it makes the query intent clear and avoids returning unnecessary data.

WHERE — Filtering Results

WHERE adds a condition that each row must satisfy to be included in the results. Only rows where the condition is true are returned.

Basic WHERE syntax:

SELECT columns FROM table WHERE condition;

Example 1 — retrieve all students aged 16:

SELECT * FROM Students WHERE Age = 16;

Result:

StudentIDNameAgeSubjectGrade
1Alice16MathsA
3Carol16MathsB
5Eve16EnglishA

Example 2 — retrieve names of students who achieved grade A:

SELECT Name FROM Students WHERE Grade = "A";

Result: Alice, Dan, Eve.

String values in WHERE conditions are enclosed in double or single quotes: WHERE Name = "Alice". Numbers are written without quotes: WHERE Age = 16.

Combining SELECT, FROM and WHERE

The three keywords work together as a single statement. The most useful queries specify both the columns to return and the rows to include.

Example — retrieve the name and subject of all 17-year-old students:

SELECT Name, Subject FROM Students WHERE Age = 17;

Result:

NameSubject
BobEnglish
DanScience

Example — retrieve all details of students studying Maths:

SELECT * FROM Students WHERE Subject = "Maths";

Result:

StudentIDNameAgeSubjectGrade
1Alice16MathsA
3Carol16MathsB

Worked example — a teacher wants names and grades of students in English:

SELECT Name, Grade FROM Students WHERE Subject = "English";

Result: Bob B, Eve A.

Want more lessons like this one?

Generate lessons on anything you study. Free account, no card needed.

Start generating

Multiple Conditions — AND and OR

WHERE conditions can be combined using AND and OR to filter on more than one criterion.

AND — both conditions must be true:

SELECT Name FROM Students WHERE Age = 16 AND Grade = "A";

Result: Alice, Eve. (Both are aged 16 and have grade A. Carol is 16 but grade B — excluded.)

OR — at least one condition must be true:

SELECT Name FROM Students WHERE Subject = "Maths" OR Subject = "Science";

Result: Alice, Carol, Dan. (Students studying Maths or Science.)

Combining AND and OR:

SELECT Name, Grade FROM Students WHERE Age = 17 AND Grade = "A";

Result: Dan only. (Bob is 17 but grade B.)

Use AND to narrow results (fewer rows returned), OR to broaden them (more rows returned). Both can be used in the same query.

SQL Worked Examples

Worked example 1 — find all Grade A students aged 16, return their names and subjects:

SELECT Name, Subject FROM Students WHERE Grade = "A" AND Age = 16;

Step-by-step:

  • SELECT Name, Subject → return only these two columns
  • FROM Students → search the Students table
  • WHERE Grade = "A" AND Age = 16 → include rows where grade is A and age is 16

Check each row:

  • Alice: Grade A ✓, Age 16 ✓ → included
  • Bob: Grade B ✗ → excluded
  • Carol: Grade B ✗ → excluded
  • Dan: Age 17 ✗ → excluded
  • Eve: Grade A ✓, Age 16 ✓ → included

Result: Alice (Maths), Eve (English).

Worked example 2 — retrieve all information about students who are either in Maths or aged 17:

SELECT * FROM Students WHERE Subject = "Maths" OR Age = 17;

Included: Alice (Maths ✓), Bob (Age 17 ✓), Carol (Maths ✓), Dan (Age 17 ✓). Eve: English, Age 16 — excluded.

Common Exam Mistakes

1. Forgetting quotes around string values in WHERE

WHERE Name = Alice is incorrect — the database looks for a column named Alice. Write WHERE Name = "Alice". Numbers do not use quotes: WHERE Age = 16.

2. Confusing AND and OR

WHERE Subject = "Maths" AND Subject = "Science" returns zero rows — no student can simultaneously be in both subjects. For "Maths or Science", use OR.

3. Misreading SELECT *

SELECT * returns all columns, not all rows. Filtering rows is done with WHERE. Without a WHERE clause, all rows are returned regardless.

4. Wrong capitalisation of keywords

SQL keywords (SELECT, FROM, WHERE, AND) are conventionally written in uppercase, but most databases accept lowercase. In exam answers, write them in uppercase to match the expected style.

MistakeCorrection
WHERE Name = AliceWHERE Name = "Alice"
WHERE Subject = "Maths" AND Subject = "Science"WHERE Subject = "Maths" OR Subject = "Science"
SELECT * FROM Students to get only namesSELECT Name FROM Students

Generate revision on any topic you study

Type any topic you're studying and Aicademy generates a complete lesson, quiz, and flashcard set — personalised to your level.

Lessons on anything

Structured, level-matched lessons on any topic you study

Practice quizzes

Find out what you actually know before the exam does

Flashcard sets

Lock in key concepts with instant revision cards

Ask Aica

Stuck on something? Get a clear explanation, any time

Prev

Arrays and Records

Next

Subroutines and Scope

Related lessons

7 Slides

Lesson

Data Types and Casting

OCR GCSE Computer Science · OCR J277

2 days ago

7 Slides

Lesson

Arrays and Records

OCR GCSE Computer Science · OCR J277

2 days ago

6 Slides

Lesson

Cyber Security — Threats and Prevention

OCR GCSE Computer Science · OCR J277

1 day ago