Querying with SQL
Introduit| English | Sens |
|---|---|
| SQL/ˌes kjuː ˈel/ | SQL |
| query/ˈkwɪərɪ/ | requête |
| record/ˈrekɔːd/ | enregistrement |
| field/fiːld/ | champ |
| condition/kənˈdɪʃn/ | condition |
Asking a database questions
- SQL 结构化查询语言 (Structured Query 查询 Language) picks out the records 记录 you want.
- A few keywords do almost everything at IGCSE.
- Read a query in a fixed order to work out its output.
SELECT, FROM, WHERE
SELECT FirstName, FormClass
FROM Student
WHERE FeesPaid = TRUE;
SELECT= which fields 字段 to show (use*for all).FROM= which table.WHERE= a condition 条件, so only matching records appear.

Run a SELECT … WHERE query
WHERE filters the rows to those that match; SELECT then keeps only the columns you chose. Step through to watch the table shrink to the query's answer.
In an SQL query, SELECT specifies:
Difficulté 1/5 · 100 pièces + un petit bonus lors de la première réussite aujourd'hui
SELECT chooses the fields; FROM the table; WHERE the rows; ORDER BY the sort.
The WHERE clause:
Difficulté 1/5 · 100 pièces + un petit bonus lors de la première réussite aujourd'hui
WHERE filters rows by a condition.
ORDER BY, AND, OR
SELECT FirstName, DateOfBirth
FROM Student
WHERE FormClass = '10A' AND FeesPaid = FALSE
ORDER BY DateOfBirth ASC;
ORDER BYsorts:ASC(smallest/A→Z first) orDESC(largest/Z→A first).- Join conditions with
AND(both true) orOR(at least one true).
ORDER BY mark DESC sorts the results:
Difficulté 1/5 · 100 pièces + un petit bonus lors de la première réussite aujourd'hui
DESC = descending (largest/Z→A first); ASC = ascending.
SUM, COUNT, and reading order
COUNTcounts matching records;SUMadds up a number field.- To work out a query's output, read it in this order:
- FROM (which table) → WHERE (keep matching rows) → SELECT (show chosen fields) → ORDER BY (sort).
SELECT COUNT(StudentID) FROM Student WHERE FeesPaid = FALSE;

Match each SQL keyword to what it does.
Difficulté 2/5 · 140 pièces + un petit bonus lors de la première réussite aujourd'hui
SELECT picks columns, WHERE filters rows, ORDER BY sorts, COUNT tallies the matching rows.
Put the SQL clauses in the order you read them to find the output.
Difficulté 3/5 · 200 pièces + un petit bonus lors de la première réussite aujourd'hui
FROM → WHERE → SELECT → ORDER BY gives you exactly which rows and columns appear.
Worked example: read a query's output
Table Student: (1, Amy, 16, 10A), (2, Ben, 15, 10B), (3, Cara, 17, 10A), with fields StudentID, Name, Age, Form. What does this query output?
SELECT Name, Age FROM Student WHERE Form = "10A" ORDER BY Age DESC;
- FROM Student, WHERE Form = "10A" keeps Amy and Cara. SELECT shows only the Name and Age fields.
- ORDER BY Age DESC sorts from oldest to youngest: Cara 17, then Amy 16.
Using the same table, what does SELECT COUNT(StudentID) FROM Student WHERE Age >= 16; output?
Difficulté 2/5 · 140 pièces + un petit bonus lors de la première réussite aujourd'hui
Amy (16) and Cara (17) match, so the count is 2.
Which query adds up the ages of all students?
Difficulté 1/5 · 100 pièces + un petit bonus lors de la première réussite aujourd'hui
SUM adds a number field; COUNT counts records.
You've got it
SELECT(fields) ·FROM(table) ·WHERE(condition);*= all fieldsORDER BY … ASC/DESCsorts;AND/ORjoin conditionsCOUNTcounts rows;SUMtotals a number field- read a query: FROM → WHERE → SELECT → ORDER BY