A Totals Query is used to calculate summary information from a table. It can calculate the sum, average, count, minimum, and maximum values of records. You can also group records by a field to calculate totals for each group.
A Totals Query summarizes data instead of displaying every individual record. For example, a coaching institute can use a Totals Query to calculate total fees, average fees, or the number of students in each course.
| StudentID | StudentName | Course | Fees |
|---|---|---|---|
| 101 | Amit | ADCA | 10000 |
| 102 | Priya | ADCA | 8000 |
| 103 | Rahul | Tally Prime | 4000 |
| 104 | Neha | Tally Prime | 6000 |
Using a Totals Query, you can calculate:
Follow these steps to create a Totals Query:
| Function | Purpose | Example |
|---|---|---|
| Sum | Adds numeric values. | Total fees |
| Avg | Calculates the average. | Average fees |
| Count | Counts non-Null values in a field. | Number of student IDs |
| Min | Finds the smallest value. | Lowest fee |
| Max | Finds the largest value. | Highest fee |
| Group By | Groups records with matching values. | Students by course |
| Where | Applies criteria without displaying the field as a result column. | Filter a course |
The SUM function calculates the total of numeric values in a field.
SELECT Sum(Fees) AS TotalFees
FROM Students;
For the sample records, the total is 28000.
| TotalFees |
|---|
| 28000 |
The alias TotalFees gives a readable name
to the calculated column.
The AVG function calculates the average of numeric values.
SELECT Avg(Fees) AS AverageFees
FROM Students;
The total fees are 28000 for four records. Therefore, the average is 7000.
In MS Access SQL, the aggregate function is written as
Avg().
The COUNT function counts non-Null values in a field. Counting a primary key is a useful way to count records when every record has a unique, non-Null ID.
SELECT Count(StudentID) AS TotalStudents
FROM Students;
If the Students table contains four records with StudentID values, the result is 4.
SELECT Course, Count(StudentID) AS TotalStudents
FROM Students
GROUP BY Course;
This query counts the students in each course.
The MIN function finds the smallest value, while MAX finds the largest value.
SELECT Min(Fees) AS LowestFee
FROM Students;
The result is 4000.
SELECT Max(Fees) AS HighestFee
FROM Students;
The result is 10000.
SELECT Min(Fees) AS LowestFee,
Max(Fees) AS HighestFee
FROM Students;
This query returns the lowest and highest fee values in a single result row.
GROUP BY groups records that have the same value in a specified field. It is commonly used with aggregate functions to calculate a separate result for each group.
SELECT Course, Sum(Fees) AS TotalFees
FROM Students
GROUP BY Course;
| Course | TotalFees |
|---|---|
| ADCA | 18000 |
| Tally Prime | 10000 |
Each course appears once, with the fees of its students added together.
SELECT Course,
Count(StudentID) AS TotalStudents,
Avg(Fees) AS AverageFees
FROM Students
GROUP BY Course;
This query returns the student count and average fees for each course.
You can filter records before calculating totals by adding a WHERE clause.
SELECT Sum(Fees) AS ADCAFees
FROM Students
WHERE Course = "ADCA";
This query calculates the total fees for records where the course is ADCA. Based on the sample data, the result is 18000.
SELECT Sum(Fees) AS TotalFees
FROM Students
WHERE Fees > 5000;
This query adds only those fee values that are greater than 5000.
The HAVING clause filters groups created by GROUP BY. It is useful when you want to display only groups that meet a summary condition.
SELECT Course, Sum(Fees) AS TotalFees
FROM Students
GROUP BY Course
HAVING Sum(Fees) > 10000;
This query displays only courses whose total fees are greater than 10000. In the sample data, ADCA qualifies with 18000, while Tally Prime does not qualify because its total is exactly 10000.
WHERE filters individual records.HAVING filters grouped results.Create a Students table with StudentID, StudentName, Course, and Fees fields. Insert at least ten records across multiple courses.
Goal: Practice aggregate functions, GROUP BY, WHERE, and HAVING in MS Access.
1. Which function calculates the total of numeric values?
2. Which clause groups records with matching field values?
3. Which clause filters groups after aggregation?