100 likes | 250 Views
Special Joins. Week 4. Objective. Write SELECT statements to display left, right and full outer joins. Outer Joins. Inner join displays only matching rows Use LEFT OUTER JOIN to display all matching rows and include all unmatched rows from table on left side of JOIN
E N D
Special Joins Week 4
Objective • Write SELECT statements to display left, right and full outer joins
Outer Joins • Inner join displays only matching rows • Use LEFT OUTER JOIN to display all matching rows and include all unmatched rows from table on left side of JOIN • Use RIGHT OUTER JOIN to display all matching rows and include all unmatched rows from table on right side of JOIN • Use FULL OUTER JOIN to display all matching rows and include all unmatched rows from both tables
Employees Table • EMP_ID LAST_NAME DEPT_ID • Whalen10 • Hartstein20 • Fay20 • 124Mourgos50 • 141Rajs 50 • 142Davies50 • 143Matos50 • 144 Vargas50 • 103Hunold60 • 104Ernst60 • 107Lorentz60 • 149Zlotkey80 • 174Higgins 80 • 176 Taylor80 • 100 King90 • 101Kochhar90 • 102De Haan 90 • 205 Higgins110 • 206Gietz 110 • 178 Grant NOTE: Employee 178 not in a dept • 20 rows selected.
Departments Table DEPARTMENT_ID DEPARTMENT_NAME 10Administration 20Marketing 50Shipping 60IT 80Sales 90Executive 110Accounting 190Contracting NOTE: dept 190 has no employees 8 rows selected.
Employees INNER JOIN Departments • EMP_ID LAST_NAME DEPT_IDDEPT_NAME • Whalen10 Administration • Hartstein20Marketing • Fay20Marketing • 124Mourgos50 Shipping • 141Rajs 50Shipping • 142Davies50Shipping • 143Matos50Shipping • 144 Vargas50Shipping • 103Hunold60IT • 104Ernst60IT • 107Lorentz60IT • 149Zlotkey80Sales • 174Higgins 80Sales • 176 Taylor80Sales • 100 King90Executive • 101Kochhar90Executive • 102De Haan 90Executive • 205 Higgins110Accounting • 206Gietz 110Accounting • 19 rows selected.
Employees LEFT OUTER JOIN Departments • EMP_ID LAST_NAME DEPT_IDDEPT_NAME • Whalen10 Administration • Hartstein20Marketing • Fay20Marketing • 124Mourgos50 Shipping • 141Rajs 50Shipping • 142Davies50Shipping • 143Matos50Shipping • 144 Vargas50Shipping • 103Hunold60IT • 104Ernst60IT • 107Lorentz60IT • 149Zlotkey80Sales • 174Higgins 80Sales • 176 Taylor80Sales • 100 King90Executive • 101Kochhar90Executive • 102De Haan 90Executive • 205 Higgins110Accounting • 206Gietz 110Accounting • 178Grant • 20 rows selected. NOTE: Employee 178 is now displayed!
Employees RIGHT OUTER JOIN Departments • EMP_ID LAST_NAME DEPT_IDDEPT_NAME • Whalen10 Administration • Hartstein20Marketing • Fay20Marketing • 124Mourgos50 Shipping • 141Rajs 50Shipping • 142Davies50Shipping • 143Matos50Shipping • 144 Vargas50Shipping • 103Hunold60IT • 104Ernst60IT • 107Lorentz60IT • 149Zlotkey80Sales • 174Higgins 80Sales • 176 Taylor80Sales • 100 King90Executive • 101Kochhar90Executive • 102De Haan 90Executive • 205 Higgins110Accounting • 206Gietz 110Accounting • 190Contracting • 20 rows selected. NOTE: Department 190 is now displayed!
Employees FULL OUTER JOIN Departments • EMP_ID LAST_NAME DEPT_IDDEPT_NAME • Whalen10 Administration • Hartstein20Marketing • Fay20Marketing • 124Mourgos50 Shipping • 141Rajs 50Shipping • 142Davies50Shipping • 143Matos50Shipping • 144 Vargas50Shipping • 103Hunold60IT • 104Ernst60IT • 107Lorentz60IT • 149Zlotkey80Sales • 174Higgins 80Sales • 176 Taylor80Sales • 100 King90Executive • 101Kochhar90Executive • 102De Haan 90Executive • 205 Higgins110Accounting • 206Gietz 110Accounting • 178Grant • 190Contracting • 21 rows selected.NOTE: Emp 178 and Dep 190 both displayed!