
What is SQL?
SQL stands for Structured Query Language. It is the language used to work with data stored in a database. A query is a request for data, like asking the database, “Show me the biscuit type and quantity of every order.”
All our examples use the Golden Biscuit Tin's BiscuitOrders table. If records, fields and primary keys are new to you, read Database Structure Explained first.
| OrderID | BiscuitType | Quantity |
|---|---|---|
| 101 | Hobnob | 6 |
| 102 | Viennese Swirl | 4 |
| 103 | Hobnob | 8 |
| 104 | Hobnob | 2 |
Cambridge syllabus focus
Topics 9.2–9.3: know that SQL is Structured Query Language, and understand SQL scripts that use SELECT and FROM to query a single table.
Your first query
Jacques wants to see which biscuits were ordered and how many, but he doesn't need the order numbers. He runs this query:
SELECT BiscuitType, Quantity FROM BiscuitOrders;
SELECT BiscuitType, Quantitylists the fields to display. When there is more than one, separate them with commas.FROM BiscuitOrdersnames the source table the data comes from.- The semicolon marks the end of the statement.
| BiscuitType | Quantity |
|---|---|
| Hobnob | 6 |
| Viennese Swirl | 4 |
| Hobnob | 8 |
| Hobnob | 2 |
Notice two things. First, all four records are returned. SELECT chooses columns, not rows. Second, Hobnob appears three times. Each Hobnob order is a separate record, so each one appears in the output. SELECT does not merge or remove repeated values.
Check your understanding 1
How many records and how many fields would SELECT BiscuitType FROM BiscuitOrders; output?
Reveal answer
4 records and 1 field: Hobnob, Viennese Swirl, Hobnob, Hobnob.
Selecting every field with SELECT *
The asterisk * means “every field”. It is a shortcut for listing every field name:
SELECT * FROM BiscuitOrders;
| OrderID | BiscuitType | Quantity |
|---|---|---|
| 101 | Hobnob | 6 |
| 102 | Viennese Swirl | 4 |
| 103 | Hobnob | 8 |
| 104 | Hobnob | 2 |
SELECT BiscuitType, Quantity
4 × 24 records, only the 2 named fields.
SELECT *
4 × 34 records, every field in the table.
Both queries return the same four records, and Hobnob appears three times in both. The only difference is how many fields are shown.
SELECT * shows extra fields the question did not ask for, and it may lose you the mark.What order do the rows come out in?
The outputs above list orders 101 to 104, the order they were stored in. But SQL does not guarantee any row order unless the query says so. A database may return the same records in a different order, especially in a large table.
To control the order, add an ORDER BY clause. That is covered in SQL ORDER BY Explained.
Worked example: display the order numbers and quantities
Task: Write a query to display the OrderID and Quantity of every order.
- Which fields? OrderID and Quantity, so
SELECT OrderID, Quantity. - Which table?
FROM BiscuitOrders. - Are all records needed? Yes, so no WHERE clause is needed.
SELECT OrderID, Quantity FROM BiscuitOrders;
Result: 4 records, each showing an OrderID and its Quantity: 101 and 6, 102 and 4, 103 and 8, 104 and 2.
Check your understanding 2
A student writes SELECT BiscuitOrders FROM Quantity;. What has gone wrong?
Reveal answer
The table name and the field name have been swapped. Field names go after SELECT and the table name goes after FROM: SELECT Quantity FROM BiscuitOrders;
Common Cambridge exam mistakes
SELECT returns one row for every record, so the output is Hobnob, Viennese Swirl, Hobnob, Hobnob. Repeated values are not removed.
SELECT chooses the fields (columns). Choosing records (rows) is the job of WHERE.
Original FutureLogic exam-style practice
These are original teaching questions, not official Cambridge examination questions. The marks shown are a guide only.
Question 1 (2 marks)
Write an SQL query to display the OrderID and BiscuitType of every order in the BiscuitOrders table.
Reveal model answer
SELECT OrderID, BiscuitType FROM BiscuitOrders;
1 mark for SELECT OrderID, BiscuitType and 1 mark for FROM BiscuitOrders.
Question 2 (2 marks)
State how many records and how many fields are output by this query, and explain why “Hobnob” appears more than once.
SELECT BiscuitType FROM BiscuitOrders;
Reveal model answer
4 records and 1 field (1). Hobnob appears three times because there are three separate Hobnob orders/records, and SELECT outputs a value for every record (1).
Quick quiz
- What does SQL stand for?
- What is a query?
- What does
*mean after SELECT? - Without ORDER BY, is the row order guaranteed?
Quiz answers
Reveal all answers
- Structured Query Language.
- A request for data from a database.
- Every field in the table.
- No. Use ORDER BY to control it.
Final summary
SQL
Structured Query Language: the language used to query a database.
SELECT
Chooses the fields (columns) to display.
FROM
Names the source table.
SELECT *
Every field. The same 4 records are still returned.