Table: 'STUDENT'
rollnumber name course 1 Ajay Science 2 Ankita Commerce 3 Anmol Science 4 Bharti Science 5 Karan Commerce 6 Mohit Arts
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?
Table: 'STUDENT'
| rollnumber | name | course |
|---|---|---|
| 1 | Ajay | Science |
| 2 | Ankita | Commerce |
| 3 | Anmol | Science |
| 4 | Bharti | Science |
| 5 | Karan | Commerce |
| 6 | Mohit | Arts |
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
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.
Related questions:
2026: 23 May Shift 2
2026: 13 May Shift 2
2025: 3 June Shift 2
2026: 29 May Shift 2