Fetching Query Results: fetchone(), fetchall() and rowcount (Class 12 Computer Science)
Updated 17 October 2026 · Fact-checked
After cursor.execute() runs a SELECT, fetchone() returns the next row as a tuple (or None when no rows remain), and fetchall() returns all remaining rows as a list of tuples. rowcount gives the number of rows retrieved by the query once the rows have been fetched.
Understand Fetching Query Results: fetchone(), fetchall() and rowcount
This topic reads data from the STUDENT table of the database school (RollNo, Name, Class, Marks, City; five rows) after cur = con.cursor().
fetchone(). Returns the next single row as a tuple. Each call moves to the next row. When all rows have been read it returns None.
fetchall(). Returns all the remaining rows as a list of tuples; an empty list [] if there are none.
cur.execute("SELECT Name, Marks FROM STUDENT ORDER BY Marks DESC")
print(cur.fetchone())
print(cur.fetchone())
print(cur.fetchall())Output:
('Joy', 91)
('Asha', 88)
[('Meena', 79), ('Ravi', 64), ('Kiran', 55)]The two fetchone() calls consumed the first two rows; fetchall() returned the three that remained.
Looping over rows. Each row is a tuple, so a field is read by index (0 is the first selected column).
cur.execute("SELECT * FROM STUDENT WHERE Marks > 75 ORDER BY RollNo")
data = cur.fetchall()
for r in data:
print(r[1], "scored", r[3])
print(cur.rowcount, "records")Output:
Asha scored 88
Meena scored 79
Joy scored 91
3 recordsrowcount is the number of rows the query produced (for INSERT, UPDATE and DELETE, the number of rows affected). Read it after fetchall(). Do not rely on it before the rows are fetched.
Aggregate queries return one row, so fetchone() is enough:
cur.execute("SELECT COUNT(*), AVG(Marks) FROM STUDENT")
rec = cur.fetchone()
print(rec[0], rec[1])Output: 5 75.4000. MySQL returns AVG as a decimal value (377 / 5 = 75.4, shown here with four decimals).
Result with no rows. For a WHERE that matches nothing, fetchone() returns None and fetchall() returns []; test with if data == []: and print "No record found".
Fetching Query Results: fetchone(), fetchall() and rowcount: key facts
- fetchone and fetchall
- fetchone(): next row as a tuple, None when finished | fetchall(): all remaining rows as a list of tuples, [] when none
- A row field is read by index: r[0], r[1].
- rowcount
- SELECT: number of rows in the result (read after fetchall) | INSERT, UPDATE, DELETE: number of rows affected
- Use it to print the count of records.
- Position of the cursor
- Each fetchone moves to the next row
- fetchall returns only what is left
- After all rows are read, fetchone gives None
- Fetch order follows ORDER BY.
- Single-row queries
- COUNT, SUM, AVG, MAX, MIN give one row
- Use fetchone() and index the result
- AVG is returned as a decimal value
- Example: rec = cur.fetchone(); rec[0].
How to answer Fetching Query Results: fetchone(), fetchall() and rowcount questions
For a fetch question, trace the cursor row by row.
- 1Work out the rows the SELECT returns, in the order given (ORDER BY decides it).
- 2Each fetchone() takes the next unread row; fetchall() takes everything unread.
- 3Write each fetched value in its Python form: a tuple in brackets, a list of tuples in square brackets, strings in quotes.
- 4For a loop, apply the print statement to each row, using indexes for fields.
- 5For rowcount, give the number of rows the query matched.
- 6If the result is empty, fetchone gives None and fetchall gives an empty list.
Fetching Query Results: fetchone(), fetchall() and rowcount in real life
The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.
A topper list
Suppose a teacher needs the top scorer and then the remaining list from the STUDENT table.
Which methods would she use and what does each return?
Try it first, then show the solutionSolution
- Sort by Marks in descending order.
- fetchone() gives the first row, the top scorer ('Joy', 91); fetchall() gives the remaining four rows as a list.
Answer: fetchone: ('Joy', 91); fetchall: the other four rows as a list of tuples.
Memory hook: One tray first, the rest of the plates after.
How many rows matched
Suppose the school wants to print the number of students scoring above 75.
Write the idea and the answer.
Try it first, then show the solutionSolution
- Run the SELECT with Marks > 75, fetch all, and print rowcount.
- Asha 88, Meena 79 and Joy 91 qualify.
Answer: 3
Memory hook: Count the tickets after the fetch.
Tricks to remember Fetching Query Results: fetchone(), fetchall() and rowcount
Short memory aids. Say each one aloud once, then use it on a question the same day.
One versus all
fetchone takes one row; fetchall takes all the rest.
Use it when: Choosing the method.
Row = tuple, result = list
A row is a tuple; fetchall gives a list of those tuples.
Use it when: Types of results.
None after the last
fetchone returns None when no rows remain.
Use it when: Empty result cases.
Index from 0
r[0] is the first selected column.
Use it when: Reading fields.
Fetching Query Results: fetchone(), fetchall() and rowcount: solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: Return type · 1 mark · MCQBoard level
The method that returns all the remaining rows of a query result as a list of tuples is:
(a) fetchone() (b) fetchall() (c) fetchmany() (d) rowcount
Try it first, then show the solutionSolution
- The name says all.
Answer: (b) fetchall()
Example 2: fetchone and fetchall · 2 marks · Short answerBoard level
Differentiate between fetchone() and fetchall().
Try it first, then show the solutionSolution
- fetchone() returns the next single row of the result as a tuple, and None when no rows are left. (1 mark)
- fetchall() returns all the remaining rows as a list of tuples. (1 mark)
Answer: fetchone: one row (tuple); fetchall: all remaining rows (list of tuples).
Example 3: Trace the cursor · 3 marks · Short answerHarder board level
Write the output:
cur.execute("SELECT Name, Marks FROM STUDENT ORDER BY Marks DESC")
print(cur.fetchone())
print(cur.fetchone())
print(cur.fetchall())Try it first, then show the solutionSolution
- Order by marks: Joy 91, Asha 88, Meena 79, Ravi 64, Kiran 55. (0.5 mark)
- First fetchone: ('Joy', 91); second: ('Asha', 88). (1 mark)
- fetchall returns the remaining three: [('Meena', 79), ('Ravi', 64), ('Kiran', 55)]. (1.5 marks)
Answer: ('Joy', 91)
('Asha', 88)
[('Meena', 79), ('Ravi', 64), ('Kiran', 55)]
Example 4: Program with a loop · 3 marks · Short answerHarder board level
Write a program fragment (after the cursor is created) to display the name and marks of the students with marks above 75 in the format "Asha scored 88", and then print the number of such students. Give the output.
Try it first, then show the solutionSolution
(2 marks)cur.execute("SELECT * FROM STUDENT WHERE Marks > 75 ORDER BY RollNo") data = cur.fetchall() for r in data: print(r[1], "scored", r[3]) print(cur.rowcount, "records")- Output: Asha scored 88; Meena scored 79; Joy scored 91; 3 records. (1 mark)
Answer: Asha scored 88
Meena scored 79
Joy scored 91
3 records
Example 5: Case: summary report · 4 marks · Case-basedHOTS / competency
Suppose the principal asks for a one-line summary of the STUDENT table.
cur.execute("SELECT COUNT(*), AVG(Marks), MAX(Marks) FROM STUDENT")
rec = cur.fetchone()
print("Students:", rec[0])
print("Average:", rec[1])
print("Highest:", rec[2])(i) How many rows does this query return, and why does fetchone() suffice? (1 mark)
(ii) Write the output. (2 marks)
(iii) What would a second call to cur.fetchone() return? (1 mark)
Try it first, then show the solutionSolution
- (i) One row, because aggregate functions without GROUP BY give a single result row.
- (ii) COUNT(*) = 5; AVG = (88 + 64 + 79 + 91 + 55) / 5 = 377 / 5 = 75.4, shown by MySQL as 75.4000; MAX = 91. Output: Students: 5, Average: 75.4000, Highest: 91.
- (iii) None, because the only row has been read.
Answer: (i) One row (ii) Students: 5 / Average: 75.4000 / Highest: 91 (iii) None
Where students lose marks in Fetching Query Results: fetchone(), fetchall() and rowcount
The slips examiners see most often, and what to write instead.
Mistake: Writing a row without tuple brackets
fetchone returns a tuple, printed in round brackets with quotes on strings.
Wrong working
Asha 88
Right working
('Asha', 88)
Do this: Write the printed form exactly.
Usually costs: 1 mark
Mistake: Using fetchall before execute
The cursor has no result until execute is called.
Wrong working
data = cur.fetchall()
cur.execute("SELECT * FROM STUDENT")Right working
cur.execute("SELECT * FROM STUDENT")
data = cur.fetchall()Do this: Execute first, then fetch.
Usually costs: 1 mark
Mistake: Reading rowcount before fetching
rowcount is reliable after the rows are fetched.
Wrong working
print(cur.rowcount) just after execute
Right working
data = cur.fetchall()
print(cur.rowcount)Do this: Fetch first, then print rowcount.
Usually costs: 0.5 mark
Exam tips
- Write the output of fetch calls with exact brackets: round for a row, square for the list.
- State the order of rows only when ORDER BY is present; otherwise follow table order.
- Use the index to pick fields: r[1] is the second column of the row.
- For counts, fetchone with an aggregate is neater than fetchall.
- If no record can match, test the fetched list or None before printing.
Practice questions on Fetching Query Results: fetchone(), fetchall() and rowcount
Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.
Q1 · 1 mark · MCQBoard level
What does fetchone() return when there are no more rows to read?
(a) 0 (b) an empty list (c) None (d) an errorHint
Think of nothing.
Answer
(c) None
Q2 · 1 mark · Very short answerBoard level
Each row returned by fetchall() is of which Python type?
Hint
A row is shown in round brackets.
Answer
A tuple (and fetchall itself is a list of such tuples).
Q3 · 2 marks · Short answerHarder board level
After cur.execute("SELECT Name FROM STUDENT WHERE City = 'Delhi' ORDER BY RollNo"), write the value of cur.fetchall().
Hint
Two students live in Delhi.
Answer
[('Asha',), ('Joy',)]. Each row is a one-element tuple, so it is written with a trailing comma.
Q4 · 1 mark · Assertion-reasonHarder board level
Assertion (A): After fetchall() has read the whole result, a call to fetchone() returns None.
Reason (R): fetchall() moves the cursor past the last row of the result.
(a) Both A and R are true and R is the correct explanation of A. (b) Both A and R are true but R is not the correct explanation of A. (c) A is true but R is false. (d) A is false but R is true.Hint
Which rows remain?
Answer
(a) Both are true and R explains A.
Q5 · 3 marks · Short answerHOTS / competency
Write a program fragment that asks for a city name, shows the names and marks of the students from that city, and prints "No record found" if there is none. Explain the test used for "none".
Hint
Use the format or %s from the next topic, then test the list.
Answer
city = input("City: ") cur.execute("SELECT Name, Marks FROM STUDENT WHERE City = %s", (city,)) data = cur.fetchall() if data == []: print("No record found") else: for r in data: print(r[0], r[1])When no row matches, fetchall() returns an empty list, so data == [] is True. For input Delhi it prints Asha 88 and Joy 91.
Test your recall
Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.
1.What does fetchone() return?Show answer
The next row as a tuple, or None.
2.What does fetchall() return?Show answer
A list of tuples (all remaining rows).
3.What does rowcount give after a SELECT with fetchall?Show answer
The number of rows retrieved.
4.What is an empty fetchall result?Show answer
An empty list.
5.How is a column of a fetched row read?Show answer
By index, such as r[0].
6.Which method comes before any fetch?Show answer
execute().
Fetching Query Results: fetchone(), fetchall() and rowcount: frequently asked questions
What is the difference between fetchone() and fetchall()?
fetchone() returns the next row as a tuple (None when finished). fetchall() returns all remaining rows as a list of tuples.
What does rowcount do in Python MySQL connectivity?
It gives the number of rows produced by a SELECT (after fetching) or affected by INSERT, UPDATE or DELETE.
How do I print the rows of a table in Python?
Execute SELECT * FROM table, call fetchall(), and loop over the list, printing each tuple or its fields by index.
Why does my fetchall output look like a list of brackets?
Each row is a tuple and the whole result is a list, so it prints as [(...), (...)]. Index the tuple to print the fields neatly.