syedmokthiyar / DBMS--EX-04

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

EX-04 Aggregate Function

AIM:

To study and implement aggregate functions, group by and having clause with suitable examples.

THEORY:

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);

1.How many appointments are scheduled in each hour of the day?

Screenshot 2024-05-08 120144

Query:

SELECT strftime('%H', AppointmentDateTime) AS HourOfDay,COUNT(*) AS TotalAppointments
FROM Appointments
GROUP BY HourOfDay
ORDER BY HourOfDay;

Output:

Screenshot 2024-05-08 120413

2.What is the most common diagnosis among patients?

Screenshot 2024-05-08 120504

Query:

SELECT Diagnosis, COUNT(*) AS DiagnosisCount
FROM MedicalRecords
GROUP BY Diagnosis
ORDER BY DiagnosisCount DESC
LIMIT 1;

Output:

Screenshot 2024-05-08 120610

3.How many prescriptions were written by each doctor?

Screenshot 2024-05-08 120735

Query:

SELECT DoctorID, COUNT(*) AS TotalPrescriptions
FROM Prescriptions
GROUP BY DoctorID
ORDER BY DoctorID;

Output:

Screenshot 2024-05-08 120835

4.Write a SQL query to count the number of customers. Return number of customers.

Screenshot 2024-05-08 120946

Query:

SELECT COUNT(*) AS COUNT
FROM customer;

Output:

Screenshot 2024-05-08 121023

5.Write a SQL query to find how many employees have an income greater than 50K?

Screenshot 2024-05-08 121133

Query:

SELECT COUNT(*) AS employees_count
FROM employee
WHERE income > 50000;

Output:

Screenshot 2024-05-08 121215

6.Write a SQL query to find the shortest email address in the customer table?

Screenshot 2024-05-08 121324

Query:

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;

Output:

Screenshot 2024-05-08 121411

7.Write a SQL query to find the total income of employees aged 40 or above.

Screenshot 2024-05-08 121548

Query:

SELECT SUM(income) AS total_income
FROM employee
WHERE age >= 40;

Output:

Screenshot 2024-05-08 121650

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.

Screenshot 2024-05-08 121802

Query:

SELECT occupation, SUM(workhour)
FROM employee1
GROUP BY occupation
HAVING SUM(workhour) > 20;

Output:

Screenshot 2024-05-08 121843

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.

Screenshot 2024-05-08 121925

Query:

SELECT city, AVG(income)
FROM employee
GROUP BY city
HAVING AVG(income) > 500000;

Output:

Screenshot 2024-05-08 122022

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.

Screenshot 2024-05-08 122136

Query

SELECT (age / 5) * 5 AS age_group, MIN(age)
FROM customer1
GROUP BY (age / 5) * 5
HAVING MIN(age) < 25;

Output:

Screenshot 2024-05-08 122258

Result:

Therefore these are some example to implement Aggregate Functions, Group by and Having Clause.

About