0% found this document useful (0 votes)
45 views6 pages

Joints, Aggregation Functions and Group by Clause.

Uploaded by

mohamedapdo1212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views6 pages

Joints, Aggregation Functions and Group by Clause.

Uploaded by

mohamedapdo1212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DBMS Laboratory (1)

Lab. 5: Joints, Aggregation Functions and group by clause.

joins in SQL server are used to query (retrieve) data from 2 or more related tables. In general tables are related
to each other using foreign key constraints.
In SQL server, there are different types of JOINS.
1. CROSS JOIN 2. INNER JOIN 3. OUTER JOIN
Outer Joins are again divided into 3 types
1. Left Join or Left Outer Join 2. Right Join or Right Outer Join 3. Full Join or Full Outer Join
Employee Table (tblEmployee)
ID Name Gender Salary DepartmentId
1 Tom Male 4000 1
2 Pam Female 3000 3
3 John Male 3500 1
4 Sam Male 4500 2
5 Todd Male 2800 2
6 Ben Male 7000 1
7 Sara Female 4800 3
8 Valarie Female 5500 1
9 James Male 6500 NULL
10 Russell Male 8800 NULL

Departments Table (tblDepartment)


ID DepartmentName Location DepartmentHead
1 IT London Rick
2 Payroll Delhi Ron
3 HR New York Christie
4 Other Department Sydney Cinderella

 General Formula for Joins:


SELECT ColumnList
FROM LeftTableName
JOIN_TYPE RightTableName
ON JoinCondition
 CROSS JOIN
CROSS JOIN produces the Cartesian product of the 2 tables involved in the join.
Cross Join shouldn't have ON clause
 CROSS JOIN Query:

1
SELECT Name, Gender, Salary, DepartmentName SELECT Name, Gender, Salary, DepartmentName
FROM tblEmployee FROM tblEmployee, tblDepartment
CROSS JOIN tblDepartment
 JOIN or INNER JOIN
Write a query, to retrieve Name, Gender, Salary and DepartmentName of all Employees.
SELECT Name, Gender, Salary, DepartmentName
FROM tblEmployee
INNER JOIN tblDepartment
ON tblEmployee.DepartmentId = tblDepartment.Id
OR
SELECT Name, Gender, Salary, DepartmentName
FROM tblEmployee
JOIN tblDepartment
ON tblEmployee.DepartmentId = tblDepartment.Id

Name Gender Salary DepartmentName


Tom Male 4000 IT
Pam Female 3000 HR
John Male 3500 IT
Sam Male 4500 Payroll
Todd Male 2800 Payroll
Ben Male 7000 IT
Sara Female 4800 HR
Valarie Female 5500 IT

 LEFT JOIN or LEFT OUTER JOIN:

LEFT JOIN, returns all the matching rows + non-matching rows from the left table. In reality, INNER
JOIN and LEFT JOIN are extensively used.

 Write a query, to retrieve Name, Gender, Salary and DepartmentName of all Employees and also return
the non-matching rows in the Employee table.
SELECT Name, Gender, Salary, DepartmentName
FROM tblEmployee
LEFT OUTER JOIN tblDepartment
ON tblEmployee.DepartmentId = tblDepartment.Id
OR

2
SELECT Name, Gender, Salary, DepartmentName
FROM tblEmployee
LEFT JOIN tblDepartment
ON tblEmployee.DepartmentId = tblDepartment.Id

Name Gender Salary DepartmentName

Tom Male 4000 IT

Pam Female 3000 HR

John Male 3500 IT

Sam Male 4500 Payroll

Todd Male 2800 Payroll

Ben Male 7000 IT

Sara Female 4800 HR

Valarie Female 5500 IT

James Male 6500 NULL

Russell Male 8800 NULL

 RIGHT JOIN or RIGHT OUTER JOIN:


RIGHT JOIN, returns all the matching rows + non matching rows from the right table.
 Write a query, to retrieve Name, Gender, Salary and DepartmentName of all Employees and also return
the non-matching rows in the Department table.
SELECT Name, Gender, Salary, DepartmentName
FROM tblEmployee
RIGHT OUTER JOIN tblDepartment
ON tblEmployee.DepartmentId = tblDepartment.Id
OR
SELECT Name, Gender, Salary, DepartmentName
FROM tblEmployee
RIGHT JOIN tblDepartment
ON tblEmployee.DepartmentId = tblDepartment.Id

3
Name Gender Salary DepartmentName
Tom Male 4000 IT
John Male 3500 IT
Ben Male 7000 IT
Valarie Female 5500 IT
Sam Male 4500 Payroll
Todd Male 2800 Payroll
Pam Female 3000 HR
Sara Female 4800 HR
NULL NULL NULL Other Department

 FULL JOIN or FULL OUTER JOIN:


FULL JOIN, returns all rows from both the left and right tables, including the non- matching rows.
 Write a query, to retrieve Name, Gender, Salary and DepartmentName of all Employees and also return
the non-matching rows in the two tables.
SELECT Name, Gender, Salary, DepartmentName
FROM tblEmployee
FULL OUTER JOIN tblDepartment
ON tblEmployee.DepartmentId = tblDepartment.Id
OR
SELECT Name, Gender, Salary, DepartmentName
FROM tblEmployee
FULL JOIN tblDepartment
ON tblEmployee.DepartmentId = tblDepartment.Id

Name Gender Salary DepartmentName


Tom Male 4000 IT
Pam Female 3000 HR
John Male 3500 IT
Sam Male 4500 Payroll
Todd Male 2800 Payroll
Ben Male 7000 IT
Sara Female 4800 HR
Valarie Female 5500 IT
James Male 6500 NULL

4
Russell Male 8800 NULL
NULL NULL NULL Other Department

 Aggregate Functions:
Aggregate functions are functions that take a collection (a set or multiset) of values
as input and return a single value. SQL offers five built-in aggregate functions:

avg min max sum count


 Find the average salary of all male Employees.
select avg(salary) as average_salary
from tblEmployee
where Gender='Male';

average_salary
5300

 Find the total number of employees in the college.


select count (*) as E_count
from tblEmployee
 Find the total number of departments from the Employee table.

select count (distinct DepartmentId) as D_count


from tblEmployee

D_count
3

Q: Find the maximum and minimum salary of all employees in the IT department.

 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 or expressions. It is always used in conjunction with one or more aggregate functions.

 Find average of salaries in each department.


SELECT Departmentname,avg(salary) as D_average
FROM tblEmployee
JOIN tblDepartment
ON tblEmployee.DepartmentId = tblDepartment.Id
group by Departmentname

5
Departmentname D_average
HR 3900

IT 5000
Payroll 3650

 Find number of employees in each gender.

SELECT gender,count(ID) as C_Employee


FROM tblEmployee
group by Gender

Gender C_Employee

Female 3

Male 7
 Note:
Any field that is not present in the group by clause must appear only inside an aggregate function if it
appears in the select clause, otherwise the query is treated as erroneous.

Q: For each department, find the maximum and minimum of salaries as well as the count of employees in each
gender in that department.

Eng.\ Maged Albadany

You might also like