Skip to main contentSkip to solution

Table: 'STUDENT'

rollnumbernamecourse
1AjayScience
2AnkitaCommerce
3AnmolScience
4BhartiScience
5KaranCommerce
6MohitArts

Consider the given query:

SELECT COURSE, COUNT(*) FROM STUDENT GROUP BY COURSE HAVING COURSE="Science";

What is the use of the 'HAVING' clause in the above query?

Solution

✅ Correct Option: 2

HAVING filters the groups created by GROUP BY, after grouping and aggregation. Here the table is grouped by course into Science, Commerce and Arts, and HAVING keeps only the Science group. The output is one row: Science, 3.


Option 1: HAVING does not filter columns. Columns are chosen in the SELECT list.

Option 3: HAVING does not sort anything. Sorting is done by ORDER BY.

Option 4: Filtering individual row values before grouping is the job of WHERE, not HAVING.

WHERE filters rows before grouping, while HAVING filters groups after grouping.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question