Download presentation
Presentation is loading. Please wait.
Published byBranden Newton Modified over 9 years ago
1
Multiple Table Queries 2: Outer Joins, Self Joins, Nested Queries, and Views CS 320
2
Review: Inner Join Queries Retrieve records from multiple tables Tables must have links through primary key/foreign key relationships
3
Problem: What if you want to create a query that retrieves records from two tables even if all of the records don't have matching records in a joined table? Example: Create a query that retrieves the customer ID and name of every customer, along with the purchase dates of all of the customer’s purchases. Do not retrieve duplicate records, and order the values by customer ID. Problem: some customers have not made purchases! SELECT DISTINCT cust.cust_id, cust_name, purch_date FROM candy_customer cust JOIN candy_purchase purch ON cust.cust_id = purch.cust_id ORDER BY cust.cust_id;
4
Solution: Create an Outer Join Retrieves all of the records from one table, and also retrieves the matching columns from a second table Retrieves the records in the first table even if they don’t have matching records in the second table Types of outer joins: Left Right Full (not supported by MySQL)
5
Left Outer Join Retrieves all of the records in the left (first) table, along with matching records in the right (second) table, if they exist General syntax: SELECT field1, field2, … FROM Table1 LEFT OUTER JOIN Table2 ON join_condition WHERE search_condition(s); NOTE: the word "OUTER" is optional
6
Left Outer Join Example SELECT DISTINCT c.cust_id, cust_name, purch_date FROM candy_customer c LEFT JOIN candy_purchase p ON c.cust_id = p.cust_id ORDER BY c.cust_id
7
Right Outer Join Retrieves all of the records in the right (second) table, along with matching records in the left (first) table, if they exist General syntax: SELECT field1, field2, … FROM table_1 RIGHT JOIN table_2 ON join_condition WHERE search_condition(s);
8
Self Joins Join the table to itself using a table alias ACME_EMPLOYEE Employee_ID Employee_First_Name Employee_Last_Name Employee_DOB SupervisorID (FK) SELECT emp.employee_last_name AS employee, sup.employee_last_name AS supervisor FROM acme_employee emp JOIN acme_employee sup ON emp.supervisor_id = sup.employee_id; Employee_ ID Employee_ First_ Name Employe_ Last_ Name Employee_ DOB Supervisor_ ID 1MarySmith7/14/1968 2JohnJones2/19/19721 3JasonSwift11/28/19751
9
Self Joins Employee_ ID Employee_ First_ Name Employe_ Last_ Name Employee_ DOB Supervisor_ ID 1MarySmith7/14/1968 2JohnJones2/19/19721 3JasonSwift11/28/19751 EMP alias Employee_ ID Employee_ First_ Name Employe_ Last_ Name Employee_ DOB Supervisor_ ID 1MarySmith7/14/1968 2JohnJones2/19/19721 3JasonSwift11/28/19751 SUP alias SELECT emp.employee_last_name AS employee, sup.employee_last_name AS supervisor FROM acme_employee emp JOIN acme_employee sup ON emp.supervisor_id = sup.employee_id;
10
Consist of a main query and a subquery Subquery retrieves a search condition value for the main query Uses Alternative way to retrieve data from one table based on a search condition from another table Sometimes it's the only way you can retrieve the data you need Nested Queries
11
Nested Query Syntax SELECT purch_id, purch_date, purch_pounds FROM candy_purchase WHERE prod_id = (SELECT prod_id FROM candy_product WHERE prod_desc = 'Celestial Cashew Crunch')
12
The subquery returns exactly ONE value The SearchColumn in the main query MUST MATCH the SearchColumn in the subquery The SearchColumn is usually a foreign key value Nested Query Example Notes SELECT purch_id, purch_date, purch_pounds FROM candy_purchase WHERE prod_id = (SELECT prod_id FROM candy_product WHERE prod_desc = 'Celestial Cashew Crunch')
13
Use if the subquery might retrieve multiple values Alternate Nested Query Syntax SELECT cust_id, purch_id, purch_date, purch_pounds FROM candy_purchase WHERE cust_id IN (SELECT cust_id FROM candy_customer WHERE cust_type = 'P')
14
The main query in a nested query can have multiple search conditions whose values are specified by separate subqueries: Nested Queries with Multiple Subqueries SELECT purch_id, purch_date FROM candy_purchase WHERE prod_id = (SELECT prod_id FROM candy_product WHERE prod_desc = 'Celestial Cashew Crunch') AND cust_id IN (SELECT cust_id FROM candy_customer WHERE cust_type = 'P')
15
SELECT prod_desc FROM candy_product WHERE prod_id IN (SELECT prod_id FROM candy_purchase WHERE purch_date = '2004-10-29' AND cust_id IN (SELECT cust_id FROM candy_customer WHERE cust_type = 'P')) A subquery can also be a nested query: Note positions of the nesting parentheses Nested Subqueries
16
For many nested queries, you could achieve the same result with a join query Nested may seem more straight-forward than the equivalent join query Downside: Executes more slowly Final result can only come from the main query table Some nested query results can’t be retrieved through a join Notes on Using Nested Queries
17
Query requiring a nested query: Create a query that retrieves the IDs of all customers whose average purchase amount is greater than the average pounds of all customers' purchases SELECT cust_id, AVG(purch_pounds) FROM candy_purchase GROUP BY cust_id HAVING AVG(purch_pounds) > (SELECT AVG(purch_pounds) FROM candy_purchase);
18
Create and debug the subquery(ies) first, then add the main query If the main query doesn’t run, debug it by hard-coding the search condition rather than using the subquery Strategy for Creating and Debugging Nested Queries
19
Views Virtual table based on a query You can think of it as a stored query Purposes: Controls the fields that certain users can access Provides a way to simplify join queries
20
Types of Views Simple view: based on a single table Primary use: hides fields to control access Users can select, insert, update, and delete from simple views just as with tables CREATE OR REPLACE VIEW candy_customer_view AS SELECT cust_id, cust_name, cust_type, cust_addr,cust_zip FROM candy_customer;
21
Types of Views Complex view: created by joining multiple tables Primary use: to simplify queries Users can select from complex views Users CANNOT insert, update, and delete from complex views CREATE OR REPLACE VIEW purchases_view AS SELECT cust_name, purch_date, purch_delivery_date, prod_desc, purch_pounds FROM candy_purchase purch INNER JOIN candy_product prod ON purch.prod_id = prod.prod_id JOIN candy_customer cust ON cust.cust_id = purch.cust_id;
22
Test Yourself: You need to retrieve the description of all candy products, along with the purchase date and pounds purchased. You need to create a(n) _________ query. a. Inner join b. Outer join c. Nested d. View e. None of the above
23
Test Yourself: You need to retrieve the description of all candy products, along with the purchase date and pounds purchased. You need to create a(n) _________ query. a. Inner join b. Outer join c. Nested d. View e. None of the above
24
Test Yourself: How many records does the following query retrieve? a. 9 b. 14 c. 7 d. None of the above SELECT DISTINCT c.cust_id, cust_name, purch_date FROM candy_customer c RIGHT JOIN candy_purchase p ON c.cust_id = p.cust_id ORDER BY c.cust_id
25
Test Yourself: How many records does the following query retrieve? a. 9 b. 14 c. 7 d. None of the above SELECT DISTINCT c.cust_id, cust_name, purch_date FROM candy_customer c RIGHT JOIN candy_purchase p ON c.cust_id = p.cust_id ORDER BY c.cust_id
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.