← QCE Digital Solutions Unit 2

Running a SELECT from code and fetching rows

Returns every row from the SELECT that cursor.execute() ran, as a list of tuples. An empty result gives an empty list []. fetchone() returns just one row.

Understanding Running a SELECT from code and fetching rows in QCE Digital Solutions Unit 2

What it does: fetchone() gives one tuple such as (1, 'Pencil'), not a list of tuples. An empty result from fetchall() is [], so comparing rows to [] detects that nothing was found. execute() sends the SQL to the database. fetchall() takes no SQL; it only collects the rows afterwards. By default, sqlite3 returns each row as a tuple, in the same order as the columns in the SELECT. fetchall() returns all the rows of the last query as a list of tuples, and [] when no rows match.

Common mistakes: A for statement must end with a colon. Line 4 should be 'for row in rows:' so it can loop over the list of tuples returned by fetchall(). fetchall() returns a normal Python list, and lists have no .len() method. Use the len() function: print(len(rows)) to count the rows.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 337–409) and word search (Modules 234–409).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line runs a SELECT query from Python using a cursor?
cursor.fetchall("SELECT * FROM students")cursor.execute("SELECT * FROM students")
Matches
rows = cursor.fetchall print(rows)
↓
The brackets are missing after fetchall, so the method is never called. rows holds the method itself, not the data.
Applying
Meanings
Term
cursor.fetchall()
Meaning
Returns every row from the SELECT that cursor.execute() ran, as a list of tuples. An empty result gives an empty list []. fetchone() returns just one row.
Practice
import sqlite3
Testing
Gaps
cursor.("SELECT * FROM students")
Check Answer
Spots
3rows = cur.fetchall()
4for row in rows

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Desert Jerboa' badge — High Jumper, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line runs a SELECT query from Python using a cursor?
A. cursor.execute("SELECT * FROM students") ✓
B. cursor.fetchall("SELECT * FROM students")
execute() sends the SQL to the database. fetchall() takes no SQL; it only collects the rows afterwards.
2. What does cursor.fetchall() return after a SELECT runs?
A. A list of tuples, one tuple per row ✓
B. A single string containing all the data
fetchall() gives a list, and each row in it is a tuple, for example [(1, 'Ava'), (2, 'Liam')].
3. Which is the correct order of steps?
A. rows = cursor.fetchall() then cursor.execute(sql)
B. cursor.execute(sql) then rows = cursor.fetchall() ✓
The query must be executed first. fetchall() only collects rows from the SELECT that execute() has already run.
4. A SELECT matches no rows. What does fetchall() return?
A. An error that stops the program
B. An empty list: [] ✓
An empty result is not an error. fetchall() simply returns an empty list, so you can check it with if not rows.
5. What does cursor.fetchone() return?
A. Just one row, as a single tuple ✓
B. Every row, as a list of tuples
fetchone() returns the next single row, such as (1, 'Ava', 'Nguyen'). fetchall() is the one that returns all rows.

Match the Pairs

cursor.execute("SELECT name, price FROM products") → Sends the SELECT query to the database to run it. The rows are not returned yet; they wait to be fetched.
rows = cursor.fetchall() → Collects every row from the SELECT as a list of tuples, or an empty list [] if nothing matched.
fetchone() returns just the first row as a single tuple, so you can read one record without a list. → row = cursor.fetchone()
A SELECT that matches no rows gives an empty list, so you can check for it with an if statement. → if rows == []:
rows = cursor.fetchall print(rows) → The brackets are missing after fetchall, so the method is never called. rows holds the method itself, not the data.
rows = cursor.fetchall() cursor.execute("SELECT * FROM students") → fetchall() is called before execute(), so there is no query to fetch rows from. The query must be run first.

Key Term

cursor.fetchall()
Returns every row from the SELECT that cursor.execute() ran, as a list of tuples. An empty result gives an empty list []. fetchone() returns just one row.

Fill in the Blanks

1. cursor.execute("SELECT * FROM students")
Hint: Type the cursor method that sends the SQL text to the database and runs it.
2. cursor.execute("SELECT name FROM subjects") rows = cursor.fetchall()
Hint: Type the cursor method that returns every row as a list of tuples.
3. cursor.execute("SELECT * FROM students") row = cursor.fetchone()
Hint: Type the cursor method that returns only a single row as one tuple.
4. rows = cursor.fetchall() if len(rows) == 0: print("No rows found")
Hint: Type the number of rows in the list when the SELECT matches nothing.
5. rows = cursor.fetchall() for row in rows: print(row[0])
Hint: Type the index that picks the first value out of each row tuple.

Spot the Error

Question 1
A cur = conn.cursor()
B cur.execute(sql)
C rows = cur.fetchall()
D for row in rows
E print(row)
Line D — A for statement must end with a colon. Line 4 should be 'for row in rows:' so it can loop over the list of tuples returned by fetchall().
Question 2
A cur = conn.cursor()
B cur.execute(sql)
C rows = cur.fetchall()
D print(rows.len())
Line D — fetchall() returns a normal Python list, and lists have no .len() method. Use the len() function: print(len(rows)) to count the rows.
Question 3
A cur = conn.cursor
B sql = "SELECT * FROM results"
C cur.execute(sql)
D rows = cur.fetchall()
Line A — conn.cursor is a method and must be called with brackets. Without () cur just holds the method, so cur.execute() fails. It should be cur = conn.cursor().
Question 4
A cur = conn.cursor()
B sql = "SELECT * FROM results"
C cur.fetchall(sql)
D row = cur.fetchone()
E print(row)
Line C — It is cur.execute() that runs the SELECT. fetchall() only collects rows after the query has run and takes no query argument. Line 3 should be cur.execute(sql).
Question 5
A cur = conn.cursor()
B sql = SELECT * FROM results
C cur.execute(sql)
D rows = cur.fetchall()
Line B — The SQL statement is passed to execute() as a string, so it must be inside quotes. It should be sql = "SELECT * FROM results".

Student with a class code? Join your class →