0% found this document useful (0 votes)
4 views

PRDBM3

The document outlines the use of aggregate functions in SQL, including COUNT, SUM, AVG, MAX, and MIN, along with their syntax and examples. It also explains the GROUP BY and HAVING clauses for filtering grouped results, as well as the creation and deletion of views in a database. The document provides practical examples using a 'students' table to illustrate these concepts.

Uploaded by

Praveena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

PRDBM3

The document outlines the use of aggregate functions in SQL, including COUNT, SUM, AVG, MAX, and MIN, along with their syntax and examples. It also explains the GROUP BY and HAVING clauses for filtering grouped results, as well as the creation and deletion of views in a database. The document provides practical examples using a 'students' table to illustrate these concepts.

Uploaded by

Praveena
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

DATE: EXP NO: PAGE NO:

AIM: Queries using Aggregate functions (COUNT, SUM, AVG, MAX and MIN), GROUP BY, HAVING and
Creation and dropping of Views.

DESCRIPTION:
Aggregate functions perform calculations on a set of values and return a single value.

COUNT() → Counts the number of rows.


SUM() → Returns the sum of values.
AVG() → Returns the average value.
MAX() → Returns the maximum value.
MIN() → Returns the minimum value.
CREATE TABLE students ( roll_number INT PRIMARY KEY, name VARCHAR(15), marks INT, subject VARCHAR(15));

ROLL_NUMBER NAME MARKS SUBJECT


1 Alice 85 Math
2 Bob 75 Math

3 Charlie 90 Science
4 David 65 Math

5 Eve 80 Science
6 Frank 70 Science

7 Grace 95 Math
8 Hannah 88 Science
(a) COUNT() :Counts the number of rows in a table or a specific column
Syntax: SELECT COUNT(column_name) FROM table_name
EXAMPLE: SELECT subject, COUNT(*) AS total_students

FROM student
GROUP BY subject;
OUTPUT:

subject total_students
Math 4
Science 4
(b) SUM():Returns the total sum of a numeric column.
SYNTAX: SELECT SUM(column_name) FROM table_name;

DATABASE MANAGEMENT SYSTEM LABORATORY CSE DEPARTMENT


DATE: EXP NO: PAGE NO:
EXAMPLE: select sum(marks) from totalmarks;
OUTPUT:
subject total_marks
Math 320
Science 328
(c) AVG():Returns the average (mean) of a numeric column.
SYNTAX: SELECT AVG(column_name) FROM table_name;
EXAMPLE: SELECT subject, AVG(marks) AS average_marks FROM student
GROUP BY subject;
OUTPUT:
subject average_marks
Math 80.00
Science 82.00
(d) MAX(): Returns the highest value in a column.j
Syntax : SELECT MAX(column_name) FROM table_name;
Example: SELECT subject, MAX(marks) AS highest_marks FROM student GROUP BY subject;
OUTPUT:
subject highest_marks
Math 95
Science 90
(e) MIN():Returns the lowest value in a column.
SYNTAX: SELECT MIN(column_name) FROM table_name;
EXAMPLE: SELECT subject, MIN(marks) AS lowest_marks FROM student GROUP BY subject;
OUTPUT:
subject lowest_marks
Math 65
Science 70
GROUP BY: groups rows with the same values.
SYNTAX: SELECT column_name, aggregate_function(column_name)FROM table_name GROUP BY
column_name;
EXAMPLE: SELECT name, subject, SUM(marks) AS FROM student GROUP BY name, subject;

OUTPUT:

DATABASE MANAGEMENT SYSTEM LABORATORY CSE DEPARTMENT


DATE: EXP NO: PAGE NO:

ORDER BY:
SYNTAX: SELECT column1, column2, ...FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;
EXAMPLE: SELECT name, marks FROM student ORDER BY marks ASC;
OUTPUT:

name marks
David 65
Frank 70
Bob 75
Eve 80
Alice 85
Hannah 88
Charlie 90
Grace 95

HAVING: filters grouped results based on conditions.


SYNTAX: SELECT column_name, aggregate_function(column_name) FROM table_name GROUP BY
column_name HAVING condition;
EXAMPLE: SELECT subject, AVG(marks) AS average_marks FROM student GROUP BY subject

HAVING AVG(marks) > 80;


OUTPUT:

subject average_marks
Science 82.00
CREATE VIEW: A view is a virtual table based on an SQL query.
SYNTAX: CREATE VIEW view_name AS SELECT column1, column2, ... FROM table_name WHERE
condition;
EXAMPLE: CREATE VIEW high_scorers AS SELECT roll_number, name, marks, subject
FROM student WHERE marks > 80;
Output: View created.

Select from the View:


SELECT * FROM high_scorers;

DATABASE MANAGEMENT SYSTEM LABORATORY CSE DEPARTMENT


DATE: EXP NO: PAGE NO:

rollnumber name Marks subject


1 Alice 85 Math
3 Charlie 90 Science
7 Grace 95 Math
8 Hannah 88 Science

DROP VIEW: Deletes a view from the database.


Syntax: DROP VIEW view_name;

Example: drop view high scorers;


Output: View dropped.

Result: Hence Queries using Aggregate functions is executed successfully.

DATABASE MANAGEMENT SYSTEM LABORATORY CSE DEPARTMENT

You might also like