Skip to content
Question
Q.

Consider the following table :

Table : RESULTS

STUDENTIDNAMEEXAMIDSCORE
10Leena120
10Leena225
11Samarth130
11Samarth235
12Jai114
12Jai215
14Shoaib130
14Shoaib212

Abhay wants to know the number of students who took the test. He writes the following SQL statement to count STUDENTID without duplicates. However the statement is not correct. Rewrite the correct statement. SELECT DISTINCT(COUNT STUDENTID) FROM RESULTS;

CBSECBSE Class XII Board 2019Subjective· 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 original SQL statement incorrectly applies DISTINCT to the result of COUNT; to count unique student IDs, DISTINCT must be used inside the COUNT function.

When working with databases, a fundamental task is to extract meaningful insights from raw data. Abhay's goal is to determine the number of unique students who participated in the test. This requires identifying each student only once, even if they have multiple entries in the RESULTS table (which they do, as each student took two exams). The concept here is to count distinct occurrences of a particular identifier, in this case, STUDENTID.

The SQL statement Abhay wrote, SELECT DISTINCT(COUNT STUDENTID) FROM RESULTS;, contains a couple of common misunderstandings regarding how aggregate functions and the DISTINCT keyword interact.

First, the COUNT function, like all aggregate functions in SQL, requires its argument to be enclosed in parentheses. So, COUNT STUDENTID is syntactically incorrect; it should be COUNT(STUDENTID). If this were corrected to COUNT(STUDENTID), it would simply count all non-null STUDENTID entries in the RESULTS table. In the given table, there are 8 entries, so COUNT(STUDENTID) would return 8.

Second, applying DISTINCT to the result of an aggregate function like COUNT is generally not what is intended when trying to count unique values within a column. If COUNT(STUDENTID) returns a single value (e.g., 8), then DISTINCT(8) would still just be 8. The DISTINCT keyword needs to operate before the counting happens, on the individual values within the column itself, to filter out duplicates.

To correctly count the number of unique students, we need to tell the COUNT function to consider only the distinct values of STUDENTID before it performs the count. This is achieved by placing the DISTINCT keyword inside the parentheses of the COUNT function, immediately before the column name. …

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.