DB-Basics-Subqueries-and-JOINs
DB-Basics-Subqueries-and-JOINs
and Indices
Table of Content
1. Joins
2. Subqueries
3. Common Table Expressions (CTE)
4. Indices
2
JOINS
Gathering Data From Multiple Tables
Data from Multiple Tables
Sometimes you need data from several tables:
Employees Departments
EmployeeName DepartmentID DepartmentID DepartmentName
Edward 3 3 Sales
John NULL 4 Marketing
5 Purchasing
4
Types of Joins
Inner joins
Left, right and full outer joins
Cross joins
7
INNER vs. OUTER Joins
Inner join
A join of two tables returning only rows matching the join
condition
Left (or right) outer join
Returns the results of the inner join as well as unmatched rows
from the left (or right) table
Full outer join
Returns the results of an inner join along with all unmatched rows
8
Inner Join
Employees Departments
EmployeeID DepartmentID DepartmentID DepartmentName
263 3 3 Sales
270 NULL 4 Marketing
5 Purchasing
Result
EmployeeID DepartmentID DepartmentID DepartmentName
263 3 3 Sales
9
Inner Join Syntax
Join Condition
10
Left Outer Join
Employees Departments
EmployeeID DepartmentID DepartmentID DepartmentName
263 3 3 Sales
270 NULL X 4 Marketing
5 Purchasing
Result
EmployeeID DepartmentID DepartmentID DepartmentName
263 3 3 Sales
270 NULL NULL NULL
11
Left Outer Join Syntax
Join Condition
12
Right Outer Join
Employees Departments
EmployeeID DepartmentID DepartmentID DepartmentName
263 3 3 Sales
270 NULL X 4 Marketing
X 5 Purchasing
Result
EmployeeID DepartmentID DepartmentID DepartmentName
263 3 3 Sales
NULL NULL 4 Marketing
NULL NULL 5 Purchasing
13
Right Outer Join Syntax
14
Full Join
Employees Departments
EmployeeID DepartmentID DepartmentID DepartmentName
263 3 3 Sales
270 NULL X 4 Marketing
X 5 Purchasing
Result
EmployeeID DepartmentID DepartmentID DepartmentName
263 3 3 Sales
270 NULL NULL NULL
NULL NULL 4 Marketing
NULL NULL 5 Purchasing
15
Full Join Syntax
16
Cross Join
Employees Departments
EmployeeID DepartmentID DepartmentID DepartmentName
263 3 3 Sales
270 NULL 4 Marketing
5 Purchasing
Result
EmployeeID DepartmentID DepartmentID DepartmentName
263 3 3 Sales
263 3 4 Marketing
263 3 5 Purchasing
270 NULL 3 Sales
270 NULL 4 Marketing
270 NULL 5 Purchasing
17
Cross Join Syntax
18
Join Overview
Sally 13 18 Accounting
John 10 10 Marketing
Michael 22 12 HR
Bob 11 22 Engineering
Robin 7 8 Sales
Jessica 15 7 Executive
Relation
19
Join Overview
18 Accounting
Sally 13
John 10 10 Marketing
12 HR
Michael 22 22 Engineering
8 Sales
Bob 11
Robin 7 7 Executive
Jessica 15
20
Join Overview
Inner Join
18 Accounting
Sally 13
John 10 10 Marketing
12 HR
Michael 22 22 Engineering
8 Sales
Bob 11
Robin 7 7 Executive
Jessica 15
21
Join Overview
Left Outer Join
18 Accounting
Sally 13 NULL NULL
John 10 10 Marketing
12 HR
Michael 22 22 Engineering
8 Sales
Bob 11 NULL NULL
Robin 7 7 Executive
Jessica 15 NULL NULL
22
Join Overview
Right Outer Join
NULL NULL 18 Accounting
Sally 13
John 10 10 Marketing
NULL NULL 12 HR
Michael 22 22 Engineering
NULL NULL 8 Sales
Bob 11
Robin 7 7 Executive
Jessica 15
23
Join Overview
Full Outer Join
NULL NULL 18 Accounting
Sally 13 NULL NULL
John 10 10 Marketing
NULL NULL 12 HR
Michael 22 22 Engineering
NULL NULL 8 Sales
Bob 11 NULL NULL
Robin 7 7 Executive
Jessica 15 NULL NULL
24
Join Overview
Negated Left Outer Join
18 Accounting
Sally 13 NULL NULL
John 10 10 Marketing
12 HR
Michael 22 22 Engineering
8 Sales
Bob 11 NULL NULL
Robin 7 7 Executive
Jessica 15 NULL NULL
25
Join Overview
Negated Right Outer Join
NULL NULL 18 Accounting
Sally 13
John 10 10 Marketing
NULL NULL 12 HR
Michael 22 22 Engineering
NULL NULL 8 Sales
Bob 11
Robin 7 7 Executive
Jessica 15
26
Join Overview
Negated Outer Join
NULL NULL 18 Accounting
Sally 13 NULL NULL
John 10 10 Marketing
NULL NULL 12 HR
Michael 22 22 Engineering
NULL NULL 8 Sales
Bob 11 NULL NULL
Robin 7 7 Executive
Jessica 15 NULL NULL
27
Problem: Addresses with Towns
Display address information of all employees in "SoftUni"
database. Select first 50 employees.
The exact format of data is shown below.
Order them by FirstName, then by LastName (ascending).
Hint: Use three-way join.
28
Solution: Addresses with Towns
29
Problem: Sales Employees
Find all employees that are in the "Sales" department. Use
"SoftUni" database.
Follow the specified format:
30
Solution: Sales Employees
31
Problem: Employees Hired After
Show all employees that:
Are hired after 1/1/1999
Are either in "Sales" or "Finance" department
32
Solution: Employees Hired After
33
Problem: Employee Summary
Display information about employee's manager and employee's
department .
Show only the first 50 employees.
The exact format is shown below:
34
Solution: Employee Summary
35
☰
☰
☰
Subqueries
Query Manipulati on on Multiple Levels
Subqueries
Use a query’s result as data for another query
Employees
EmployeeID Salary
Query
59 19,000
71 43,300
... ...
Subquery
DepartmentID Name
WHERE DepartmentID IN 10 Finance
37
Subquery Syntax
39
Solution: Min Average Salary
SELECT
MIN(a.AverageSalary) AS MinAverageSalary
FROM
(
Subquery SELECT e.DepartmentID,
AVG(e.Salary) AS AverageSalary
FROM Employees AS e Table Employees
GROUP BY e.DepartmentID
) AS a
40
Common Table Expressions
Reusable Subqueries
Common Table Expressions
Common Table Expressions (CTE) can be considered as "named
subqueries".
They could be used to improve code readability and code reuse.
Usually they are positioned in the beginning of the query.
WITH Employees_CTE
(FirstName, LastName, DepartmentName)
AS
(
SELECT e.FirstName, e.LastName, d.Name
FROM Employees AS e
LEFT JOIN Departments AS d ON
d.DepartmentID = e.DepartmentID
)
45
Clustered Indexes
Clustered index is actually the data itself.
Very useful for fast execution of WHERE, ORDER BY and GROUP BY
clauses.
Maximum 1 clustered index per table
If a table has no clustered index, Keys
its data rows are stored in an
0-99 100-199 200-299
unordered structure (heap).
Data ☰ ☰ ☰ ☰ ☰ ☰ ☰
46
Non-Clustered Indexes (1)
Useful for fast retrieving a single record or a range of records
Maintained in a separate structure in the DB
Tend to be much narrower than the base table
Can locate the exact record(s) with less I/O
47
Non-Clustered Indexes (2)
A non-clustered index has pointers to the actual data rows
(pointers to the clustered index if there is one).
Keys Index
Data ☰ ☰ ☰ ☰ ☰ ☰ ☰ Links ☰ ☰ ☰ ☰ ☰ ☰ ☰
48
Indices Syntax
Index Type
49
Summary
1. Joins
SELECT * FROM Employees AS e
JOIN Departments AS d ON
d.DepartmentId = e.DepartmentID