You are on page 1of 18

------------------------------------------------------------------------

IMPORTANT INFO ON JOINING


------------------------------------------------------------------------
PK (coded as PRI) - PRIMARY KEY - determinant column - yung pinaka main basehan ---
employee ID daw yung first column therefore PK in employees table,,,
- sa departments ang pk ay department_id

FK (coded as MUL) - FOREIGN KEY - column that can be seen on another table -
identifies the primary key of another table - the connection from another table
- department ID is the FK in employees connecting to departments - FK needs have
the same content to qualify as FK
- location_id is the FK in departments connecting to locations
- IMPORTANT - FK needs to be connected to a PK.

IN JOINING RULE - N-1 ----> So 2 tables 2-1 - 1 join condition required.


---->sample of join condition ---> WHERE
employees.department_id = departments.department_id;

------------------------------------------------------------------------
*Cartesian Products - The WRONG WAY TO JOIN :) ---> also called cross join
------------------------------------------------------------------------
A Cartesian product is formed when:
A join condition is omitted
A join condition is invalid
All rows in the first table are joined to all rows in the second table

To avoid a Cartesian product, always include a valid join condition in a WHERE


clause.

Cartesian#product: #20x8=160 rows lows selected

SELECT last_name, department_name


FROM employees, departments;

------------------------------------------------------------------------
*Creating Cross Joins
------------------------------------------------------------------------
The CROSS JOIN clause produces the cross-product of two tables.
This is the same as a Cartesian product between the two tables.

SELECT last_name, department_name


FROM employees
CROSS JOIN departments;

+-----------+-----------------+
| last_name | department_name |
+-----------+-----------------+
| King | Administration |
| King | Marketing |
| King | Shipping |
| King | IT |
| King | Sales |
| King | Executive |
| King | Accounting |
| King | Contracting |
| Kochhar | Administration |
| Kochhar | Marketing |
| Kochhar | Shipping |
| Kochhar | IT |
| Kochhar | Sales |
| Kochhar | Executive |
| Kochhar | Accounting |
| Kochhar | Contracting |
| De Haan | Administration |
| De Haan | Marketing |
| De Haan | Shipping |
160 rows in set (0.00 sec)

------------------------------------------------------------------------
*Retrieving Records with Natural Joins
------------------------------------------------------------------------
mysql> SELECT department_id, department_name,
-> location_id, city
-> FROM departments
-> NATURAL JOIN locations ;
+---------------+-----------------+-------------+---------------------+
| department_id | department_name | location_id | city |
+---------------+-----------------+-------------+---------------------+
| 60 | IT | 1400 | Southlake |
| 50 | Shipping | 1500 | South San Francisco |
| 10 | Administration | 1700 | Seattle |
| 90 | Executive | 1700 | Seattle |
| 110 | Accounting | 1700 | Seattle |
| 190 | Contracting | 1700 | Seattle |
| 20 | Marketing | 1800 | Toronto |
| 80 | Sales | 2500 | Oxford |
+---------------+-----------------+-------------+---------------------+
8 rows in set (0.05 sec)

mysql> SELECT *
-> FROM departments;
+---------------+-----------------+------------+-------------+
| department_id | department_name | manager_id | location_id |
+---------------+-----------------+------------+-------------+
| 10 | Administration | 200 | 1700 |
| 20 | Marketing | 201 | 1800 |
| 50 | Shipping | 124 | 1500 |
| 60 | IT | 103 | 1400 |
| 80 | Sales | 149 | 2500 |
| 90 | Executive | 100 | 1700 |
| 110 | Accounting | 205 | 1700 |
| 190 | Contracting | NULL | 1700 |
+---------------+-----------------+------------+-------------+
8 rows in set (0.00 sec)

mysql> SELECT *
-> FROM locations;
+-------------+------------------------------------------+-------------
+---------------------+----------------+------------+
| location_id | street_address | postal_code | city
| state_province | country_id |
+-------------+------------------------------------------+-------------
+---------------------+----------------+------------+
| 1400 | 2014 Jabberwocky Rd | 26192 | Southlake
| Texas | US |
| 1500 | 2011 Interiors Blvd | 99236 | South San
Francisco | California | US |
| 1700 | 2004 Charade Rd | 98199 | Seattle
| Washington | US |
| 1800 | 460 Bloor St. W. | ON M5S 1X8 | Toronto
| Ontario | CA |
| 2500 | Magdalen Centre, The Oxford Science Park | OX9 9ZB | Oxford
| Oxford | UK |
+-------------+------------------------------------------+-------------
+---------------------+----------------+------------+
5 rows in set (0.00 sec)

------------------------------------------------------------------------
*Retrieving Records #with Equijoins -------- equijoin basta may equal sign,,,
literally it means equivalent joining
------------------------------------------------------------------------
SELECT employees.employee_id, employees.last_name,
employees.department_id, departments.department_id,
departments.location_id
FROM employees, departments
WHERE employees.department_id = departments.department_id; --------------join
condition ------- connecting FK and PK

+-------------+-----------+---------------+---------------+-------------+
| employee_id | last_name | department_id | department_id | location_id |
+-------------+-----------+---------------+---------------+-------------+
| 100 | King | 90 | 90 | 1700 |
| 101 | Kochhar | 90 | 90 | 1700 |
| 102 | De Haan | 90 | 90 | 1700 |
| 103 | Hunold | 60 | 60 | 1400 |
| 104 | Ernst | 60 | 60 | 1400 |
| 107 | Lorentz | 60 | 60 | 1400 |
| 124 | Mourgos | 50 | 50 | 1500 |
| 141 | Rajs | 50 | 50 | 1500 |
| 142 | Davies | 50 | 50 | 1500 |
| 143 | Matos | 50 | 50 | 1500 |
| 144 | Vargas | 50 | 50 | 1500 |
| 149 | Zlotkey | 80 | 80 | 2500 |
| 174 | Abel | 80 | 80 | 2500 |
| 176 | Taylor | 80 | 80 | 2500 |
| 200 | Whalen | 10 | 10 | 1700 |
| 201 | Hartstein | 20 | 20 | 1800 |
| 202 | Fay | 20 | 20 | 1800 |
| 205 | Higgins | 110 | 110 | 1700 |
| 206 | Gietz | 110 | 110 | 1700 |
+-------------+-----------+---------------+---------------+-------------+
19 rows in set (0.00 sec)

------------------------------------------------------------------------
*Additional Search Conditions#Using the AND Operator
------------------------------------------------------------------------
Additional Search Conditions
In addition to the join, you may have criteria for your WHERE clause to restrict
the rows under consideration for one or more tables in the join. For example, to
display employee Matos' department number and department name, you need an
additional condition in the WHERE clause.

mysql> SELECT last_name, employees.department_id,


-> department_name
-> FROM employees, departments
-> WHERE employees.department_id = departments.department_id;

+-----------+---------------+-----------------+
| last_name | department_id | department_name |
+-----------+---------------+-----------------+
| King | 90 | Executive |
| Kochhar | 90 | Executive |
| De Haan | 90 | Executive |
| Hunold | 60 | IT |
| Ernst | 60 | IT |
| Lorentz | 60 | IT |
| Mourgos | 50 | Shipping |
| Rajs | 50 | Shipping |
| Davies | 50 | Shipping |
| Matos | 50 | Shipping |
| Vargas | 50 | Shipping |
| Zlotkey | 80 | Sales |
| Abel | 80 | Sales |
| Taylor | 80 | Sales |
| Whalen | 10 | Administration |
| Hartstein | 20 | Marketing |
| Fay | 20 | Marketing |
| Higgins | 110 | Accounting |
| Gietz | 110 | Accounting |
+-----------+---------------+-----------------+
19 rows in set (0.00 sec)

-----------------------------With specific target


SELECT last_name, employees.department_id,department_name
FROM employees, departments
WHERE employees.department_id = departments.department_id
AND last_name = 'Matos';
+-----------+---------------+-----------------+
| last_name | department_id | department_name |
+-----------+---------------+-----------------+
| Matos | 50 | Shipping |
+-----------+---------------+-----------------+
1 row in set (0.00 sec)

FROM QUIZ EXPERIENCE:


mysql> SELECT p.name
-> FROM products p, order_lines o
-> WHERE p.product_code = o.product_code
-> AND o.order_id = 7;
+---------------+
| name |
+---------------+
| Large product |
+---------------+
1 row in set (0.03 sec)

mysql> SELECT p.price


-> FROM products p, order_lines o
-> WHERE p.product_code = o.product_code
-> AND o.line_id = 5;
+-------+
| price |
+-------+
| 5.99 |
+-------+
1 row in set (0.00 sec)

------------------------------------------------------------------------
Qualifying Ambiguous #Column Names --- AMBIGUOUS ERROR WHEN YOU DO NOT SPECIFY A
PREFIX for an FK.
------------------------------------------------------------------------
Use table prefixes to qualify column names that are in multiple tables.
Improve performance by using table prefixes.
Distinguish columns that have identical names but reside in different tables by
using column aliases.

Using prefixes:
Simplify queries by using table aliases.
Improve performance by using table prefixes.

SELECT e.employee_id, e.last_name, e.department_id,


d.department_id, d.location_id
FROM employees e , departments d
WHERE e.department_id = d.department_id;
+-------------+-----------+---------------+---------------+-------------+
| employee_id | last_name | department_id | department_id | location_id |
+-------------+-----------+---------------+---------------+-------------+
| 100 | King | 90 | 90 | 1700 |
| 101 | Kochhar | 90 | 90 | 1700 |
| 102 | De Haan | 90 | 90 | 1700 |
| 103 | Hunold | 60 | 60 | 1400 |
| 104 | Ernst | 60 | 60 | 1400 |
| 107 | Lorentz | 60 | 60 | 1400 |
| 124 | Mourgos | 50 | 50 | 1500 |
| 141 | Rajs | 50 | 50 | 1500 |
| 142 | Davies | 50 | 50 | 1500 |
| 143 | Matos | 50 | 50 | 1500 |
| 144 | Vargas | 50 | 50 | 1500 |
| 149 | Zlotkey | 80 | 80 | 2500 |
| 174 | Abel | 80 | 80 | 2500 |
| 176 | Taylor | 80 | 80 | 2500 |
| 200 | Whalen | 10 | 10 | 1700 |
| 201 | Hartstein | 20 | 20 | 1800 |
| 202 | Fay | 20 | 20 | 1800 |
| 205 | Higgins | 110 | 110 | 1700 |
| 206 | Gietz | 110 | 110 | 1700 |
+-------------+-----------+---------------+---------------+-------------+
19 rows in set (0.00 sec)

FROM QUIZ EXPERIENCE: forgot to put prefix on my condition:


mysql> SELECT last_name, first_name
-> FROM customers c, customer_contacts cc
-> WHERE c.customer_code = cc.customer_code
-> AND customer_code = 'MUSGRP';
ERROR 1052 (23000): Column 'customer_code' in where clause is ambiguous

SELECT last_name, first_name


FROM customers c, customer_contacts cc
WHERE c.customer_code = cc.customer_code
AND c.customer_code = 'MUSGRP';

+-----------+------------+
| last_name | first_name |
+-----------+------------+
| Gershwin | George |
| Britten | Benjamin |
| Lennon | John |
+-----------+------------+
3 rows in set (0.00 sec)

FROM ACTIVITY

mysql> SELECT last_name,e. department_id, department_name


-> FROM employees e, departments d
-> WHERE e.department_id = d.department_id;
+-----------+---------------+-----------------+
| last_name | department_id | department_name |
+-----------+---------------+-----------------+
| King | 90 | Executive |
| Kochhar | 90 | Executive |
| De Haan | 90 | Executive |
| Hunold | 60 | IT |
| Ernst | 60 | IT |
| Lorentz | 60 | IT |
| Mourgos | 50 | Shipping |
| Rajs | 50 | Shipping |
| Davies | 50 | Shipping |
| Matos | 50 | Shipping |
| Vargas | 50 | Shipping |
| Zlotkey | 80 | Sales |
| Abel | 80 | Sales |
| Taylor | 80 | Sales |
| Whalen | 10 | Administration |
| Hartstein | 20 | Marketing |
| Fay | 20 | Marketing |
| Higgins | 110 | Accounting |
| Gietz | 110 | Accounting |
+-----------+---------------+-----------------+
19 rows in set (0.00 sec)

---------------------------------Table Aliases
Qualifying column names with table names can be very time consuming, particularly
if table names are lengthy. You can use table aliases instead of table names. Just
as a column alias gives a column another name, a table alias gives a table another
name. Table aliases help to keep SQL code smaller, therefore using less memory.

Notice how table aliases are identified in the FROM clause in the example. The
table name is specified in full, followed by a space and then the table alias. The
EMPLOYEES table has been given an alias of e, and the DEPARTMENTS table has an
alias of d.

-------->FROM employees e , departments d <-----------

Guidelines
Table aliases can be up to 30 characters in length, but shorter is better.
If a table alias is used for a particular table name in the FROM clause, then
that table alias must be substituted for the table name throughout the
SELECT statement.
Table aliases should be meaningful.
The table alias is valid only for the current SELECT statement.
------------------------------------------------------------------------
*Joining More than Two Tables
------------------------------------------------------------------------
To join n tables together, you need a minimum of n-1 join conditions. For example,
to join three tables, a minimum of two joins is required.

FROM POWERTPOINT: 1ST table has department ID and 2nd table, 2nd and 3rd table has
location_id.

SELECT e.last_name, d.department_name, l.city


FROM employees e, departments d, locations l
WHERE e.department_id = d.department_id
AND d.location_id = l.location_id;

since walang connection ang last_name and city, kelangan gawing tatlo ang from, and
yung two conditions:

e.department_id = d.department_id
AND d.location_id = l.location_id;

+-----------+-----------------+---------------------+
| last_name | department_name | city |
+-----------+-----------------+---------------------+
| Hunold | IT | Southlake |
| Ernst | IT | Southlake |
| Lorentz | IT | Southlake |
| Mourgos | Shipping | South San Francisco |
| Rajs | Shipping | South San Francisco |
| Davies | Shipping | South San Francisco |
| Matos | Shipping | South San Francisco |
| Vargas | Shipping | South San Francisco |
| King | Executive | Seattle |
| Kochhar | Executive | Seattle |
| De Haan | Executive | Seattle |
| Whalen | Administration | Seattle |
| Higgins | Accounting | Seattle |
| Gietz | Accounting | Seattle |
| Hartstein | Marketing | Toronto |
| Fay | Marketing | Toronto |
| Zlotkey | Sales | Oxford |
| Abel | Sales | Oxford |
| Taylor | Sales | Oxford |
+-----------+-----------------+---------------------+
19 rows in set (0.00 sec)

------------------------------------------------------------------------
Joining Multiple Tables - ITO YUNG INSTANCES NA CONNECTION AY BUNGI, THIRD PARTY
CONNECTION KUNG BAGA
------------------------------------------------------------------------
SELECT e.last_name, d.department_name, l.city
FROM employees e, departments d, locations l
WHERE e.department_id = d.department_id
AND d.location_id = l.location_id;

+-----------+-----------------+---------------------+
| last_name | department_name | city |
+-----------+-----------------+---------------------+
| Hunold | IT | Southlake |
| Ernst | IT | Southlake |
| Lorentz | IT | Southlake |
| Mourgos | Shipping | South San Francisco |
| Rajs | Shipping | South San Francisco |
| Davies | Shipping | South San Francisco |
| Matos | Shipping | South San Francisco |
| Vargas | Shipping | South San Francisco |
| King | Executive | Seattle |
| Kochhar | Executive | Seattle |
| De Haan | Executive | Seattle |
| Whalen | Administration | Seattle |
| Higgins | Accounting | Seattle |
| Gietz | Accounting | Seattle |
| Hartstein | Marketing | Toronto |
| Fay | Marketing | Toronto |
| Zlotkey | Sales | Oxford |
| Abel | Sales | Oxford |
| Taylor | Sales | Oxford |
+-----------+-----------------+---------------------+
19 rows in set (0.00 sec)

------------------------------------------------------------------------
*Non-Equijoins
------------------------------------------------------------------------
Non-Equijoins
A non-equijoin is a join condition containing something other than an equality
operator.
The relationship between the EMPLOYEES table and the JOB_GRADES table has an
example of a non-equijoin. A relationship between the two tables is that the SALARY
column in the EMPLOYEES table must be between the values in the LOWEST_SALARY and
HIGHEST_SALARY columns of the JOB_GRADES table. The relationship is obtained using
an operator other than equals (=).

FROM POWER POINT:


mysql> SELECT last_name, salary
-> FROM employees;
+-----------+----------+
| last_name | salary |
+-----------+----------+
| King | 24000.00 |
| Kochhar | 17000.00 |
| De Haan | 17000.00 |
| Hunold | 9000.00 |
| Ernst | 6000.00 |
| Lorentz | 4200.00 |
| Mourgos | 5800.00 |
| Rajs | 3500.00 |
| Davies | 3100.00 |
| Matos | 2600.00 |
| Vargas | 2500.00 |
| Zlotkey | 10500.00 |
| Abel | 11000.00 |
| Taylor | 8600.00 |
| Grant | 7000.00 |
| Whalen | 4400.00 |
| Hartstein | 13000.00 |
| Fay | 6000.00 |
| Higgins | 12000.00 |
| Gietz | 8300.00 |
+-----------+----------+
20 rows in set (0.00 sec)

mysql> SELECT *
-> FROM job_grades;
+-------------+------------+-------------+
| grade_level | lowest_sal | highest_sal |
+-------------+------------+-------------+
| A | 1000 | 2999 |
| B | 3000 | 5999 |
| C | 6000 | 9999 |
| D | 10000 | 14999 |
| E | 15000 | 24999 |
| F | 25000 | 40000 |
+-------------+------------+-------------+
6 rows in set (0.05 sec)

------------------------------------------------------------------------
Retrieving Records #with Non-Equijoins ---- basta walang equal sign, non
equijoin na :)
------------------------------------------------------------------------

SELECT e.last_name, e.salary, j.grade_level ----(pwedeng wag na lagyan ng


prefix if hindi ambiguous
FROM employees e, job_grades j
WHERE e.salary
BETWEEN j.lowest_sal AND j.highest_sal;

+-----------+----------+-------------+
| last_name | salary | grade_level |
+-----------+----------+-------------+
| King | 24000.00 | E |
| Kochhar | 17000.00 | E |
| De Haan | 17000.00 | E |
| Hunold | 9000.00 | C |
| Ernst | 6000.00 | C |
| Lorentz | 4200.00 | B |
| Mourgos | 5800.00 | B |
| Rajs | 3500.00 | B |
| Davies | 3100.00 | B |
| Matos | 2600.00 | A |
| Vargas | 2500.00 | A |
| Zlotkey | 10500.00 | D |
| Abel | 11000.00 | D |
| Taylor | 8600.00 | C |
| Grant | 7000.00 | C |
| Whalen | 4400.00 | B |
| Hartstein | 13000.00 | D |
| Fay | 6000.00 | C |
| Higgins | 12000.00 | D |
| Gietz | 8300.00 | C |
+-----------+----------+-------------+
20 rows in set (0.00 sec)

Non-Equijoins (continued)
The slide example creates a non-equijoin to evaluate an employee�s salary grade.
The salary must be between any pair of the low and high salary ranges.
It is important to note that all employees appear exactly once when this query is
executed. No employee is repeated in the list. There are two reasons for this:
None of the rows in the job grade table contain grades that overlap. That is, the
salary value for an employee can lie only between the low salary and high salary
values of one of the rows in the salary grade table.
All of the employees� salaries lie within the limits provided by the job grade
table. That is, no employee earns less than the lowest value contained in the
LOWEST_SAL column or more than the highest value contained in the HIGHEST_SAL
column.
Note: Other conditions, such as <= and >= can be used, but BETWEEN is the simplest.
Remember to specify the low value first and the high value last when using BETWEEN.

Table aliases have been specified in the slide example for performance reasons, not
because of possible ambiguity.

-
------------------------------------------------------------------------
ENTITY - ENTITY RELATIONSHIP DIAGRAM
------------------------------------------------------------------------
ENTITY - OBJECT PERSONS NEEDS TO BE SAVED IN THE DATABSE - Tables
RELATIONSHIP -

ERD is needed to create DATABASE


------------------------------------------------------------------------
Self Joins
------------------------------------------------------------------------
Joining a Table to Itself
Sometimes you need to join a table to itself. To find the name of each employee�s
manager, you need to join the EMPLOYEES table to itself, or perform a self join.
For example, to find the name of Whalen�s manager, you need to:
Find Whalen in the EMPLOYEES table by looking at the LAST_NAME column.
Find the manager number for Whalen by looking at the MANAGER_ID column. Whalen�s
manager number is 101.
Find the name of the manager with EMPLOYEE_ID 101 by looking at the LAST_NAME
column. Kochhar�s employee number is 101, so Kochhar is Whalen�s manager.
In this process, you look in the table twice. The first time you look in the table
to find Whalen in the LAST_NAME column and MANAGER_ID value of 101. The second time
you look in the EMPLOYEE_ID column to find 101 and the LAST_NAME column to find
Kochhar.

SELECT CONCAT(worker.last_name , ' works for ' ,manager.last_name) "Who's the


BOSS?"
FROM employees worker, employees manager
WHERE worker.manager_id = manager.employee_id ;

+---------------------------+
| Who's the BOSS? |
+---------------------------+
| Kochhar works for King |
| De Haan works for King |
| Hunold works for De Haan |
| Ernst works for Hunold |
| Lorentz works for Hunold |
| Mourgos works for King |
| Rajs works for Mourgos |
| Davies works for Mourgos |
| Matos works for Mourgos |
| Vargas works for Mourgos |
| Zlotkey works for King |
| Abel works for Zlotkey |
| Taylor works for Zlotkey |
| Grant works for Zlotkey |
| Whalen works for Kochhar |
| Hartstein works for King |
| Fay works for Hartstein |
| Higgins works for Kochhar |
| Gietz works for Higgins |
+---------------------------+
19 rows in set (0.00 sec)

------------------------------------------------------------------------
Outer Join -- still under equijoin
------------------------------------------------------------------------
Returning Records with No Direct Match with Outer Joins
If a row does not satisfy a join condition, the row will not appear in the query
result. For example, in the equijoin condition of EMPLOYEES and DEPARTMENTS tables,
employee Grant does not appear because there is no department ID recorded for her
in the EMPLOYEES table. Instead of seeing 20 employees in the result set, you see
19 records.

There are no employees in department 190.

-------sample equijoin for comparison

SELECT e.last_name, e.department_id, d.department_name


FROM employees e, departments d
WHERE e.department_id = d.department_id;

+-----------+---------------+-----------------+
| last_name | department_id | department_name |
+-----------+---------------+-----------------+
| King | 90 | Executive |
| Kochhar | 90 | Executive |
| De Haan | 90 | Executive |
| Hunold | 60 | IT |
| Ernst | 60 | IT |
| Lorentz | 60 | IT |
| Mourgos | 50 | Shipping |
| Rajs | 50 | Shipping |
| Davies | 50 | Shipping |
| Matos | 50 | Shipping |
| Vargas | 50 | Shipping |
| Zlotkey | 80 | Sales |
| Abel | 80 | Sales |
| Taylor | 80 | Sales |
| Whalen | 10 | Administration |
| Hartstein | 20 | Marketing |
| Fay | 20 | Marketing |
| Higgins | 110 | Accounting |
| Gietz | 110 | Accounting |
+-----------+---------------+-----------------+
19 rows in set (0.00 sec)

----------Left Outer Join -


Observations:
- department_ id and department name is in the left part of the departments
table?---hmm,, parang hindi ito,,,
- Last_name variable on the outer left, that doesn't have values/NULL was added. 20
rows now

| Grant | NULL | NULL

- Grant has "NULL' department ID(FK to PK),

- no tuple present

SELECT e.last_name, IFNULL(e.department_id,0), IFNULL(d.department_name,0)


FROM employees e
LEFT OUTER JOIN departments d
ON (e.department_id = d.department_id);

SELECT e.last_name, e.department_id, d.department_name


FROM employees e
LEFT OUTER JOIN departments d
ON (e.department_id = d.department_id);

+-----------+---------------+-----------------+
| last_name | department_id | department_name |
+-----------+---------------+-----------------+
| King | 90 | Executive |
| Kochhar | 90 | Executive |
| De Haan | 90 | Executive |
| Hunold | 60 | IT |
| Ernst | 60 | IT |
| Lorentz | 60 | IT |
| Mourgos | 50 | Shipping |
| Rajs | 50 | Shipping |
| Davies | 50 | Shipping |
| Matos | 50 | Shipping |
| Vargas | 50 | Shipping |
| Zlotkey | 80 | Sales |
| Abel | 80 | Sales |
| Taylor | 80 | Sales |
| Grant | NULL | NULL |
| Whalen | 10 | Administration |
| Hartstein | 20 | Marketing |
| Fay | 20 | Marketing |
| Higgins | 110 | Accounting |
| Gietz | 110 | Accounting |
+-----------+---------------+-----------------+
20 rows in set (0.00 sec)

------Right Outer Join


OBSERVATIONS
- department_name variable on the outer right, that doesn't have values/NULL was
added. 20 rows now

- NULL | NULL | Contracting

- Contracting department ID is not appearing in employees table.

- Contracting has a NULL manager_id---could be not important

- no tuple present

-There are no employees in department 190.

SELECT e.last_name, e.department_id, d.department_name


FROM employees e
RIGHT OUTER JOIN departments d
ON (e.department_id = d.department_id) ;

+-----------+---------------+-----------------+
| last_name | department_id | department_name |
+-----------+---------------+-----------------+
| King | 90 | Executive |
| Kochhar | 90 | Executive |
| De Haan | 90 | Executive |
| Hunold | 60 | IT |
| Ernst | 60 | IT |
| Lorentz | 60 | IT |
| Mourgos | 50 | Shipping |
| Rajs | 50 | Shipping |
| Davies | 50 | Shipping |
| Matos | 50 | Shipping |
| Vargas | 50 | Shipping |
| Zlotkey | 80 | Sales |
| Abel | 80 | Sales |
| Taylor | 80 | Sales |
| Whalen | 10 | Administration |
| Hartstein | 20 | Marketing |
| Fay | 20 | Marketing |
| Higgins | 110 | Accounting |
| Gietz | 110 | Accounting |
| NULL | NULL | Contracting |
+-----------+---------------+-----------------+
20 rows in set (0.00 sec)

SELECT e.last_name, e.department_id, d.department_name


FROM employees e
FULL OUTER JOIN departments d
ON (e.department_id = d.department_id);

+-------------------------+------------------------+
| Company | Total Quantity Ordered |
+-------------------------+------------------------+
| Musicians of America | 8 |
| Presidents Incorporated | 0 |
| Science Corporation | 46 |
+-------------------------+------------------------+
3 rows in set (0.00 sec)

------------------------------------------------------------------------
Retrieving Records with the ON Clause -- still under equijoin
------------------------------------------------------------------------
SELECT e.employee_id, e.last_name, e.department_id,
d.department_id, d.location_id
FROM employees e
JOIN departments d
ON (e.department_id = d.department_id);

+-------------+-----------+---------------+---------------+-------------+
| employee_id | last_name | department_id | department_id | location_id |
+-------------+-----------+---------------+---------------+-------------+
| 100 | King | 90 | 90 | 1700 |
| 101 | Kochhar | 90 | 90 | 1700 |
| 102 | De Haan | 90 | 90 | 1700 |
| 103 | Hunold | 60 | 60 | 1400 |
| 104 | Ernst | 60 | 60 | 1400 |
| 107 | Lorentz | 60 | 60 | 1400 |
| 124 | Mourgos | 50 | 50 | 1500 |
| 141 | Rajs | 50 | 50 | 1500 |
| 142 | Davies | 50 | 50 | 1500 |
| 143 | Matos | 50 | 50 | 1500 |
| 144 | Vargas | 50 | 50 | 1500 |
| 149 | Zlotkey | 80 | 80 | 2500 |
| 174 | Abel | 80 | 80 | 2500 |
| 176 | Taylor | 80 | 80 | 2500 |
| 200 | Whalen | 10 | 10 | 1700 |
| 201 | Hartstein | 20 | 20 | 1800 |
| 202 | Fay | 20 | 20 | 1800 |
| 205 | Higgins | 110 | 110 | 1700 |
| 206 | Gietz | 110 | 110 | 1700 |
+-------------+-----------+---------------+---------------+-------------+
19 rows in set (0.00 sec)

------------------------------------------------------------------------
Creating Three-Way Joins with the ON Clause
------------------------------------------------------------------------
- ON Clause use just like WHERE equaliing of PK = FK. but it can also be used in
connectinG 3 JOINS.

- Also ON clause is used for OUTER JOIN

- Ang on clause it works hand in hand with OUTER JOIN for EQUIJOIN situations? Use
outer join for unique cases like PK value not present in another table. Or FK value
marked as NULL.

SELECT employee_id, city, department_name


FROM employees e
JOIN departments d
ON d.department_id = e.department_id
JOIN locations l
ON d.location_id = l.location_id;
+-------------+---------------------+-----------------+
| employee_id | city | department_name |
+-------------+---------------------+-----------------+
| 103 | Southlake | IT |
| 104 | Southlake | IT |
| 107 | Southlake | IT |
| 124 | South San Francisco | Shipping |
| 141 | South San Francisco | Shipping |
| 142 | South San Francisco | Shipping |
| 143 | South San Francisco | Shipping |
| 144 | South San Francisco | Shipping |
| 100 | Seattle | Executive |
| 101 | Seattle | Executive |
| 102 | Seattle | Executive |
| 200 | Seattle | Administration |
| 205 | Seattle | Accounting |
| 206 | Seattle | Accounting |
| 201 | Toronto | Marketing |
| 202 | Toronto | Marketing |
| 149 | Oxford | Sales |
| 174 | Oxford | Sales |
| 176 | Oxford | Sales |
+-------------+---------------------+-----------------+
19 rows in set (0.00 sec)

--------------------------------------
LEFT OUTER JOIN FROM QUIZ 5
--------------------------------------
BONUS: Retrieve all customer name and the total quantity of products bought by each
customer after February 1, 2006.
Order your query by the total quantity in descending order. If no purchase has been
made by a customer, its total
quantity must display a value of 0. Name the columns as Company and Total Quantity
Ordered, respectively.

SELECT c.name "Company", IFNULL(SUM(quantity),0) "Total Quantity Ordered"


FROM customers c
LEFT OUTER JOIN orders o
ON (c.customer_code = o.customer_code)
LEFT OUTER JOIN order_lines ol
ON o.order_id = ol.order_id
AND o.order_date > '2006-02-01'
GROUP BY c.customer_code
ORDER BY "Company", "Total Quantity Ordered";

+-------------------------+------------------------+
| Company | Total Quantity Ordered |
+-------------------------+------------------------+
| Musicians of America | 8 |
| Presidents Incorporated | 0 |
| Science Corporation | 46 |
+-------------------------+------------------------+

------------------------------------------------------------------------
Summary
------------------------------------------------------------------------
Types of Joins
Equijoins Self joins
Non-equijoins Cross joins
Outer joins Natural joins

Cartesian Products
A Cartesian product results in all combinations of rows displayed. This is done by
either omitting the WHERE clause or specifying the CROSS JOIN clause.

Table Aliases
Table aliases speed up database access.
Table aliases can help to keep SQL code smaller, by conserving memory.

You might also like