PRDBM3
PRDBM3
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.
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;
OUTPUT:
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
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.