Correct option is C
The correct and most preferred query is:
SELECT SECTION, COUNT(*) FROM STUDENT WHERE MARKS < 33 GROUP BY SECTION HAVING COUNT(*) > 0 ORDER BY SECTION;
This query is syntactically correct because:
1.
WHERE clause is used to filter rows before grouping them by
SECTION.
2.
GROUP BY is applied after filtering with the WHERE clause to group the results by the
SECTION.
3. The
HAVING clause is used to filter the groups based on the result of
COUNT(*) after the grouping.
4. The
ORDER BY clause is used to order the final result by
SECTION.
Important Key Points:
1.
WHERE: Filters rows before they are grouped.
2.
GROUP BY: Groups the result set by one or more columns (in this case,
SECTION).
3.
HAVING: Filters the grouped results based on aggregate functions (like
COUNT(*)).
4.
ORDER BY: Sorts the results based on a specific column (here, by
SECTION).
5. The sequence of
WHERE ->
GROUP BY ->
HAVING ->
ORDER BY is the correct and most common way to structure SQL queries.
Knowledge Booster:
·
Option (a): This query is incorrect because the
ORDER BY clause should come after the
GROUP BY clause, and the
WHERE clause cannot follow
GROUP BY. Also, HAVING should be used to filter groups based on aggregate functions, not WHERE.
·
Option (b): This query is incorrect because
HAVING COUNT(*) > 0 should come after
GROUP BY, not before. The order of the clauses is wrong.
·
Option (d): This query is incorrect because
WHERE should come before
GROUP BY. The query tries to filter after grouping, which is not the correct sequence. The filtering condition on
COUNT(*) should be placed in the
HAVING clause, not in the
WHERE clause.