Assignment 5 (Aggregate Functions)

Oct 31, 2025
Updated 10 hours ago
3 min read

SQL Aggregate Functions: GROUP BY, HAVING & Real Query Examples

Topic: Mastering aggregate functions like AVG, SUM, MAX, MIN, and COUNT with GROUP BY and HAVING in Oracle SQL.

If you've ever needed to summarize data — find the highest salary, count employees per department, or calculate average scores — SQL aggregate functions are your go-to tools. This guide walks you through every major aggregate function with real queries and expected outputs, so you can revise fast and write confidently.


What Are Aggregate Functions in SQL?

Aggregate functions perform a calculation on a set of rows and return a single value. They're widely used in data analysis, reporting dashboards, and backend business logic.

Here's a quick reference table before we dive into the queries:

Function

Description

Example

AVG(expr)

Returns the average of values

AVG(salary)

SUM(expr)

Returns the total of values

SUM(salary)

MIN(expr)

Returns the smallest value

MIN(salary)

MAX(expr)

Returns the largest value

MAX(salary)

COUNT(expr)

Counts rows with non-null values

COUNT(commission_pct)

COUNT(*)

Counts all rows including nulls

COUNT(*)


GROUP BY and HAVING: The Power Duo

Before jumping into queries, two clauses you'll always see alongside aggregate functions:

  • GROUP BY — groups rows that share the same value in a column (e.g., all employees in the same department).

  • HAVING — filters groups after aggregation. Think of it as WHERE, but for grouped results.

💡 Quick rule: Use WHERE to filter rows before grouping. Use HAVING to filter after grouping.


Assignment 5 — All Queries With Expected Output

Query 1: Minimum, Maximum, and Average Salary

sql
SELECT MIN(salary) AS "MIN_SALARY",
       MAX(salary) AS "MAX_SALARY",
       AVG(salary) AS "AVG_SALARY"
FROM employees;

Expected Output:

MIN_SALARY

MAX_SALARY

AVG_SALARY

2000

24000

6461.83


Query 2: Count of Distinct Job Titles

sql
SELECT COUNT(DISTINCT job_id) AS "JOB_TITLES"
FROM employees;

Expected Output:

JOB_TITLES

19


Query 3: Difference Between Maximum and Minimum Salary

sql
SELECT (MAX(salary) - MIN(salary)) AS "SALARY_DIFFERENCE"
FROM employees;

Expected Output:

SALARY_DIFFERENCE

22000


Query 4: Total Salary Expense of the Company

sql
SELECT SUM(salary) AS "TOTAL_SALARY_EXPENSE"
FROM employees;

Expected Output:

TOTAL_SALARY_EXPENSE

691400


Query 5: Average Salary of All Employees

sql
SELECT AVG(salary) AS "AVERAGE_SALARY"
FROM employees;

Expected Output:

AVERAGE_SALARY

6461.83


Query 6: Total Records in the Employee Table

sql
SELECT COUNT(*) AS "TOTAL_RECORDS"
FROM employees;

Expected Output:

TOTAL_RECORDS

107


Query 7: Number of Employees in Each Department

sql
SELECT department_id,
       COUNT(employee_id) AS "NO_OF_EMPLOYEES"
FROM employees
GROUP BY department_id
ORDER BY department_id;

Expected Output (partial):

DEPARTMENT_ID

NO_OF_EMPLOYEES

10

1

20

2

30

6


Query 8: Max and Min Salary Per Department

sql
SELECT department_id,
       MAX(salary) AS "MAX_SAL",
       MIN(salary) AS "MIN_SAL"
FROM employees
GROUP BY department_id;

Expected Output (partial):

DEPARTMENT_ID

MAX_SAL

MIN_SAL

10

4400

4400

30

9400

2500


Query 9: Jobs With Fewer Than 3 Employees (HAVING)

sql
SELECT job_id,
       COUNT(*) AS "EMP_COUNT"
FROM employees
GROUP BY job_id
HAVING COUNT(*) < 3;

Expected Output (partial):

JOB_ID

EMP_COUNT

AD_PRES

1

SA_MAN

2


Query 10: Departments Where Max Salary Exceeds 10,000

sql
SELECT department_id,
       AVG(salary) AS "AVG_SALARY"
FROM employees
GROUP BY department_id
HAVING MAX(salary) > 10000;

Expected Output:

DEPARTMENT_ID

AVG_SALARY

90

19333.33

100

8600.00


Query 11: Job-wise Total Salary (Excluding Managers, Total > 13000)

sql
SELECT job_id,
       SUM(salary) AS "TOTAL_SALARY"
FROM employees
WHERE job_id NOT LIKE '%MAN%'
GROUP BY job_id
HAVING SUM(salary) > 13000
ORDER BY TOTAL_SALARY;

Expected Output:

JOB_ID

TOTAL_SALARY

SA_REP

20000

AD_VP

34000


Common Mistakes to Avoid

  • Using WHERE instead of HAVING for aggregate conditions — this will throw an error in Oracle SQL.

  • Forgetting GROUP BY when selecting non-aggregate columns alongside aggregate functions.

  • Misusing COUNT(column) vs COUNT(*)COUNT(column) ignores NULLs, COUNT(*) counts everything.


Also Explore These Topics

Looking to build on this knowledge? Here are related notes worth checking out:


High authority articles