Database Management System Lab_9_1738999516683
Database Management System Lab_9_1738999516683
Assignment 9
Problem statement: Using Order By, Group By, With Clause, Having Clause, Distinct
keyword
Theory:
The GROUP BY statement groups rows that have the same values into summary
rows, like "find the number of customers in each country".
The GROUP BY statement is often used with aggregate functions
(COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more
columns.
GROUP BY Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Example:
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
Order By Syntax
The HAVING clause was added to SQL because the WHERE keyword cannot be used with
aggregate functions.
HAVING SyntaxSELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
Eg.
SELECT customer_id, SUM(amount) AS total
FROM Orders
Group by customer_id
HAVING SUM(amount) < 700;
The SQL SELECT DISTINCT Statement
The SELECT DISTINCT statement is used to return only distinct (different) values.
Eg.