ADVANCED SQL SELECT QUERIES CS 260 Database Systems.

Slides:



Advertisements
Similar presentations
Advanced SQL Topics Edward Wu.
Advertisements

Advanced SQL (part 1) CS263 Lecture 7.
Multiple Table Queries
© Abdou Illia MIS Spring 2014
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Join Queries CS 146. Introduction: Join Queries So far, our SELECT queries have retrieved data from a single table Usually queries combine data from multiple.
Chapter 4 Joining Multiple Tables
A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
MULTIPLE-TABLE QUERIES
Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs WHERE SUM (duration) = 100; (this will give an error)
Multiple Table Queries 2: Outer Joins, Self Joins, Nested Queries, and Views CS 320.
Action Queries CS 320. Review: SQL Command Types  Data Definition Language (DDL)  Used to create and modify database objects  Data Manipulation Language.
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
1 DDL – subquery Sen Zhang. 2 Objectives What is a subquery? Learn how to create nested SQL queries Read sample scripts and book for different kinds of.
XP Chapter 3 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Analyzing Data For Effective Decision Making.
1 Minggu 4, Pertemuan 8 SQL: Data Manipulation (Cont.) Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Creating Database Tables CS 320. Review: Levels of data models 1. Conceptual: describes WHAT data the system contains 2. Logical: describes HOW the database.
Chapter 6 SQL: Data Manipulation Cont’d. 2 ANY and ALL u ANY and ALL used with subqueries that produce single column of numbers u ALL –Condition only.
Inner join, self join and Outer join Sen Zhang. Joining data together is one of the most significant strengths of a relational database. A join is a query.
Analyzing Data For Effective Decision Making Chapter 3.
Chapter 9 Joining Data from Multiple Tables
ITBIS373 Database Development
A Guide to MySQL 5. 2 Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables Use a subquery.
CSC271 Database Systems Lecture # 12. Summary: Previous Lecture  Row selection using WHERE clause  WHERE clause and search conditions  Sorting results.
Chapter 6 SQL: Data Manipulation (Advanced Commands) Pearson Education © 2009.
INTRODUCTION TO DATABASES CS 260 Database Systems.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Single-Table Queries 1: Basics CS 320 Online. Review: SQL Command Types  Data Definition Language (DDL)  Used to create and modify database objects.
Using Special Operators (LIKE and IN)
Joins & Sub-queries. Oracle recognizes that you may want data that resides in multiple tables drawn together in some meaningful way. One of the most important.
JOI/1 Data Manipulation - Joins Objectives –To learn how to join several tables together to produce output Contents –Extending a Select to retrieve data.
Creating PHPs to Insert, Update, and Delete Data CS 320.
Chapter 4 Multiple-Table Queries
M1G Introduction to Database Development 5. Doing more with queries.
XP Chapter 3 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Analyzing Data For Effective Decision Making Chapter.
1 Multiple Table Queries. 2 Objectives  Retrieve data from more than one table by joining tables  Using IN and EXISTS to query multiple tables  Nested.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
Advanced SELECT Queries CS 146. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data Note the default formats… SELECT.
Introduction to Databases Queries CS 146. Sample Database: CANDY_CUSTOMER CANDY_PURCHASE CANDY_CUST_TYPE CANDY_PRODUCT.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Multiple Table Queries 1: Inner Joins CS 320. Introduction: Join Queries Usually queries combine data from multiple tables:  List how much (pounds) of.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
SQL ACTION QUERIES AND TRANSACTION CONTROL CS 260 Database Systems.
DATA NORMALIZATION CS 260 Database Systems. Overview  Introduction  Anomalies  Functional dependence  Normal forms  1NF  2NF  3NF  BCNF  Denormalization.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
Single-Table Queries 2: Advanced Topics CS 320. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data SELECT field1, field2,
# 1# 1 QueriesQueries How do we ask questions of the data? What is SELECT? What is FROM? What is WHERE? What is a calculated field? Spring 2010 CS105.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
Join Queries CS 146. Introduction: Join Queries  So far, our SELECT queries have retrieved data from a single table  Usually queries combine data from.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
Slide 1 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla In this session, you will learn to: Query data by using joins Query.
Copyright © 2016 Pearson Education, Inc. CHAPTER 7: ADVANCED SQL (PART I) Modern Database Management 12 th Edition Jeff Hoffer, Ramesh Venkataraman, Heikki.
CSC314 DAY 9 Intermediate SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall USING AND DEFINING VIEWS  Views provide users controlled.
Introduction to Databases Queries CS 146. Sample Database: CANDY_CUSTOMER CANDY_PURCHASE CANDY_CUST_TYPE CANDY_PRODUCT.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
MySQL Subquery Source: Dev.MySql.com
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
References: Text Chapters 8 and 9
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
Database Systems: Design, Implementation, and Management Tenth Edition
David M. Kroenke and David J
Using the Set Operators
Chapter Name SQL: Data Manipulation
Creating and Maintaining
Contents Preface I Introduction Lesson Objectives I-2
Database Systems: Design, Implementation, and Management Tenth Edition
Presentation transcript:

ADVANCED SQL SELECT QUERIES CS 260 Database Systems

Overview  Queries using multiple tables  Join queries Inner Outer Self  Nested queries  Queries using set operations  Views

Queries Using Multiple Tables  So far, our SELECT queries have retrieved data from a single table  Often queries require combining data from multiple tables  List the name of each product and how many pounds of it was purchased today  List the customer name and product name for a specific purchase

Sample Database (CANDY) CANDY_CUSTOMER CANDY_PURCHASE CANDY_CUST_TYPE CANDY_PRODUCT

Queries Using Multiple Tables  Approaches  Join queries Join tables on primary key/foreign key relationships  Nested queries Use the output of one query in a search condition in a second query

Overview  Queries using multiple tables  Join queries Inner Outer Self  Nested queries  Queries using set operations  Views

Join Queries  Inner join example CANDY_CUSTOMER CANDY_PURCHASE CANDY_PRODUCT I want to display the purchase date and product description for all products purchased by “Bobby Bon Bons”

Join Queries  An inner join retrieves data from multiple tables where matching column values exist according to the join condition  Inner join syntax  The “ON” clause is referred to as the “join condition” The join condition should join a foreign key in one table to another table’s primary key  The word "INNER" is optional (default join) SELECT Column1, Column2, … FROM Table1 INNER JOIN Table2 ON Table1.JoinColumn = Table2.JoinColumn WHERE SearchCondition(s)

Join Queries

Join Queries  Inner join example  For an INNER JOIN… Order of tables in FROM clause doesn’t matter Order of tables in join condition doesn’t matter SELECT cust_id, purch_id, purch_date, prod_desc FROM candy_purchase JOIN candy_product ON candy_purchase.prod_id = candy_product.prod_id WHERE cust_id = 7

Join Queries  Alternate inner join syntax  Tables to be joined are listed in the FROM, separated by commas  The join condition is listed in the WHERE  What happens if you forget the join in the WHERE clause? SELECT cust_id, purch_id, purch_date, prod_desc FROM candy_purchase, candy_product WHERE candy_purchase.prod_id = candy_product.prod_id AND cust_id = 7

Join Queries  Alternate inner join syntax (join conditions omitted) SELECT parent_id, child_id FROM pc_parent, pc_child Expected Result Actual Result

Join Queries  Alternate inner join syntax (join conditions omitted)  With the WEHRE clause omitted, you get a “product” of the tables  Every record in the first table is combined with every record in the second table # of returned rows = # of rows in 1 st table * # of rows in 2 nd table  If 3 or more tables have omitted join conditions # of returned rows is the product of the # of rows in each table Potentially returns millions of records, slowing down network and server

Join Queries  Omitting join conditions in original inner join syntax  Oracle requires the ON clause, so this would generate an error  MySQL does not require the ON clause, so this would return a product of the number of rows in each table SELECT cust_id, purch_id, purch_date, prod_desc FROM candy_purchase JOIN candy_product WHERE cust_id = 7; SELECT cust_id, purch_id, purch_date, prod_desc FROM candy_purchase JOIN candy_product ON candy_purchase.prod_id = candy_product.prod_id WHERE cust_id = 7;

Join Queries  Qualifying field names  What if a join query retrieves a field that exists in both tables?

Join Queries  Qualifying field names  If multiple columns with the same name in different tables are referenced, they must be “qualified” Preface the field name with the name of either table

Join Queries  Qualifying field names  Are the following two queries the same? SELECT purch_id, candy_customer.cust_id, cust_name FROM candy_purchase JOIN candy_customer ON candy_customer.cust_id = candy_purchase.cust_id WHERE candy_customer.cust_id = 5 SELECT purch_id, candy_purchase.cust_id, cust_name FROM candy_purchase JOIN candy_customer ON candy_customer.cust_id = candy_purchase.cust_id WHERE candy_customer.cust_id = 5

Join Queries  Table aliases  Names associated with tables for use in a single query  Aliases must be used everywhere in the query

Join Queries  Inner joins with more than 2 tables  Placing each INNER JOIN and ON clause on a separate line can make the query easier to read and understand  Multiple inner joins may be specified in any order SELECT Column1, Column2, … FROM Table1 INNER JOIN Table2 ON Table1.JoinColumn = Table2.JoinColumn INNER JOIN Table3 ON Table2.JoinColumn = Table3.JoinColumn WHERE SearchCondition(s)

Join Queries  Inner join example  Sometimes you need to include a table in join queries to provide needed links even if you don't include fields from the table in the SELECT clause SELECT prod_desc FROM candy_product JOIN candy_purchase ON candy_product.prod_id = candy_purchase.prod_id JOIN candy_customer ON candy_purchase.cust_id = candy_customer.cust_id WHERE cust_name = 'Bobby Bon Bons'

Sample Database (CANDY) CANDY_CUSTOMER CANDY_PURCHASE CANDY_CUST_TYPE CANDY_PRODUCT

Overview  Queries using multiple tables  Join queries Inner Outer Self  Nested queries  Queries using set operations  Views

Join Queries  Outer joins  What if you want to create a query that retrieves records from one table, even if the records do not have matching records in one of the joined tables? Inner joins do not accomplish this  Example Create a query that retrieves the following: Customer ID and name of every customer The purchase dates of all of their purchases (if any) Without duplicate records Ordered by customer ID

Sample Database (CANDY) CANDY_CUSTOMER CANDY_PURCHASE CANDY_CUST_TYPE CANDY_PRODUCT

Join Queries  Outer joins  First attempt at example query What happened to customers 1, 3, and 8? SELECT DISTINCT c.cust_id, cust_name, purch_date FROM candy_customer c JOIN candy_purchase p ON c.cust_id = p.cust_id ORDER BY c.cust_id

Join Queries  Outer joins  Retrieve all of the records from one table, plus the matching records from a second table, according to the join condition 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

Join Queries  Left outer joins  Retrieves all of the records in the left (first) table, along with the joined records in the right (second) table if they exist (otherwise NULL values are present for result columns from the right table) The “OUTER” term is optional and often omitted The order of the tables in the FROM clause matters SELECT Column1, Column2, … FROM Table1 LEFT OUTER JOIN Table2 ON JoinCondition WHERE SearchCondition(s);

Join Queries

Join Queries  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

Join Queries  Right outer joins  Retrieves all of the records in the right (second) table, along with the joined records in the left (first) table if they exist (otherwise NULL values are present for result columns from the right table) Again, the “OUTER” term is optional and often omitted Again, the order of the tables in the FROM clause matters SELECT Column1, Column2, … FROM Table1 RIGHT OUTER JOIN Table2 ON JoinCondition WHERE SearchCondition(s);

Join Queries

Join Queries  Right outer join examples SELECT DISTINCT c.cust_id, cust_name, purch_date FROM candy_purchase p RIGHT JOIN candy_customer c ON c.cust_id = p.cust_id ORDER BY c.cust_id 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

Sample Database (CANDY) CANDY_CUSTOMER CANDY_PURCHASE CANDY_CUST_TYPE CANDY_PRODUCT

Join Queries  Full outer joins  Retrieves all of the joined records in both tables if they exist (otherwise NULL values are present for result columns from the table in which a joined record does not exist) Assuming a purchase record was present without an associated cust_id

Join Queries

Join Queries  General approach to outer joins with 3 or more tables Multiple joins are processed from top to bottom Table that you want to display ALL records from must always use an outer join If the query contains subsequent joins, you must continue to join the result to other tables using an outer join Create a query that retrieves: Every customer ID and name The description of every product the customer has ever purchased Without duplicate records Ordered by customer ID

Join Queries  Outer joins with 3 or more tables SELECT DISTINCT c.cust_id, cust_name, prod_desc FROM candy_customer c LEFT JOIN candy_purchase p ON c.cust_id = p.cust_id LEFT JOIN candy_product pr ON p.prod_id = pr.prod_id ORDER BY c.cust_id

Overview  Queries using multiple tables  Join queries Inner Outer Self  Nested queries  Queries using set operations  Views

Join Queries  Self joins  Sometimes database tables contain FKs that reference a field within the same table  A self join uses this to join a table with itself Employee ID Employee Name Employee Manager ID 1Smith 2Jones 3Black1 4Gonzales1 5Kelley2 Project IDProjectName Parent ProjectID 1Hardware Support Intranet 2Exploration Database 3Hardware Support Database1 4Hardware Support Services1 5Hardware Support Interface1 6Exploration JSPs2

Join Queries  Self joins  Create a table alias so you can join the table to a copy of itself Employee ID Employee Name Employee Manager ID 1Smith 2Jones 3Black1 4Gonzales1 5Kelley2 Table “a” Employee ID Employee Name Employee Manager ID 1Smith 2Jones 3Black1 4Gonzales1 5Kelley2 Table “b”

Join Queries  Self joins  Example SELECT a.emp_name, b.emp_name AS MANAGER FROM employee a JOIN employee b ON a.emp_manager_id = b.emp_id

Overview  Queries using multiple tables  Join queries Inner Outer Self  Nested queries  Queries using set operations  Views

Nested Queries  Consist of a main query and a subquery  The subquery retrieves a search condition value for the main query  Find the purchase id, date, and pounds for all purchases of Celestial Cashew Crunch SELECT purch_id, purch_date, pounds FROM candy_purchase WHERE prod_id = (SELECT prod_id FROM candy_product WHERE prod_desc = 'Celestial Cashew Crunch')

Nested Queries SELECT purch_id, purch_date, pounds FROM candy_purchase WHERE prod_id = (SELECT prod_id FROM candy_product WHERE prod_desc = 'Celestial Cashew Crunch')

Nested Queries  The subquery returns exactly one value  The SearchColumn data type in the main query must match the SearchColumn in the subquery  The SearchColumn is usually a foreign key value SELECT purch_id, purch_date, pounds FROM candy_purchase WHERE prod_id = (SELECT prod_id FROM candy_product WHERE prod_desc = 'Celestial Cashew Crunch')

Nested Queries  Alternate format SELECT cust_id, purch_id, purch_date, pounds FROM candy_purchase WHERE cust_id IN (SELECT cust_id FROM candy_customer WHERE cust_type = 'P’)

Nested Queries  Alternate format  Nested queries were actually unnecessary in the previous examples

Nested Queries  Nested queries with multiple subqueries  A nested query’s main query can have multiple search conditions whose values are specified by subqueries SELECT purch_id, purch_date, pounds 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'); Can this query be written without any nested queries?

Nested Queries  Previous query without any nested queries SELECT purch_id, purch_date, pounds FROM candy_purchase pu JOIN candy_product pr ON pu.prod_id = pr.prod_id JOIN candy_customer c ON pu.cust_id = c.cust_id WHERE c.cust_type = 'P' AND pr.prod_desc = 'Celestial Cashew Crunch’;

Nested Queries  For many nested queries, you could achieve the same result with a join query  A nested query may seem more straight-forward than the equivalent join query  Restrictions Final results can only come from the main query table Execution time used to be slower but this is no longer the case with improved query optimizers  However, some nested query results can’t be retrieved through a join…

Nested Queries  Create a query that retrieves the IDs and names of all customers who have made an order in which the total pounds of the order is greater than the average pounds of all individual orders  Answer is developed in the next three slides

Sample Database (CANDY) CANDY_CUSTOMER CANDY_PURCHASE CANDY_CUST_TYPE CANDY_PRODUCT

Nested Queries  Strategy for creating and debugging nested queries  Create and debug the subquery(ies) first  Next work on the main query  Integrate the main and subquery(ies)  If the integration doesn’t run, debug it by hard-coding the search condition rather than using the subquery

Nested Queries  Strategy for creating and debugging nested queries  Create and debug the subquery(ies) first  Next work on the main query SELECT AVG(pounds) FROM candy_purchase; SELECT c.cust_id, cust_name, SUM(pounds) FROM candy_customer c INNER JOIN candy_purchase p ON c.cust_id = p.cust_id GROUP BY c.cust_id, cust_name ORDER BY c.cust_id; Find average pounds per candy purchase Find customer id, name, and total pounds per customer purchase

Nested Queries  Strategy for creating and debugging nested queries  Results of subquery and main query

Nested Queries  Strategy for creating and debugging nested queries  Integrate the main and subquery(ies)

Nested Queries  Order of fields in the GROUP BY clause  Changing the order of the fields can change the order the rows are returned in, but the number of rows and their content will be the same cust_name, c.cust_id is equivalent to c.cust_id, cust_name SELECT c.cust_id, cust_name, SUM(pounds) FROM candy_customer c INNER JOIN candy_purchase p ON c.cust_id = p.cust_id GROUP BY c.cust_id, cust_name

Order of Evaluation  SQL clauses are evaluated in the following order  FROM  WHERE  GROUP BY  HAVING  SELECT  ORDER BY

Overview  Queries using multiple tables  Join queries Inner Outer Self  Nested queries  Queries using set operations  Views

Queries Using Set Operations  Basic set operators  UNION  UNION ALL  INTERSECT  MINUS

Queries Using Set Operations  UNION and UNION ALL  Combines the retrieved recordsets of 2 independent queries  Example Retrieve a list of all dates on which there was any activity (purchase or delivery) SELECT purch_date FROM candy_purchase UNION SELECT delivery_date FROM candy_purchase;

Queries Using Set Operations  UNION and UNION ALL  UNION: suppresses duplicate values  UNION ALL: returns ALL values

Queries Using Set Operations  UNION and UNION ALL Restrictions  Each SELECT query must retrieve… Exactly the same number of fields Fields must have the same corresponding data types  What if you want to retrieve two different sets of data with different data types using a single query? Use placeholders in each unioned query Placeholders can be null, text, number, etc. But the data types must match in both queries Use null to match anything

Queries Using Set Operations  UNIONing two different datasets SELECT f_last, f_first, ' ' AS s_last, ' ' AS s_first FROM nw_faculty UNION ALL SELECT ' ', ' ', s_last, s_first FROM nw_student;

Queries Using Set Operations  INTERSECT and MINUS  INTERSECT: retrieves the matching records in two independent queries  MINUS: retrieves all of the records in the first query, “minus” the matching records in the second query  Not SQL-standard, and not available for many DBMSs Not available in MySQL

Queries Using Set Operations  INTERSECT example  Which candy customers have actually purchased anything? SELECT cust_id FROM candy_customer ORDER BY cust_id; SELECT cust_id FROM candy_customer INTERSECT SELECT cust_id FROM candy_purchase ORDER BY cust_id;

Queries Using Set Operations  INTERSECT alternatives  Use an INNER JOIN along with DISTINCT  Works with both Oracle and MySQL SELECT cust_id FROM candy_customer ORDER BY cust_id; SELECT DISTINCT c.cust_id FROM candy_customer c JOIN candy_purchase p ON c.cust_id = p.cust_id ORDER BY c.cust_id

Queries Using Set Operations  INTERSECT alternatives  Temporary views (works with both Oracle and MySQL) A view is treated identically to a table A “temporary view” is created in the FROM clause when it contains a SELECT clause SELECT DISTINCT q1.cust_id FROM ( SELECT * FROM candy_customer ) q1 INNER JOIN ( SELECT * FROM candy_purchase ) q2 ON q1.cust_id = q2.cust_id ORDER BY q1.cust_id Alias necessary for join condition

Queries Using Set Operations  INTERSECT alternatives  Example where an inner join without temporary views won’t work nicely List the customers who have purchased both “Celestial Cashew Crunch” and “Mystery Melange” Let’s write this using both an INTERSECT and temporary views…

Queries Using Set Operations SELECT cust_name FROM candy_customer c JOIN candy_purchase p ON c.cust_id = p.cust_id JOIN candy_product pr ON pr.prod_id = p.prod_id WHERE pr.prod_desc = 'Celestial Cashew Crunch’ INTERSECT SELECT cust_name FROM candy_customer c JOIN candy_purchase p ON c.cust_id = p.cust_id JOIN candy_product pr ON pr.prod_id = p.prod_id WHERE pr.prod_desc = 'Mystery Melange';  INTERSECT solution

Queries Using Set Operations  INTERSECT alternative using temporary views SELECT DISTINCT q1.cust_name FROM ( SELECT c.cust_id, cust_name FROM candy_customer c JOIN candy_purchase p ON c.cust_id = p.cust_id JOIN candy_product pr ON pr.prod_id = p.prod_id WHERE pr.prod_desc = 'Celestial Cashew Crunch' ) q1 INNER JOIN ( SELECT c.cust_id, cust_name FROM candy_customer c JOIN candy_purchase p ON c.cust_id = p.cust_id JOIN candy_product pr ON pr.prod_id = p.prod_id WHERE pr.prod_desc = 'Mystery Melange' ) q2 ON q1.cust_id = q2.cust_id;

Queries Using Set Operations  MINUS example  Which candy customers have never purchased anything? SELECT cust_id FROM candy_customer ORDER BY cust_id; SELECT cust_id FROM candy_customer MINUS SELECT cust_id FROM candy_purchase ORDER BY cust_id; SELECT DISTINCT cust_id FROM candy_purchase ORDER BY cust_id;

Queries Using Set Operations  MINUS alternative  Use a LEFT JOIN along with IS NULL  Works in both Oracle and MySQL SELECT c.cust_id, p.cust_id FROM candy_customer c LEFT JOIN candy_purchase p ON c.cust_id = p.cust_id ORDER BY c.cust_id; SELECT c.cust_id FROM candy_customer c LEFT JOIN candy_purchase p ON c.cust_id = p.cust_id WHERE p.cust_id IS NULL ORDER BY c.cust_id;

Overview  Queries using multiple tables  Join queries Inner Outer Self  Nested queries  Queries using set operations  Views

Views  A view is a virtual table based on a query  It can be used to restrict the fields that certain users can access  It can be used to simplify join queries You can think of it as a “stored query”

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 VIEW candy_customer_view AS SELECT cust_id, cust_name, cust_type, cust_addr,cust_zip FROM candy_customer; SELECT * FROM candy_customer_view;

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 These “action queries” only work on single tables CREATE VIEW purchase_product_view AS SELECT purch_date, delivery_date, prod_desc, pounds FROM candy_purchase pu INNER JOIN candy_product pr ON pu.prod_id = pr.prod_id;