SQL COUNT function

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

SQL COUNT function

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which query tells you how many records are in the Students table?
A SELECT COUNT(*) FROM Students;
B SELECT SUM(*) FROM Students;
2. How many rows of output does SELECT COUNT(*) FROM Students; produce?
A One row containing a single number
B One row for every record in the table
3. What does COUNT(*) do?
A Adds up the values in every field
B Counts the number of records
4. A table has 25 records. What does SELECT COUNT(*) FROM Students; output?
A 25
B The 25 student names, one per row
5. Which function would you use to find out HOW MANY students there are in a table?
A SUM
B COUNT

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1SELECT COUNT(*) FROM Students; ASELECT COUNT(*) FROM Books;
2SELECT COUNT(StudentID) FROM Students WHERE Year = 10; BThe FROM keyword is missing, so the SQL does not say which table to count the records in.
3To find how many records are in a table called Books, use COUNT rather than listing every record. CSUM was used to count records. SUM adds up the values of a numeric field and cannot be used with *. COUNT(*) should be used.
4To add up the values in a numeric Price field instead of counting records, use SUM, not COUNT. DOutputs one row containing the number of records in the Students table.
5SELECT COUNT(*) Students; ECounts only the records where Year is 10 and outputs that total.
6SELECT SUM(*) FROM Students; FSELECT SUM(Price) FROM Books;

Definitions

COUNT(*)

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
SELECT _____(*) FROM Students;
2.
SELECT COUNT(_) FROM Students;
3.
SELECT COUNT(*) ____ Students;
4.
SELECT COUNT(*) FROM Students _____ Year = 9;
5.
SELECT ___(Points) FROM Students;

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A SELECT COUNT(*)
B FROM Students
C SELECT COUNT(*
Question 2
A SELECT COUNT(StudentID)
B FROM Students
C WHERE Year == 9;
Question 3
A SELECT SUM(*)
B FROM Books
C WHERE Price > 10;
Question 4
A SELECT COUNT(*)
B FROM Students
C WHERE Year = 10
D AND House = Blue;
Question 5
A SELECT COUNT(*)
B FORM Students
C WHERE Points >= 100;

Answer Key

SQL COUNT function
Section A: Quick Questions
Q1
A — SELECT COUNT(*) FROM Students;
Q2
A — One row containing a single number
Q3
B — Counts the number of records
Q4
A — 25
Q5
B — COUNT
Section B: Match the Pairs
Q1
SELECT COUNT(*) FROM Students; → Outputs one row containing the number of records in the Students table.
Q2
SELECT COUNT(StudentID) FROM Students WHERE Year = 10; → Counts only the records where Year is 10 and outputs that total.
Q3
To find how many records are in a table called Books, use COUNT rather than listing every record. → SELECT COUNT(*) FROM Books;
Q4
To add up the values in a numeric Price field instead of counting records, use SUM, not COUNT. → SELECT SUM(Price) FROM Books;
Q5
SELECT COUNT(*) Students; → The FROM keyword is missing, so the SQL does not say which table to count the records in.
Q6
SELECT SUM(*) FROM Students; → SUM was used to count records. SUM adds up the values of a numeric field and cannot be used with *. COUNT(*) should be used.
Section C: Definitions
Q1
COUNT(*) — SQL function that counts records. SELECT COUNT(*) FROM Students; outputs one row with the number of records in the table. It counts rows, whereas SUM adds up values.
Section D: Fill in the Blanks
Q1
SELECT COUNT(*) FROM Students;
Q2
SELECT COUNT(*) FROM Students;
Q3
SELECT COUNT(*) FROM Students;
Q4
SELECT COUNT(*) FROM Students WHERE Year = 9;
Q5
SELECT SUM(Points) FROM Students;
Section E: Spot the Error
Q1
C — The closing bracket is missing from the COUNT function. It should be SELECT COUNT(*).
Q2
C — SQL uses a single = to compare values in a WHERE condition, not ==. It should be WHERE Year = 9;
Q3
A — SUM adds up the values in a numeric field, so it cannot be used with *. To count how many records match, use SELECT COUNT(*).
Q4
D — Text values in a WHERE condition must be inside quotes. It should be AND House = 'Blue';
Q5
B — The keyword is misspelt. It should be FROM, which names the table being counted.