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. /Joining conditions with AND and OR

Course 9.3·Lesson 2 of 3

Joining conditions with AND and OR

Real questions often have two conditions: Saturday sales of evening shows, or members who are under 12 or over 65. AND needs both conditions to be true, OR needs at least one. You join conditions, find a range with AND, and count and add the records that pass.

about 25 min1 simulation1 practice question
Lesson 2 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.

Two conditions at once

The manager now wants the evening sales on Saturday only. That is two conditions: the day is Saturday, and the show is the evening show. We join them with AND.

sql
SELECT SaleID, Seats
FROM Tickets
WHERE Day = 'Saturday' AND Show = 'Evening';

The output:

table
SaleID  Seats
T2      2
T5      5

AND: both must be true

With AND, a record passes only if both conditions are true for it.

  • T3 is on Saturday, but it is a matinee. One part is false, so it fails.
  • T4 is an evening show, but on Sunday. It fails too.

AND usually gives fewer records than either condition alone.

OR: at least one must be true

With OR, a record passes if at least one condition is true, or both.

sql
SELECT SaleID
FROM Tickets
WHERE Day = 'Saturday' OR Show = 'Evening';

The output:

table
SaleID
T2
T3
T4
T5
T6

T3 passes because it is on Saturday. T4 passes because it is an evening show. T2 and T5 pass on both counts, but each is output once. Only T1, a Friday matinee, fails both.

OR usually gives more records than either condition alone.

Two overlapping circles. The left circle is labelled Day = 'Saturday' and holds T3 alone; the right circle is labelled Show = 'Evening' and holds T4 and T6 alone; the overlap holds T2 and T5. T1 is outside both circles. Under the picture: AND gives the overlap (T2, T5); OR gives everything inside either circle (T2, T3, T4, T5, T6).

A range with AND

To find values between two limits, join two comparisons on the same field with AND.

sql
SELECT SaleID
FROM Tickets
WHERE Seats >= 2 AND Seats <= 4;

The output:

table
SaleID
T1
T2
T4

Outside a range is the opposite: WHERE Seats < 2 OR Seats > 4. A value cannot be less than 222 and more than 444 at the same time, so that one needs OR.

AND and OR with COUNT and SUM

The conditions work with COUNT and SUM in the same way.

sql
SELECT SUM(Amount)
FROM Tickets
WHERE Day = 'Saturday' AND Show = 'Evening';

The output:

table
SUM(Amount)
105

Watch out. Each part of a condition needs its own field name. Write Day = 'Friday' OR Day = 'Sunday', not Day = 'Friday' OR 'Sunday'.

Practise with new numbers

Try it in Python

The program runs the same two conditions joined first with AND and then with OR, and prints the records and the count each time.

Read it first. Then run it. Which gives more records, AND or OR?

1. Change 'Evening' to 'Matinee' in both scripts. Predict both outputs before you run it.

Common mistakes

  • AND when the question says "or". "Friday or Sunday" needs OR: no sale is on both days.
  • OR for a range. "From 2 to 4 seats" needs AND.
  • A missing field name. Day = 'Friday' OR 'Sunday' does not test the day twice.
  • Counting a record twice with OR. A record that meets both parts is output once.

Remember these

  • AND: both conditions must be true. Usually fewer records.
  • OR: at least one condition must be true. Usually more records.
  • A range is Field >= low AND Field <= high.

Check yourself

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

  1. Which sale IDs does WHERE Day = 'Friday' AND Seats > 2 give?
  1. How many records pass WHERE Day = 'Friday' OR Day = 'Sunday'?
  1. Write the condition for sales with an amount from 303030 to 505050 dollars, including both ends.
  1. Why does WHERE Day = 'Friday' AND Day = 'Sunday' output nothing?
  1. What does SELECT COUNT(*) FROM Tickets WHERE Show = 'Matinee' OR Seats = 5; output?

References

  • Cambridge International, IGCSE Computer Science 0478 syllabus (exams in 2026, 2027 and 2028), section 9 Databases, 9.4 (AND and OR).

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 (4)

A decorator lists her tins of paint in a table Paints…Easy · High School (9–10) · by StemPy Lab
A camera club lends out equipment and stores each loan in a table Loans…Medium · High School (9–10) · by StemPy Lab
A school weather station stores one record a day in a table Weather: the rainfall in millimetres…Medium · High School (9–10) · by StemPy Lab
Next: lesson 3Reading, completing and correcting SQL scripts
← SQL: SUM, COUNT, AND, OR and Reading Query Output
← Counting and adding with COUNT and SUMReading, completing and correcting SQL scripts →

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.

A school garden club records each plot in a table Plots: the water it needs each day in litres, its…Hard · High School (9–10) · by StemPy Lab

Python Labs (1)

Two conditions with AND and ORPython Lab · by StemPy Lab