SQL: WHERE/filtering

Computer Science · Databases

Question 1 of 10

0

    students name | age | class | score Ben | 15 | 8A | 41 Fay | 14 | 7B | 70 Hana | 17 | 8A | 62 Dev | 17 | 7B | 70 Eli | 18 | 8A | 95 Ava | 14 | 7A | 51 How many rows does this query return? SELECT name FROM students WHERE age >= 15;

    Decimals (0.75 or 0,75), fractions (3/4) and mixed numbers (1 1/2) are accepted. Do not use thousands separators.

    How to type your answer
    • Fractions: 3/4. Mixed numbers: 1 1/2.
    • Decimals with a point or a comma: 0.75 or 0,75.
    • Negative numbers start with a minus: -3/4.
    • Percentages need the % sign: 75%.

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    SQL: WHERE/filtering explained

    The WHERE clause in an SQL query keeps only the rows that match a condition. Without it, a SELECT query returns every row in the table. WHERE comes after FROM, and each condition is a comparison such as age > 18 or city = 'Leeds'.

    Conditions can be combined with AND, OR and NOT. AND needs both parts to be true, OR needs at least one part to be true, and NOT reverses a condition. Brackets control which parts are checked together, just as they do in arithmetic.

    • Use = for equal to, <> for not equal, and <, >, <= and >= for comparisons.
    • Text values go in single quotation marks, but numbers do not.
    • BETWEEN 10 AND 20 includes both end values, and IN ('Leeds', 'York') matches any value in the list.
    • LIKE 'A%' matches text that starts with the letter A.

    A common mistake is to write WHERE age = 18 OR 19 and expect it to match either age. The second condition must name the column again: WHERE age = 18 OR age = 19.

    Worked example

    A table has 10 students with the ages 15, 17, 18, 18, 19, 21, 16, 20, 14 and 22. How many rows does this query return?

    SELECT name FROM students WHERE age >= 18 AND age < 21;
    
    1. The ages from 18 up to but not including 21 are 18, 18, 19 and 20.
    2. Counting them gives 4 students who match.
    3. So the query returns 4 rows.

    Check: the ages 21 and 22 fail the second condition, and the ages 14, 15, 16 and 17 fail the first, which leaves 4 rows.

    What you will practise

    New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.

    Example questions

    • Easy

      students name | age | class | score Ben | 15 | 7A | 78 Ava | 17 | 8A | 85 Gus | 18 | 7A | 86 Hana | 17 | 8A | 42 Kai | 16 | 7A | 94 Eli | 15 | 7B | 63

      How many rows does this query return? SELECT name FROM students WHERE class = '7A';

    • Medium

      students name | age | class | score Gus | 14 | 7A | 67 Jada | 17 | 8A | 52 Kai | 15 | 8A | 45 Ava | 17 | 8A | 44 Lena | 18 | 7B | 71 Ivo | 18 | 8A | 98

      How many rows does this query return? SELECT name FROM students WHERE class = '7A' OR score > 84;

    • Hard

      students name | age | class | score Hana | 15 | 7B | 83 Lena | 17 | 7B | 99 Fay | 18 | 7B | 75 Ben | 17 | 7A | NULL Eli | NULL | 8A | 43 Ivo | 14 | 7B | 78 Cara | 14 | 7B | 74

      How many rows does this query return? SELECT name FROM students WHERE score <> 76;