← RGPV CS-502 DBMS November 2023 solved question paper
← RGPV CS-502 DBMS May 2024 solved question paper
RGPV CS-502 (GS) | November 2023 | Q8(c) | 7 marks
Question (restated): Explain SQL aggregate functions with examples.
Answer
Aggregate functions summarize a set of rows into a single value per group. COUNT counts rows or non-NULL values, SUM totals numeric values, AVG computes the arithmetic mean, and MIN and MAX return the smallest and largest non-NULL values. GROUP BY partitions rows into groups; HAVING filters the resulting groups.
| Dept | Salary |
|---|---|
| HR | 5000 |
| HR | 3000 |
| IT | 6000 |
| IT | NULL |
SELECT Dept, COUNT(*) AS n, COUNT(Salary) AS known, SUM(Salary) AS total, AVG(Salary) AS mean, MIN(Salary) AS minimum, MAX(Salary) AS maximum FROM Employee GROUP BY Dept;
| Dept | n | known | total | mean | minimum | maximum |
|---|---|---|---|---|---|---|
| HR | 2 | 2 | 8000 | 4000 | 3000 | 5000 |
| IT | 2 | 1 | 6000 | 6000 | 6000 | 6000 |
COUNT(*) includes all rows, whereas COUNT(Salary) excludes NULL values. AVG divides by the number of non-NULL salaries, not by the total number of employees. For an empty input COUNT returns zero while SUM, AVG, MIN and MAX generally return NULL. DISTINCT can be used inside suitable aggregates to remove duplicate input values.
SELECT Dept, AVG(Salary) FROM Employee GROUP BY Dept HAVING AVG(Salary)>4500;
This returns IT with average 6000. WHERE Salary>4500 would instead remove individual rows before aggregation and could produce a different result. Aggregate functions are useful for payroll summaries, examination statistics, inventory reports and business analytics. Nonaggregated selected columns normally need to appear in GROUP BY, subject to database-specific functional dependency rules.
Further reading: Silberschatz, Korth and Sudarshan, Database System Concepts; Elmasri and Navathe, Fundamentals of Database Systems.
May 2024 — Q3(b)
Question (restated): Demonstrate aggregate functions using a student database.
Answer
Aggregate functions summarize a group of rows. COUNT(*) counts rows; COUNT(Marks) counts non-null marks; SUM adds non-null values; AVG computes their arithmetic mean; MIN and MAX find extremes. GROUP BY partitions rows, and HAVING filters groups after aggregation. WHERE filters individual rows before grouping. Consider STUDENT(RollNo, Name, Dept, Marks) with rows (1,Asha,CSE,80), (2,Ravi,CSE,60), (3,Meera,ECE,90), (4,Dev,ECE,NULL). COUNT(*) is 4, COUNT(Marks) is 3, SUM is 230, AVG is 76.6667 approximately, MIN is 60 and MAX is 90. Department averages are CSE 70 and ECE 90. NULL is ignored by these aggregates except COUNT(*); an empty input gives zero for COUNT and normally NULL for other aggregates. Use aliases for readable output.
SELECT COUNT(*) AS total, COUNT(Marks) AS graded, SUM(Marks), AVG(Marks), MIN(Marks), MAX(Marks) FROM STUDENT; SELECT Dept, AVG(Marks) AS avg_marks FROM STUDENT GROUP BY Dept HAVING AVG(Marks) >= 70;

Discussion
Ask a question, share your thoughts or report an error.
Sign in to participate in the discussion.
Join the discussion