To study and implement aggregate functions, group by and having clause with suitable examples.
An aggregate function is a function that performs a calculation on a set of values, and returns a single value. Aggregate functions are often used with the GROUP BY clause of the SELECT statement. The GROUP BY clause splits the result-set into groups of values and the aggregate function can be used to return a single value for each group. The most commonly used SQL aggregate functions are: MIN() - returns the smallest value within the selected column Syntax: SELECT MIN(column_name) FROM table_name WHERE condition; Example: SELECT MIN (Sal) FROM emp;
MAX() - returns the largest value within the selected column Syntax: SELECT MAX(column_name) FROM table_name WHERE condition; Example: SELECT MAX (Sal) FROM emp;
COUNT() - returns the number of rows in a set Syntax: SELECT COUNT(column_name) FROM table_name WHERE condition; Example: SELECT COUNT (Sal) FROM emp;
SUM() - returns the total sum of a numerical column Syntax: SELECT SUM(column_name) FROM table_name WHERE condition; Example: SELECT SUM (Sal) From emp;
AVG() - returns the average value of a numerical column Syntax: SELECT AVG(column_name) FROM table_name WHERE condition; Example: Select AVG (10, 15, 30) FROM DUAL;
GROUP BY: This query is used to group all the records in a relation together for each and every value of a specific key(s) and then display them for a selected set of fields in the relation. Syntax: SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s) ORDER BY column_name(s); Example: SQL> SELECT EMPNO, SUM (SALARY) FROM EMP GROUP BY EMPNO;
GROUP BY-HAVING: The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions. The HAVING clause must follow the GROUP BY clause in a query and must also precede the ORDER BY clause if used. Syntax: SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s) HAVING condition ORDER BY column_name(s);
SELECT strftime('%H', AppointmentDateTime) AS HourOfDay,COUNT(*) AS TotalAppointments
FROM Appointments
GROUP BY HourOfDay
ORDER BY HourOfDay;
SELECT Diagnosis, COUNT(*) AS DiagnosisCount
FROM MedicalRecords
GROUP BY Diagnosis
ORDER BY DiagnosisCount DESC
LIMIT 1;
SELECT DoctorID, COUNT(*) AS TotalPrescriptions
FROM Prescriptions
GROUP BY DoctorID
ORDER BY DoctorID;
SELECT COUNT(*) AS COUNT
FROM customer;
SELECT COUNT(*) AS employees_count
FROM employee
WHERE income > 50000;
SELECT name, email, LENGTH(email) AS min_email_length
FROM customer
WHERE LENGTH(email) = (
SELECT MIN(LENGTH(email))
FROM customer
WHERE email IS NOT NULL
)
LIMIT 1;
SELECT SUM(income) AS total_income
FROM employee
WHERE age >= 40;
8.Write the SQL query that achieves the grouping of data by occupation, calculates the total work hours for each occupation, and excludes occupations where the total work hour sum is not greater than 20.
SELECT occupation, SUM(workhour)
FROM employee1
GROUP BY occupation
HAVING SUM(workhour) > 20;
9.Write the SQL query that achieves the grouping of data by city, calculates the average income for each city, and includes only those cities where the average income is greater than 500,000.
SELECT city, AVG(income)
FROM employee
GROUP BY city
HAVING AVG(income) > 500000;
10.Write the SQL query that accomplishes the grouping of data by age intervals using the expression (age/5)5, calculates the minimum age for each group, and excludes groups where the minimum age is not less than 25.
SELECT (age / 5) * 5 AS age_group, MIN(age)
FROM customer1
GROUP BY (age / 5) * 5
HAVING MIN(age) < 25;
Therefore these are some example to implement Aggregate Functions, Group by and Having Clause.