Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

CSIT115 Winter 2023

Lab5 – Group By and Subqueries


Group By
Group by statement puts rows with the same value for specific column into groups over
unique values of the column and allows applying aggregate functions on groups rather than
rows.
BASIC Syntax:
SELECT column, aggregate_function(column)
FROM table
Group By column
Consider employee, department and works_in tables below
Employee(eid, ename, address, gender, email, mgrid)
Department( dname, did, budget, location)
Works_in(did,eid)
Query1 how many female employees are working in the company
select gender,count(eid) from employee where gender='F';
This query is not a group by statement cause the aggregate function can be run over
rows by filtering the rows based on gender
Query2 how many employees are working in the company for each gender
This needs a group by statement, because we need to apply function over group of rows
that have the same gender value.
select gender,count(eid) from employee group by (gender);
When you need to write more than one query to generate a summary (use aggregate
functions), in this case one query for male and one query for female then you need to use
group by over the column (in this case gender column), because in most cases you do not know
the list of all the unique values. Group by statement basically find all the unique values in the
column, creates groups, and applies the function to each set of rows under each group.

Group by puts the rows in groups


Table Q1 filters rows then counts then counts
ei
d gender Salary eid gender Salary count Male 3
1 F 15000 1 F 15000 4 2
2 M 23000 3 F 6000 5
3 F 6000 4 F 3000 6
4 F 3000 7 F 22000 Female 4
5 M 3500 1
6 M 7500 3
7 F 22000 4
7
Group by with WHERE condition:
SELECT column, aggregate_function(column)
FROM tables
WHERE conditions
Group By column
Sometimes, before you make the group, you may need to remove the rows you are not
interested in, then apply group by on the filtered rows.
Query 3 show average salary per gender for employees who are not managers
select gender, avg(salary) from employee where mgrid is not null
group by(gender);

Group by with HAVING condition:


SELECT column, aggregate_function(column)
FROM tables
Group By columns
HAVING condition_on_aggregate_function
Sometimes, after you create groups and generate summary, you may want to show
specific groups only. HAVING filters groups based on condition the same way WHERE filters
rows based on conditions.
Query4 show departments with more than two employees.
select dname, count(eid) from department d join works_in w on
d.did=w.did group by (dname) having count(eid)>2;

Group by Complete syntax


SELECT column, aggregate_function(column)
FROM tables
WHERE conditions
Group By column
HAVING condition
Query5 find average employee salary for all departments with more than one
employee and budget higher than 500,000
select dname, avg(salary) from department d
join works_in w on d.did=w.did join employee e on e.eid=w.eid
where budget>500000 group by (dname) having count(w.eid)>1;
Advanced Query: Derived Tables
You can save a query result in a memory by assigning it a name, and then you can use it
the same way you use a table. These queries are referred to as derived tables.
Query6 show employee name and their department for employees working in
departments with more than one employee
select e.ename, w.did from employee e, (select d.did as did from
department d join works_in w on d.did=w.did
group by (d.did) having count(eid)>1) dep, works_in w where
e.eid=w.eid and dep.did=w.did;
TASKS
TASK0 open Lab5_SalesDatabase.sql, edit it as needed and run the file.

TASK1 for each department show number of sales agents, and their average salary

TASK2 how many no discounted product are there for each product category

TASK3 show product name and the total income of all products with more than 1 order

TASK4 show total profit per product category, for product categories that have more than one
product with cost above 2500

profit per product = pprice-pcost

TASK5 count number of product category per department

TASK6 find total profit generated by each agent before discount and after discount along with
agent name and id

TASK7 show product name and order income generated by product for products with more
income from orders than average income generated from all orders

TASK8 show agent names for agents selling products of product category with more than two
products (use derived table)

You might also like