SQL queries Cambridge IGCSE Computer Science (9–1) revision

Not started

Learn it

In plain words

Structured query language (SQL) is how you ask a database a question. A query reads almost like a sentence: select these fields, from this table, where this is true, in this order.

6 things to know

  1. SELECT lists the fields to be shown. FROM names the table.
  2. WHERE gives the condition a record must meet to be included. Conditions can be joined with AND (both must be true) or OR (at least one must be true).
  3. ORDER BY sorts the results by a field: ASC for ascending order and DESC for descending order.
  4. SUM adds up the values in a field. COUNT counts the number of records.
  5. The order of the parts never changes: SELECT, FROM, WHERE, ORDER BY.
  6. Text values in a condition go inside quotation marks. Numbers do not.

Worked example

A table called Books has the fields Title, Genre and Price. It holds: Night Sky, Science, 12.50. Lost Map, Adventure, 7.99. Deep Sea, Science, 9.00. Iron Gate, Crime, 7.50. What is the output of SELECT Title FROM Books WHERE Price < 10 ORDER BY Price ASC?

  1. WHERE Price < 10 keeps the books that cost less than 10: Lost Map (7.99), Deep Sea (9.00) and Iron Gate (7.50).
  2. ORDER BY Price ASC puts them in order of price, cheapest first: 7.50, 7.99, 9.00.
  3. SELECT Title shows only the titles: Iron Gate, Lost Map, Deep Sea.

Tips and tricks

  • Only the fields after SELECT are shown. A field used in WHERE or ORDER BY does not appear unless it is also selected.
  • Work in this order: apply WHERE to choose the records, then ORDER BY to sort them, then SELECT to pick the fields.
6 questions, about 2 minutes.

It lands in your notebook with its questions as flashcards.

SQL queries: 6 questions and answers

These are the quiz’s questions. Do the quiz first, then come back here for the ones that got you.

  1. Which SQL keyword lists the fields to be displayed?
    • SELECT (the answer)
    • FROM
    • WHERE
    • ORDER BY

    SELECT comes first in every query.

  2. Which SQL keyword names the table to search?
    • SELECT
    • FROM (the answer)
    • WHERE
    • SUM

    FROM Books, for example.

  3. Which SQL keyword sets the condition that records must meet?
    • SELECT
    • FROM
    • WHERE (the answer)
    • COUNT

    Only records that meet the condition are included.

  4. What does ORDER BY Price DESC do?
    • shows only the prices
    • sorts the results with the highest price first (the answer)
    • sorts the results with the lowest price first
    • adds up the prices

    DESC means descending.

  5. What does COUNT do in an SQL query?
    • adds up the values in a field
    • counts the number of records (the answer)
    • sorts the records
    • deletes records

    SUM is the one that adds up.

  6. Which records does WHERE Age > 15 AND House = 'Red' select?
    • everyone over 15, and everyone in Red house
    • only those who are over 15 and also in Red house (the answer)
    • everyone in the table
    • those who are 15 and in Red house

    With AND, both conditions must be true.

Still stuck on this one?Ask in the Papermunch Discord, or help someone else who is. Discord is for ages 13 and up.Join the server

Things you can type

Or go straight to

Or browse a shelf