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

Practical - 5

The document discusses the group by and having clauses in SQL. The group by clause groups rows based on column values and is used with aggregate functions. The having clause imposes conditions on groups after they are formed, similar to where but for groups. Examples show using count, group by, and having to group and filter customer data by country.

Uploaded by

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

Practical - 5

The document discusses the group by and having clauses in SQL. The group by clause groups rows based on column values and is used with aggregate functions. The having clause imposes conditions on groups after they are formed, similar to where but for groups. Examples show using count, group by, and having to group and filter customer data by country.

Uploaded by

Awaish Memon
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Database Management System

Practical No : 5

Aim : 1. Group by
2. Having

Group by clause: Group by clause is used to group a selected set of rows into a
set of summary rows by the values of one or more columns. It is always used in
conjunction with one or more aggregate function. This optional clause tells
Oracle/SQL Server to group rows based on distinct values that exists for specified
columns.
Select <columnname 1><columnname 2>...<columnname n>,
Aggregate_function(<expression>) from tablename Where <condition>
Group by <columnname 1><columnname 2>...<columnname n>;

SELECT COUNT(CustomerID), Country


FROM Customers
GROUP BY Country;

Having clause: imposes a condition on group by clause.


SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);

SELECT COUNT(CustomerID), Country


FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;

WHERE is used to filter rows before grouping and HAVING is used to exclude records
after grouping
Write SQL to answer following questions:

1) Display the total expenditure of company on the salary of employees.


2) List the details of the department where maximum number of emps.
3) Find department wise total salary.
4) Find jobwise salary average.
5) Find the name of department taking maximum salary.
6) Find name of department taking minimum salary.
7) Find average salary of clerks.
8) Find average salary of managers and salesman.
9) Find employee with maximum annual income.
10) Find the employee with minimum monthly income.
11) List the emps in the ASC order of job ids of those joined after the
second half of 2014.
12) Find the number of employees earning more than average salary of
employees.
13) List the emps along with their Experience whose Daily Sal is more
than Rs.350.
14) List the emps in asc order of seniority.
15) List all the emps who joined before or after 2001.
16) List the emps who joined in the year 1990.
17) List the emps who joined in 2005.
18) Find the average of experience of all clerks (both ceil and floor
value).
19) List the emps who joined in January.
20) List the emps who have completed 10+ years today.
21) List the employees who are senior to most recently hired employee
working under Lex.
22) Display total amount of order placed by each salesman.
23) Identify the salesman who have placed any order of more than
Rs.10000.

You might also like