Queries using Aggregate functions (COUNT, SUM, AVG, MAX and MIN), GROUP BY, HAVING and Creation and dropping of Views.
Certainly! Below are SQL queries demonstrating the use of aggregate functions (COUNT
, SUM
, AVG
, MAX
, and MIN
), along with GROUP BY
, HAVING
, and the creation and dropping of views:
SELECT
CASE
WHEN marks BETWEEN 90 AND 100 THEN '90-100'
WHEN marks BETWEEN 80 AND 89 THEN '80-89'
WHEN marks BETWEEN 70 AND 79 THEN '70-79'
ELSE 'Below 70'
END AS marks_range,
COUNT(*) AS student_count
FROM students
GROUP BY marks_range
ORDER BY marks_range;
SELECT name, AVG(marks) AS average_marks
FROM students
GROUP BY name;
SELECT
MAX(marks) AS max_marks,
MIN(marks) AS min_marks
FROM students;
SELECT name, AVG(marks) AS average_marks
FROM students
GROUP BY name
HAVING AVG(marks) >= 80;
CREATE VIEW top_students AS
SELECT *
FROM students
WHERE marks >= 90;
DROP VIEW IF EXISTS top_students;