[9.4] SQL single-table queries

[9.4] SQL Single-Table Queries: SELECT, FROM, WHERE, ORDER BY, SUM, COUNT, AND, OR

Why learn SQL for a single table?

SQL (Structured Query Language) lets you ask questions about data stored in a database table and return exactly what you need. In the IGCSE you should be able to read, understand and complete short single-table SQL scripts that use the clauses and operators listed in this benchmark: SELECT, FROM, WHERE, ORDER BY with ASC/DESC, the aggregate functions SUM and COUNT, and the logical operators AND and OR.

Think of a database table as a spreadsheet with named columns. SQL is the precise way to filter rows, choose columns, sort results and calculate totals without changing the stored data. Small details matter: a missing condition, the wrong operator, or sorting in the wrong direction can change the answer. This page will show you the structure of each clause, common mistakes to avoid, and realistic patterns you will meet in questions.

A running example table

All examples refer to a single table called Orders with these fields:

Field nameTypeMeaningExample
OrderIDINTEGERUnique identifier for each order10217
CustomerTEXTCustomer name"Ali Khan"
ItemTEXTProduct name"Wireless Mouse"
QtyINTEGERQuantity ordered3
PriceEachREALUnit price (£)9.99
CityTEXTDelivery city"Bristol"
OrderDateDATEDate of order2026-03-14

Core SQL clause structure

Use this mental template for single-table queries:

  • SELECT columns or expressions
  • FROM TableName
  • WHERE row condition optional
  • ORDER BY column ASC/DESC optional

Add SUM or COUNT in the SELECT list when a total or number of rows is required. For this benchmark, totals are for the whole filtered set (no grouping across categories).

Reading, understanding, completing

Exam questions often show a partly written query with missing parts indicated by ???. Your job is to complete it correctly by using field names and the right operators. Always cross-check with the field list given in the question.

Tabs of targeted examples

Task: Show all orders delivered to Bristol. Return OrderID, Customer and Qty.

SELECT OrderID, Customer, Qty
FROM Orders
WHERE City = 'Bristol';

Why: WHERE City = 'Bristol' filters the rows. Quotation marks indicate a text value.

Task: Find orders where the city is Bristol or the quantity is more than 5. Return OrderID and City.

SELECT OrderID, City
FROM Orders
WHERE City = 'Bristol' OR Qty > 5;

AND requires both conditions to be true, OR requires either. Choose the operator that matches the English meaning in the question.

Task: Show orders for Bristol customers who ordered more than 5 items, or any order on 2026-03-14 regardless of city.

Be careful: AND is evaluated before OR. Use brackets to force the intended logic.
SELECT OrderID, Customer, City, Qty, OrderDate
FROM Orders
WHERE (City = 'Bristol' AND Qty > 5)
   OR OrderDate = '2026-03-14';

Why: The brackets group the first pair of conditions so the date condition applies to all cities.

Task: List Customer and Qty for Bristol orders, highest quantity first.

SELECT Customer, Qty
FROM Orders
WHERE City = 'Bristol'
ORDER BY Qty DESC;

ASC is the default if you do not specify direction. Use DESC for largest to smallest.

Task: List City and Customer alphabetically by City, and for the same City sort by Customer name A–Z.

SELECT City, Customer
FROM Orders
ORDER BY City ASC, Customer ASC;

The first key groups by city. The second key sorts customers within each city group.

Edge case: If numerical values are stored in a text field, ordering will use alphabetical rules (e.g. "100" comes before "9"). For numbers, store them as INTEGER or REAL so ORDER BY behaves numerically.

Task: Find total revenue for all Bristol orders. Revenue per row is Qty * PriceEach.

SELECT SUM(Qty * PriceEach) AS TotalRevenue
FROM Orders
WHERE City = 'Bristol';

Aggregates like SUM produce a single value for all rows that pass the WHERE filter.

Task: Count the number of orders made on 2026-03-14.

SELECT COUNT(*) AS NumOrders
FROM Orders
WHERE OrderDate = '2026-03-14';

COUNT(*) counts rows. It ignores which columns you selected elsewhere.

COUNT(*) counts all rows that meet the filter. COUNT(column) only counts rows where that specific column is not empty. For most IGCSE tasks, use COUNT(*) unless the question specifies otherwise.

Checklist for completing SQL in the exam

  1. Identify fields exactly as given. SQL is sensitive to spelling of field names.
  2. Choose operators carefully: =, >, <, and use AND/OR to match the English meaning.
  3. Quote text values with single quotes. Do not quote numbers or dates if the exam board shows date as text, follow their format exactly.
  4. Calculate inside SELECT using expressions such as Qty * PriceEach when totals are needed.
  5. Sort only if the question asks for a specific order. State ASC or DESC if not obvious.
  6. Test logic by paraphrasing: “I am selecting these columns from this table where this condition is true, then ordering by this column.”

Deep Dive: Translating English questions into SQL

Underline the output columns, circle the filter words, and box any sorting phrases. For totals, look for verbs like “total”, “sum”, or “how many”. Then map them to clauses: outputs to SELECT, table name to FROM, filters to WHERE (with AND/OR), sorting to ORDER BY, and totals to SUM/COUNT. Finally, check the units and directions, e.g. “highest first” means DESC.

 Key Takeaways

  • Single-table SQL follows a clear pattern: SELECTFROM → optional WHERE → optional ORDER BY.
  • Use AND when both conditions must be true and OR when either condition is acceptable. Add brackets for clarity.
  • SUM and COUNT work on the set of rows that pass the WHERE filter.
  • Quote text values, not numbers. Sort correctly with ASC or DESC as the question demands.
  • Read the question carefully and translate outputs, filters, sort order and totals into the right SQL clauses.
  • Small syntax details matter. Copy field names accurately and show calculation expressions precisely.