DBMS 5
DBMS 5
:-05
Objective :- Aggregating data from multiple tables.
Theory :-
SQL Clauses
1. Group By Clause : SQL GROUP BY statement is used to arrange identical data into groups. The
GROUP BY statement is used with the SQL SELECT statement. The GROUP BY statement follows the
WHERE clause in a SELECT statement and precedes the ORDER BY clause. The GROUP BY
statement is used with aggregation function.
Syntax :- SELECT column
FROM table_name
WHERE conditions
GROUP BY column
ORDER BY column
Code :- SELECT COMPANY, COUNT(*)
FROM PRODUCT_MAST
GROUP BY COMPANY;
Output :-
2. Having Clause : HAVING clause is used to specify a search condition for a group or an aggregate.
HAVING is used in a GROUP BY clause. If you are not using GROUP BY clause then you can use
HAVING function like a WHERE clause.
Syntax : SELECT column1, column2
FROM table_name
WHERE conditions
GROUP BY column1, column2
HAVING conditions
ORDER BY column1, column2;
Code : SELECT COMPANY, COUNT(*)
FROM PRODUCT_MAST
GROUP BY COMPANY
HAVING COUNT(*)>2;
Output :
3. Order By Clause : The ORDER BY clause sorts the result-set in ascending or descending order. It
sorts the records in ascending order by default. DESC keyword is used to sort the records in descending
order.
Syntax : SELECT column1, column2
FROM table_name
WHERE condition
ORDER BY column1, column2... ASC|DESC;
3. Avg function : The AVG function is used to calculate the average value of the numeric type. AVG
function returns the average of all non-Null values.
Syntax : AVG()
or
AVG( [ALL|DISTINCT] expression )
Code : SELECT AVG(COST)
FROM PRODUCT_MAST;
Output :
4. Max Function : MAX function is used to find the maximum value of a certain column. This function
determines the largest value of all selected values of a column.
Syntax : MAX()
or
MAX( [ALL|DISTINCT] expression )
Code : SELECT MAX(RATE)
FROM PRODUCT_MAST;
Output :
5. Min Function : MIN function is used to find the minimum value of a certain column. This function
determines the smallest value of all selected values of a column.
Syntax : MIN()
or
MIN( [ALL|DISTINCT] expression )
Code : SELECT MIN(RATE)
FROM PRODUCT_MAST;
Output :