HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
SQL: SUM, COUNT, AND, OR and Reading Query Output
  1. 1Counting and adding with COUNT and SUM
  2. 2Joining conditions with AND and OR
  3. 3Reading, completing and correcting SQL scripts
  1. Computer Science
  2. /SQL: SUM, COUNT, AND, OR and Reading Query Output
  3. /Reading, completing and correcting SQL scripts

Course 9.3·Lesson 3 of 3

Reading, completing and correcting SQL scripts

In the examination you read a script and give its exact output, complete a script that has gaps, and find and correct the errors in a script. This lesson gives you a method for each task and practises it on the Tickets table.

about 25 min1 simulation1 practice question
Lesson 3 of 3Show all lessonsHide
SQL: SUM, COUNT, AND, OR and Reading Query Output
  1. 1Counting and adding with COUNT and SUM
  2. 2Joining conditions with AND and OR
  3. 3Reading, completing and correcting SQL scripts

Try the code and the simulations beside the lesson.

Three kinds of question

Examination questions on SQL come in three kinds.

  1. Read a script and give its output.
  2. Complete a script that has gaps, so that it gives a stated output.
  3. Find and correct the errors in a script.

All three use the same few keywords. Here is a method for each.

1. Giving the output

Work through the script in this order, whatever order it is written in.

  1. FROM: which table?
  2. WHERE: test each record. Mark the ones that pass. With AND both parts must be true, with OR at least one.
  3. COUNT or SUM: if the script has one, the output is a single number.
  4. ORDER BY: sort the records that passed.
  5. SELECT: write down only the selected fields, in the order of the SELECT list.
Five boxes joined by arrows from left to right, the order in which to work out the output of a script: 1 FROM: which table; 2 WHERE: keep the records that pass; 3 COUNT or SUM: one number; 4 ORDER BY: sort; 5 SELECT: write down the chosen fields.

Let us try it.

sql
SELECT Day, Amount
FROM Tickets
WHERE Seats > 2 AND Show = 'Evening'
ORDER BY Amount DESC;

The output:

table
Day       Amount
Saturday  75
Sunday    45
  • WHERE. Evening sales: T2, T4, T5 and T6. Of these, more than 222 seats: T4 (333) and T5 (555).
  • ORDER BY. Amounts 454545 and 757575 dollars, largest first: T5, then T4.
  • SELECT. The day and the amount of each.

Write the output as a table, with the field names at the top. Keep the values exactly as they are in the table.

2. Completing a script

Read what the output must be, then fill each gap with one of the keywords or a field name.

sql
SELECT ________(Seats)
FROM Tickets
WHERE Day = 'Friday' ________ Day = 'Sunday';

The script must give the total seats sold on Friday and Sunday. A total is SUM. A sale cannot be on two days, so the days are joined with OR. The output is 4+3+1=84 + 3 + 1 = 84+3+1=8.

3. Finding and correcting errors

Check each part of the script against this list.

  • The clauses are in the order SELECT, FROM, WHERE, ORDER BY.
  • Commas go between field names, and not after the last one.
  • Field and table names are spelled as in the table.
  • Text values are in quotes, numbers and TRUE or FALSE are not.
  • Each part of an AND or OR has its own field name.
  • ASC or DESC comes after the field in ORDER BY.

Example: SELECT SaleID Amount FROM Ticket WHERE Day = Saturday ORDER BY DESC Amount; has four errors. There is no comma between the fields. The table is Tickets. Saturday needs quotes. DESC must come after Amount.

Practise with new numbers

Try it in Python

The program runs a list of scripts on the Tickets table. For each one it prints the field names and the output, laid out like an examination answer. Write your own answer on paper first, then run it.

Read it first. Predict the output of each script on paper. Then run it and compare.

1. Add your own script to the list: the total amount of the matinee sales. Predict the output first.

Common mistakes

  • Working in the written order. Do the WHERE first and the SELECT last.
  • Outputting fields that are not selected. Only the SELECT list is output.
  • A list for COUNT or SUM. They give one number.
  • Fixing one error and stopping. An error question often has several errors.

Remember these

  • To give the output: FROM, WHERE, COUNT or SUM, ORDER BY, then SELECT.
  • To complete a script: decide what the output needs, then choose the keyword.
  • To correct a script: check the order, the commas, the names, the quotes and each condition.

Check yourself

Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.

  1. Give the output of SELECT SaleID FROM Tickets WHERE Day = 'Saturday' ORDER BY Seats ASC;.
  1. Complete: SELECT ________ FROM Tickets WHERE Show = 'Matinee'; so that it outputs 222.
  1. Find the error: SELECT COUNT(*) FROM Tickets WHERE Seats > 2 AND < 5;.
  1. What does SELECT SUM(Amount) FROM Tickets WHERE Day = 'Sunday' OR Seats = 1; output?
  1. Which step comes last when you work out the output of a script?

References

  • Cambridge International, IGCSE Computer Science 0478 syllabus (exams in 2026, 2027 and 2028), section 9 Databases, 9.4: read, understand and complete SQL scripts, and identify the output of a script.

Enroll free in SQL: SUM, COUNT, AND, OR and Reading Query Output

Every lesson is open to read. Enrolling also lets you:

  • Mark lessons done and carry on where you stopped.
  • Take the tests of this lesson and its course.
  • Count the course towards the certificate for its chapter.
No payment. You can leave at any time.

Problems (3)

A gym stores its members in a table Gym…Easy · High School (9–10) · by StemPy Lab
A town library stores its readers in a table Readers…Medium · High School (9–10) · by StemPy Lab
A restaurant stores its staff in a table Staff…Hard · High School (9–10) · by StemPy Lab
That was the last lessonBack to the course: take the test
← SQL: SUM, COUNT, AND, OR and Reading Query Output
← Joining conditions with AND and ORNext →

Discussion (0)

Log in to join the discussion.

Loading…

1 Python program
5 questions to check yourself

This lesson is free to read. Enroll free to keep your place and take the tests.

Question Sets (1)

COUNT, SUM, AND and OR: Test Yourself10 questions · by StemPy Lab

Notes (1)

COUNT, SUM, AND and OR: Quick RevisionNote · by StemPy Lab