Professional Documents
Culture Documents
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.
------------------------------------------------------------------------
*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
------------------------------------------------------------------------
*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.
+-----------+-----------------+
| 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.
+-----------+---------------+-----------------+
| 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)
------------------------------------------------------------------------
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.
+-----------+------------+
| last_name | first_name |
+-----------+------------+
| Gershwin | George |
| Britten | Benjamin |
| Lennon | John |
+-----------+------------+
3 rows in set (0.00 sec)
FROM ACTIVITY
---------------------------------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.
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.
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 (=).
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 :)
------------------------------------------------------------------------
+-----------+----------+-------------+
| 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 -
+---------------------------+
| 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.
+-----------+---------------+-----------------+
| 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)
- no tuple present
+-----------+---------------+-----------------+
| 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)
- no tuple present
+-----------+---------------+-----------------+
| 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)
+-------------------------+------------------------+
| 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.
- 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.
--------------------------------------
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.
+-------------------------+------------------------+
| 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.