Lesson 28 of 50 – Totals Queries in MS Access
56%

Totals Queries in MS Access

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.

1. What Is a Totals Query?

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.

Example: Students Table
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:

  • Total fees collected or recorded.
  • Average fees per student.
  • Total number of student records.
  • Highest and lowest fees.
  • Total fees for each course.

2. Creating a Totals Query in Design View

Follow these steps to create a Totals Query:

  1. Open your MS Access database.
  2. Click the Create tab.
  3. Select Query Design.
  4. Add the Students table.
  5. Add the fields you want to use in the query.
  6. Click the Totals (Σ) button on the Design tab.
  7. A Total row will appear in the query design grid.
  8. Select the required calculation from the Total row.
  9. Click Run (!) to view the results.
  10. Save the query with a meaningful name.
Important: The Totals button adds a Total row to the query grid. You can choose Group By, Sum, Avg, Count, Min, Max, and other available options.

3. Common Totals Query Functions

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

4. Using the SUM Function

The SUM function calculates the total of numeric values in a field.

Example: Calculate Total Fees
SELECT Sum(Fees) AS TotalFees
FROM Students;

For the sample records, the total is 28000.

Result
TotalFees
28000

The alias TotalFees gives a readable name to the calculated column.

5. Using the AVG Function

The AVG function calculates the average of numeric values.

Example: Calculate Average Fees
SELECT Avg(Fees) AS AverageFees
FROM Students;

The total fees are 28000 for four records. Therefore, the average is 7000.

Formula: Average = Sum of values / Number of values

In MS Access SQL, the aggregate function is written as Avg().

6. Using the COUNT Function

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.

Example: Count Student Records
SELECT Count(StudentID) AS TotalStudents
FROM Students;

If the Students table contains four records with StudentID values, the result is 4.

Count Students by Course
SELECT Course, Count(StudentID) AS TotalStudents
FROM Students
GROUP BY Course;

This query counts the students in each course.

7. Using MIN and MAX Functions

The MIN function finds the smallest value, while MAX finds the largest value.

Example: Lowest Fee
SELECT Min(Fees) AS LowestFee
FROM Students;

The result is 4000.

Example: Highest Fee
SELECT Max(Fees) AS HighestFee
FROM Students;

The result is 10000.

Find Both Values in One Query
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.

8. Grouping Records with GROUP BY

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.

Example: Total Fees by Course
SELECT Course, Sum(Fees) AS TotalFees
FROM Students
GROUP BY Course;
Expected Result
Course TotalFees
ADCA 18000
Tally Prime 10000

Each course appears once, with the fees of its students added together.

Count Students and Calculate Average Fees by Course
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.

9. Using WHERE with a Totals Query

You can filter records before calculating totals by adding a WHERE clause.

Example: Total Fees for ADCA Students
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.

Example: Total Fees Above 5000
SELECT Sum(Fees) AS TotalFees
FROM Students
WHERE Fees > 5000;

This query adds only those fee values that are greater than 5000.

Remember: WHERE filters individual records before the aggregate calculation. To filter groups after aggregation, use HAVING.

10. Using HAVING with GROUP BY

The HAVING clause filters groups created by GROUP BY. It is useful when you want to display only groups that meet a summary condition.

Example: Courses with Total Fees Above 10000
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.

Difference:
  • WHERE filters individual records.
  • HAVING filters grouped results.

11. Practical Assignment

Create a Students table with StudentID, StudentName, Course, and Fees fields. Insert at least ten records across multiple courses.

Tasks
  1. Calculate the total fees of all students.
  2. Calculate the average fees.
  3. Count all student records using StudentID.
  4. Find the highest fee.
  5. Find the lowest fee.
  6. Calculate the total fees for each course.
  7. Count the students in each course.
  8. Calculate the average fees for each course.
  9. Calculate total fees for ADCA students only.
  10. Display courses whose total fees are greater than 10000.

Goal: Practice aggregate functions, GROUP BY, WHERE, and HAVING in MS Access.

12. Revision Questions

  1. What is a Totals Query?
  2. What is the purpose of the SUM function?
  3. What does the AVG function calculate?
  4. What is the difference between COUNT and SUM?
  5. What are the purposes of MIN and MAX?
  6. What is the use of GROUP BY?
  7. How does WHERE work with a Totals Query?
  8. What is the difference between WHERE and HAVING?

🧠 Quick Quiz

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?