SQL: SELECT queries

Computer Science · Databases

Question 1 of 10

0

    students name | class | grade | score Ava | 7A | B | 94 Kai | 8A | C | 85 Lena | 8A | B | 83 Hana | 7A | B | 54 How many rows does this query return? SELECT DISTINCT class FROM students;

    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: SELECT queries explained

    SQL, short for Structured Query Language, is the language used to ask questions of a relational database. A SELECT query reads data from a table without changing it. SELECT names the columns to return, FROM names the table to read, and an optional WHERE keeps only the rows that meet a condition.

    A table is made of rows, one per record, and columns, one per field. SELECT * returns every column, while SELECT name, score returns only those two. SQL keywords are not case sensitive, but it is common practice to write them in capitals so they stand out. Text values in a condition go in single quotes, while numbers do not.

    A common mistake is to write the clauses in the wrong order. They must come in the order SELECT, FROM, WHERE, and putting WHERE before FROM causes a syntax error. Another mistake is to think a SELECT query changes the table. It only reads the data, so the table stays the same.

    Worked example

    A table called Students has these rows:

    name   score
    Ali    72
    Bea    58
    Cem    91
    Dee    65
    

    Run this query:

    SELECT name, score FROM Students WHERE score >= 65;
    
    1. Keep only the rows with a score of 65 or more: Ali (72), Cem (91) and Dee (65). Bea is removed.
    2. The result has 3 rows.
    3. The average of the returned scores is (72+91+65)÷3=76(72 + 91 + 65) \div 3 = 76.

    Key facts

    The practice questions use a table called students with four columns: name, class, grade and score. They ask what a query returns, using four ideas that go beyond a plain SELECT.

    • DISTINCT removes repeated rows from the result. With more than one column, two rows count as repeats only when every selected column matches.
    • ORDER BY score DESC sorts from highest to lowest. Without DESC the order is ascending, lowest first.
    • LIMIT k keeps only the first k rows of the sorted result.
    • OFFSET n skips the first n rows before LIMIT counts. LIMIT 2 OFFSET 1 means "skip one row, then keep two".

    The clauses always come in this order: SELECT, FROM, WHERE, ORDER BY, LIMIT. Sorting happens before LIMIT, so the rows you keep depend on the sort.

    All the examples below use this table:

    students
    name | class | grade | score
    Ali  | 7A    | B     | 72
    Bea  | 7B    | A     | 91
    Cem  | 7A    | B     | 58
    Dee  | 8A    | C     | 65
    Eli  | 7B    | A     | 80
    Fay  | 8A    | B     | 77
    

    Worked example 2

    How many rows does each query return?

    SELECT DISTINCT class FROM students;
    SELECT DISTINCT class, grade FROM students;
    
    1. The classes down the table are 7A, 7B, 7A, 8A, 7B, 8A. The different values are 7A, 7B and 8A, so the first query returns 3 rows.
    2. For the second query, write out each (class, grade) pair: 7A/B, 7B/A, 7A/B, 8A/C, 7B/A, 8A/B.
    3. The repeats are the second 7A/B and the second 7B/A. The different pairs are 7A/B, 7B/A, 8A/C and 8A/B, so the second query returns 4 rows.

    The table has 6 rows, but 3 is not the answer to the second query. DISTINCT looks at the whole row of selected columns, not at one column at a time. Note that 8A appears with two grades (C and B), so both pairs stay.

    Worked example 3

    What is the sum of the scores returned by this query?

    SELECT score FROM students ORDER BY score DESC LIMIT 2 OFFSET 1;
    
    1. Sort the scores from high to low: 91, 80, 77, 72, 65, 58.
    2. OFFSET 1 skips the first row, which is 91.
    3. LIMIT 2 keeps the next two rows: 80 and 77.
    4. The sum is 80+77=15780 + 77 = 157.

    If the query had no OFFSET, LIMIT 2 would return 91 and 80. If it used ASC instead of DESC, the sorted list would start 58, 65, 72, so the answer would change completely. Always write the sorted list out before you apply LIMIT.

    Worked example 4

    Which names does this query return, and in what order?

    SELECT name FROM students WHERE class = '7B' ORDER BY score ASC;
    
    1. WHERE runs first and keeps only the rows with class 7B: Bea (91) and Eli (80).
    2. ORDER BY score ASC puts the lowest score first: Eli (80), then Bea (91).
    3. SELECT name returns only the name column, so the result is Eli, then Bea.

    The score column is used to sort even though it is not shown. A query can sort by a column it does not return.

    Common mistakes

    • Using LIMIT without ORDER BY and assuming the rows are in score order. Without ORDER BY, the order is not guaranteed.
    • Forgetting that ORDER BY is ascending by default.
    • Applying OFFSET after LIMIT in your head. OFFSET skips first, then LIMIT counts.
    • Using DISTINCT on one column when two are selected. DISTINCT works on the whole row.
    • Returning the first row when the question asks for the last row of a LIMIT result.
    • Putting quotes round numbers or leaving them off text such as '7B'.

    Exam tips

    • Rewrite the table in sorted order before you read off an answer. It takes ten seconds and removes most slips.
    • For DISTINCT, list the pairs or values and cross out repeats instead of counting by eye.
    • Check the question: "how many rows", "the last row" and "the sum of the scores" need different final steps.
    • Read the clause order as a recipe: filter (WHERE), then sort (ORDER BY), then trim (LIMIT).
    • Write out which rows were skipped by OFFSET and which were kept by LIMIT, so a method mark is available.

    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 | class | grade | score Hana | 7A | B | 45 Jada | 7A | A | 71 Eli | 7B | C | 65 Gus | 8A | C | 73 Kai | 7B | C | 72

      How many rows does this query return? SELECT DISTINCT class FROM students;

    • Medium

      students name | class | grade | score Jada | 8A | C | 72 Ava | 7A | B | 87 Lena | 7A | B | 84 Hana | 7A | B | 63 Ivo | 7A | A | 44

      What is the score in the last row this query returns? SELECT name, score FROM students ORDER BY score DESC LIMIT 2;

    • Hard

      students name | class | grade | score Ava | 7A | A | 63 Jada | 8A | B | 82 Dev | 7B | C | 77 Lena | 8A | B | 72 Cara | 8A | A | 93 Ivo | 7B | B | 78 Kai | 8A | A | 45

      How many rows does this query return? SELECT DISTINCT class, grade FROM students;