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

DBMS 5

DBMS

Uploaded by

manishverma22011
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

DBMS 5

DBMS

Uploaded by

manishverma22011
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Program No.

:-05
Objective :- Aggregating data from multiple tables.

Theory :-

SQL Clauses

The following are the various 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;

SQL Aggregate Functions

The following are the various SQL Aggregation Function :


1. Count Function : COUNT function is used to Count the number of rows in a database table. It can
work on both numeric and non-numeric data types. COUNT function uses the COUNT(*) that returns
the count of all the rows in a specified table. COUNT(*) considers duplicate and Null.
Syntax : COUNT(*)
or
COUNT( [ALL|DISTINCT] expression )
Code : COUNT()
SELECT COUNT(*)
FROM PRODUCT_MAST;
Output :

Code : COUNT with WHERE


SELECT COUNT(*)
FROM PRODUCT_MAST;
WHERE RATE>=20;
Output :

Code : COUNT() with DISTINCT


SELECT COUNT(DISTINCT COMPANY)
FROM PRODUCT_MAST;
Output :

Code : COUNT() with GROUP BY


SELECT COMPANY, COUNT(*)
FROM PRODUCT_MAST
GROUP BY COMPANY;
Output :
2. Sum Function : Sum function is used to calculate the sum of all selected columns. It works on numeric
fields only.
Syntax : SUM()
or
SUM( [ALL|DISTINCT] expression )
Code : SUM()
SELECT SUM(COST)
FROM PRODUCT_MAST;
Output :

Code : SUM() with WHERE


SELECT SUM(COST)
FROM PRODUCT_MAST
WHERE QTY>3;
GROUP BY COMPANY;
Output :

Code : SUM() with HAVING


SELECT COMPANY, SUM(COST)
FROM PRODUCT_MAST
GROUP BY COMPANY
HAVING SUM(COST)>=170;
Output :

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 :

You might also like