/
NOTES FOR JOINS
72 lines (58 loc) · 2.42 KB
/
NOTES FOR JOINS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use Community_Assist
--joins
Select * from Employee
Select PersonLastName, PersonFirstName, PersonEmail, EmployeeHireDate, EmployeeAnnualSalary
from Person
inner join Employee
On Person.PersonKey = Employee.PersonKey
--'on' doesnt have to relate defined primary of foreign keys
--word 'inner' is optional. Joins are 'inner' by default
Select PersonLastName, PersonFirstName, PersonEmail, EmployeeHireDate, EmployeeAnnualSalary
from Person
join Employee
On Person.PersonKey = Employee.PersonKey --'qualified' column name
--you can alias them to save typing
Select PersonLastName, PersonFirstName, PersonEmail, EmployeeHireDate, EmployeeAnnualSalary
from Person p
join Employee e
On p.PersonKey = E.PersonKey
--to select column that appears in both tables, you must alias it to say which table you want it from
Select p.PersonKey, PersonLastName, PersonFirstName, PersonEmail, EmployeeHireDate, EmployeeAnnualSalary
from Person p
join Employee e
On p.PersonKey = E.PersonKey
Select * from EmployeePosition
Select * from Position
--4 table inner join
Select PersonLastName, PersonFirstName, PersonEmail, EmployeeHireDate, PositionName, EmployeeAnnualSalary
from Person
inner Join Employee
on Person.PersonKey=Employee.PersonKey
inner Join EmployeePosition
on Employee.EmployeeKey=EmployeePosition.EmployeeKey
inner join Position
on Position.PositionKey=EmployeePosition.PositionKey
Select PersonLastName, PersonFirstName, PersonEmail, EmployeeHireDate, PositionName, EmployeeAnnualSalary
from Person, Employee, EmployeePosition, Position
Where Person.PersonKey=Employee.PersonKey
And Employee.EmployeeKey=EmployeePosition.EmployeeKey
And Position.PositionKey=EmployeePosition.EmployeeKey
--crossjoin
Select PersonLastName, PersonFirstName, PersonEmail, EmployeeHireDate
from Person, Employee
Select PersonLastName, PersonFirstName, PersonEmail, EmployeeHireDate
from Person
cross join Employee
--outer join
Select GrantTypeName, GrantRequest.GrantTypeKey
from GrantType
left outer join GrantRequest --left refers to first table, right refers to the second table
on GrantType.GrantTypeKey = GrantRequest.GrantTypeKey
Where GRantRequest.GrantTypeKey is null
--inner joins with aggregate functions
Select GrantTypeName, format(avg(GrantRequestAmount),'$#,##0.00') [Average]
From GrantType
inner join GrantRequest
on GrantType.GrantTypeKey=GrantRequest.GrantTypeKey
Group by GrantTypeName --because its not part of the avg function
having avg(grantRequestAmount)>400