O Level · Computer Science 0478

SQL

Revise SQL for Computer Science 0478 (O Level) — revision notes and instant AI marking. Free to start.

📖 Revision notes · preview
  Cambridge (CIE) IGCSE Computer Science

SQL — Structured Query Language

SQL is the language you use to ask a database questions — "give me these rows," "only if they match this condition," "sorted this way," "add these numbers up." Once you can read a table and translate "what I want" into SELECT / FROM / WHERE, you can write almost any query this course throws at you.

  Summary — What This Chapter Covers

  • SQL is a programming language used to interact with a DBMS (Database Management System).
  • SQL lets you select, order, sum, and count data.
  • SELECT and FROM are the two commands that appear in almost every query — SELECT says what columns you want, FROM says which table.
  • WHERE filters rows based on a condition; AND / OR combine multiple conditions.
  • Wildcards (* and %) let you match "everything" or "partial text" like names starting with a letter.
  • ORDER BY sorts your results (ASC = ascending, DESC = descending).
  • SUM adds up a numeric column; COUNT counts how many rows match.

  What is SQL?

Think of a database table like a giant spreadsheet sitting inside a piece of software called a DBMS (Database Management System) — think MySQL, Oracle, or Microsoft Access working behind the scenes. You can't just "look" at the spreadsheet and pick out rows by eye when it has millions of entries. Instead, you write a short, precise instruction in SQL, and the DBMS does the searching for you and hands back exactly what you asked for.

SQL stands for Structured Query Language. The word "structured" matters — SQL queries always follow a predictable shape (SELECT ... FROM ... WHERE ...), a bit like a sentence template. Once you know the template, you're just filling in the blanks with your own table and column names.

Analogy
Imagine a librarian with a card catalogue of a million books. Instead of walking every aisle yourself, you tell the librarian: "Find me books, written after 1990, by authors starting with 'K', sorted by title." That instruction — precise, structured, filtered, sorted — is exactly what an SQL query does to a database table.

SQL allows a user to do four main things with data:

  • Select data — pull out specific rows/columns
  • Order data — sort the results
  • Sum data — add up a numeric column
  • Count data — count how many rows match

  SELECT and FROM — The Core Pair

Every SQL query that retrieves data starts with these two commands working together. SELECT tells the DBMS which columns you want back. FROM tells it which table to look in. You genuinely cannot write one without the other for a basic query — SELECT on its own doesn't know where to look, and FROM on its own doesn't know what to return.

General Pattern SELECT column1, column2, ... FROM tableName;

If you want every column in the table, you don't have to type each one out — you use the wildcard * to mean "all columns."

SELECT * FROM users;
-- retrieves ALL columns from the 'users' table

SELECT name, age
FROM users;
-- retrieves only the 'name' and 'age' columns
Remember
Column names go straight after SELECT, separated by commas. The table name goes after FROM. No commas needed there unless you're joining multiple tables (which isn't covered in this chapter).
Practice Question 1

Write an SQL command to retrieve the Name and Country columns only, from a table called Customers.

Practice Question 2

Write an SQL command to retrieve every column from a table called tbl_people.

  WHERE, AND & OR — Filtering Data

SELECT and FROM get you data — but usually you don't want every row, you want specific ones. That's the job of WHERE. It filters rows based on a condition, so only the rows that satisfy that condition are returned.

Pattern SELECT columns FROM table WHERE condition;
SELECT * FROM users
WHERE age > 30;
-- only returns users older than 30

Combining conditions: AND vs OR

Often one condition isn't enough — you want rows that satisfy two conditions at once (AND), or rows that satisfy either one (OR). These two keywords behave very differently, and mixing them up is one of the most common exam mistakes.

KeywordMeaningExample
AND Row must satisfy both conditions to be included WHERE age > 18 AND city = 'New York'
→ must be over 18 and from New York
OR Row is included if it satisfies at least one condition WHERE age < 18 OR city = 'New York'
→ under 18, or from New York, or both
Common Trap
Students often think AND will return more rows because you're "adding more conditions." It's the opposite — AND is stricter (narrows results), OR is looser (widens results). If you AND two conditions that contradict each other (e.g. age > 30 AND age < 20), you'll get zero rows back.
Practice Question

Write a query to select all fields from Customers where the customer is from 'London' or 'Paris'.

  Wildcards — * and %

SQL has two wildcard symbols, and it's easy to muddle them because they look similar in purpose but are used in completely different places.

SymbolUsed whereMeaning
* Straight after SELECT "Give me all columns" — a whole-table wildcard
% Inside a LIKE condition in WHERE "Match any characters here" — a partial-text wildcard
SELECT * FROM users
WHERE name LIKE 'J%';
-- retrieves users whose name STARTS WITH 'J' (Jane, John, Jake...)

Read 'J%' as: "starts with J, then anything at all can follow." If you wanted names that contain "an" anywhere, you'd write '%an%' — a % on both sides means "anything before, anything after."

Quick way to remember
* = "star of the whole table" (used with SELECT, means all columns).
% = "percentage of a word" (used with LIKE, means partial text match).

  ORDER BY — Sorting Results

By default, a DBMS doesn't guarantee any particular order for the rows it returns. ORDER BY lets you control exactly how the results are sorted — alphabetically, numerically, ascending or descending, and even by more than one column at once (useful for things like "sort by surname, then by first name for people who share a surname").

Pattern SELECT columns FROM table ORDER BY columnName ASC/DESC;
SELECT Forename, Lastname FROM Students
WHERE StudentID < 10
ORDER BY Lastname, Forename ASC;
-- retrieves forename & lastname of students with ID under 10,
-- sorted ascending by lastname, then by forename as a tie-breaker

ASC = ascending (A→Z, or smallest→largest number). DESC = descending (Z→A, or largest→smallest). If you don't specify either, ASC is usually the default — but in an exam, always write it explicitly to show you know what you're doing.

Multiple sort columns
When you list two columns after ORDER BY, the second one only matters as a tie-breaker for rows where the first column is identical. In the example above, students are sorted by lastname first — but if two students share a lastname, their forenames decide who comes first.
Practice Question

Write a query to select the Name and Age from Customers, sorted by Age in descending order (oldest first).

  SUM and COUNT — Aggregate Functions

Sometimes you don't want individual rows back at all — you want a single number that summarises the data. SUM and COUNT are "aggregate functions" — they take many rows and collapse them into one answer.

FunctionWhat it doesExample
SUM(column) Adds up all the values in a numeric column SELECT SUM(Salary) FROM tbl_people;
→ returns one number: the total of every salary
COUNT(*) Counts how many rows match the criteria SELECT COUNT(*) FROM tbl_people WHERE Salary > 50000;
→ returns one number: how many people earn over 50,000
Key distinction
SUM adds up the values in a column (needs a numeric column, e.g. Salary). COUNT just counts how many rows exist — it doesn't care what's in them, which is why you use COUNT(*) rather than naming a specific column.
Practice Question

Write a query to count how many customers in the Customers table are older than 25.

  Full Worked Example (Exam-Style)

Below is a table of animals called tbl_animals:

AnimalBreedingNumber of Young
Red FoxYes4–6
RabbitYes4–12
African ElephantYes1
Blue WhaleNo1
OrangutanYes1
Polar BearYes1–3
DolphinYes1
KangarooYes1
LionYes1–6
PenguinYes1
Exam Question — 3 marks

Complete this SQL statement to display all of the animal breeds that are currently breeding and there was only one young born this year.

SELECT .................................
FROM .................................
WHERE .................................

  What to Memorise

SELECT
Chooses which columns to retrieve from a table.
FROM
Specifies which table to retrieve data from.
WHERE
Filters rows based on a specified condition.
AND
Row must satisfy ALL combined conditions.
OR
Row must satisfy AT LEAST ONE combined condition.
*
Wildcard meaning "all columns" (used with SELECT).
% (with LIKE)
Wildcard meaning "any characters" for partial text matching.
ORDER BY ... ASC/DESC
Sorts results ascending or descending by one or more columns.
SUM(column)
Adds up all values in a numeric column.
COUNT(*)
Counts the number of rows matching the criteria.

  Concepts Checklist

  Exam Tips & Common Mistakes

Mixing up AND and OR. A row can't be "from London AND from Paris" at the same time — that needs OR. Always re-read the question logically: does the row need to satisfy every condition, or just one?
Forgetting quotes around text values. Text (strings) in WHERE conditions must be in quotes, e.g. WHERE city = 'New York'. Numbers don't need quotes, e.g. WHERE age > 30.
Confusing * and %. * is only used after SELECT to mean "all columns." % is only used inside a LIKE condition to mean "any characters." They are never interchangeable.
Missing the semicolon. Most mark schemes expect a semicolon at the end of the statement — get in the habit of always finishing with ;
Wrong keyword order. The order is always SELECT → FROM → WHERE → ORDER BY. Writing WHERE before FROM, or ORDER BY before WHERE, will lose marks even if the logic is otherwise correct.
What examiners look for: exact keyword spelling (SELECT, not "select data"), correct column/table names copied precisely from the question, quotes around text, and the right connector (AND/OR) matching the described logic.
🔓 Read the full SQL note — free You're seeing the preview · free account, no card needed
Also in the full note
  • WHERE, AND & OR — Filtering Data
  • Exam Tips & Common Mistakes
What's inside
📖 Revision notes 🎯 Learn mode ✦ AI flashcards ✓ Instant AI marking 🧊 3D explorers 🧪 Experiments & simulations 📈 Progress tracking
📄 Practise SQL with Computer Science 0478 past papers Every paper with its mark scheme — answer online, marked instantly. Open →

Read the full SQL notes free

That's the preview — create a free account to read the rest, plus flashcards and practice questions with instant AI marking. No credit card.

Unlock the full notes free →

More Computer Science topics