Skip to content
Question
Q.

Srikanth created the following table STUDENT in his database.

Table : STUDENT

RollNoNameClassMarks
1Ritika1240
2Angad1235
3Kaveri1142
4Lalitha1221
5Daniel1144
6Rabindra1139
7Rabia1128

He now wants to count number of students in each class where the number of students is more than 3. He has executed the following query : SELECT MAX(Marks) FROM STUDENT WHERE COUNT(*)>3 GROUP BY Class; But, he got an error. Identify the error(s) and rewrite the query. Also underline the correction(s) done.

CBSECBSE Class XII Board 2022Subjective· 2mImportance★★★★★
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

The error lies in using an aggregate function (COUNT(*)) in the WHERE clause and selecting MAX(Marks) when the goal is to count students; the correct query uses HAVING for filtering grouped results and COUNT(*) in the SELECT clause.

Understanding What Went Wrong

Srikanth's intention is clear: he wants to find out how many students belong to each class, but only for those classes that have more than three students. The query he wrote, however, contains two fundamental mistakes that prevent SQL from executing it.

The first and most critical error is the placement of COUNT(*)>3 in the WHERE clause. In SQL, the WHERE clause filters individual rows before any grouping happens. It examines each row one by one and decides whether to include it. Aggregate functions like COUNT(), SUM(), MAX(), and AVG() work on groups of rows, not individual ones. You cannot ask "Is the count of this single row greater than 3?" because a single row has no count—it simply exists or doesn't. The database engine reads this and throws an error because it doesn't make logical sense at the row level.

What Srikanth needs is the HAVING clause. After GROUP BY organizes rows into groups (in this case, by Class), HAVING steps in to filter those groups based on aggregate conditions. It answers questions like "Does this group of Class 11 students have more than 3 members?" This is exactly the tool for the job.

The second error is subtler but equally important. Srikanth wants to count the number of students in each class, yet his SELECT clause asks for MAX(Marks)—the highest marks in each class. That's a different question entirely. If the goal is to display how many students are in each qualifying class, the SELECT clause should include COUNT(*), which tallies the number of rows in each group. He should also select Class so the output clearly shows which class each count belongs to.

Note

The WHERE clause is a gatekeeper for individual rows; the HAVING clause is a gatekeeper for groups. Remember: filter rows with WHERE, filter groups with HAVING.

The Corrected Query

Here is the rewritten query with corrections underlined:

SELECT <u>Class, COUNT(*)</u> FROM STUDENT GROUP BY Class HAVING <u>COUNT(*)>3</u>; …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.