Joints, Aggregation Functions and Group by Clause.
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
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
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
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
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:
average_salary
5300
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.
5
Departmentname D_average
HR 3900
IT 5000
Payroll 3650
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.