David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design, and Implementation Chapter Two: Introduction to Structured Query Language.

Slides:



Advertisements
Similar presentations
Introduction to Structured Query Language (SQL)
Advertisements

Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-1 COS 346 Day 2.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 2-1 David M. Kroenke’s Chapter Two: Introduction to Structured Query.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-1 COS 346 Day 2.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 2-1 David M. Kroenke’s Chapter Two: Introduction to Structured Query.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 2-1 David M. Kroenke’s Chapter Two: Introduction to Structured Query.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
SQL Views Chapter 3A DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 5 th Edition.
Getting Started Chapter One DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 5 th Edition.
Getting Started with Microsoft SQL Server 2012 Express Edition Appendix A DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
David M. Kroenke and David J. Auer Database Processing—12 th Edition Fundamentals, Design, and Implementation Chapter One: Introduction KROENKE AND AUER.
Getting Started Chapter One DATABASE CONCEPTS, 7th Edition
David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design and Implementation Chapter Two: Introduction to Structured Query Language.
Structured Query Language
Getting Started with Oracle Database 11g Release 2 Express Edition Appendix B DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu
Getting Started Chapter One DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
Rationale Aspiring Database Developers should be able to efficiently query and maintain databases. This module will help students learn the Structured.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 5 th Edition.
Chapter 3 Single-Table Queries
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design, and Implementation Chapter Two: Introduction to Structured Query Language.
SQL Views Appendix E DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
SQL Views Chapter 3A DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
Business Intelligence Systems Appendix J DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Getting Started Chapter One DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
1 IT420: Database Management and Organization SQL part 3 7 February 2006 Adina Crăiniceanu
Structured Query Language
Database Queries aka SQL (pronounced “ sequel ” ).
David M. Kroenke and David J. Auer Database Processing Fundamentals, Design, and Implementation Chapter Four: Database Design Using Normalization 4-1 KROENKE.
DAVID M. KROENKE’S DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-1 David M. Kroenke’s Chapter Two: Introduction to Structured Query.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design, and Implementation Chapter One: Introduction.
David M. Kroenke and David J. Auer Database Processing Fundamentals, Design, and Implementation Chapter Twelve: Big Data, Data Warehouses, and Business.
David M. Kroenke and David J. Auer Database Processing Fundamentals, Design, and Implementation Appendix J: Business Intelligence Systems.
David M. Kroenke and David J. Auer Database Processing Fundamentals, Design, and Implementation Chapter Three: The Relational Model and Normalization.
David M. Kroenke and David J. Auer Database Processing Fundamentals, Design, and Implementation Chapter Four: Database Design Using Normalization.
David M. Kroenke and David J. Auer Database Processing Fundamentals, Design, and Implementation Chapter Seven: SQL for Database Construction and Application.
David M. Kroenke and David J. Auer Database Processing Fundamentals, Design, and Implementation Appendix A: Getting Started with Microsoft Access 2013.
David M. Kroenke and David J. Auer Database Processing: F undamentals, Design, and Implementation Chapter Three: The Relational Model and Normalization.
1 Structured Query Language (SQL) Pertemuan 09 Matakuliah: F0712 / Lab MS Access Tahun: 2007.
David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design, and Implementation Chapter Ten: Managing Databases with SQL Server 2012,
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
CSIS 115 Database Design and Applications for Business Dr. Meg Fryling “Dr. Meg” Fall #csis115 © 2012 Meg Fryling.
Chapter Seven: SQL for Database Construction and Application Processing.
COS 346 Day 3 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
SQL Query Getting to the data ……..
CSIS 115 Database Design and Applications for Business
CSIS 115 Database Design and Applications for Business
David M. Kroenke and David J
COS 346 Day 3 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
Using SQL to Prepare Data for Analysis
David M. Kroenke and David J
Database Principles Constructed by Hanh Pham based on slides from: “Database Processing, Fundamentals, Design, and Implementation”, D. Kroenke, D. Auer,
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Contents Preface I Introduction Lesson Objectives I-2
Getting Started Chapter One DATABASE CONCEPTS, 5th Edition
Getting Started Chapter One DATABASE CONCEPTS, 4th Edition
Presentation transcript:

David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design, and Implementation Chapter Two: Introduction to Structured Query Language

Chapter Objectives To understand the use of extracted data sets in business intelligence (BI) systems To understand the use of ad-hoc queries in business intelligence (BI) systems To understand the history and significance of Structured Query Language (SQL) To understand the SQL SELECT/FROM/WHERE framework as the basis for database queries To create SQL queries to retrieve data from a single table KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-2

Chapter Objectives To create SQL queries that use the SQL SELECT, FROM, WHERE, ORDER BY, GROUP BY, and HAVING clauses To create SQL queries that use the SQL DISTINCT, AND, OR, NOT, BETWEEN, LIKE, and IN keywords To create SQL queries that use the SQL built-in functions of SUM, COUNT, MIN, MAX, and AVG with and without the use of a GROUP BY clause KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-3

Chapter Objectives To create SQL queries that retrieve data from a single table but restrict the data based upon data in another table (subquery) To create SQL queries that retrieve data from multiple tables using an SQL join operation KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-4

Business Intelligence (BI) Systems Business intelligence (BI) systems are information systems that assist managers and other professionals: –Assessment –Analysis –Planning –Control KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-5

Ad-Hoc Queries Ad-hoc queries: –Questions that can be answered using database data –Example: “How many customers in Portland, Oregon, bought our green baseball cap?” –Created by the user as needed, instead of programmed into an application –Common in business KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-6

Components of a Data Warehouse KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-7

Structured Query Language Structured Query Language (SQL) was developed by the IBM Corporation in the late 1970’s. SQL was endorsed as a U.S. national standard by the American National Standards Institute (ANSI) in 1992 [SQL-92]. Newer versions exist, and they incorporate XML and some object-oriented concepts. KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-8

SQL As a Data Sublanguage SQL is not a full featured programming language. –C, C#, Java SQL is a data sublanguage for creating and processing database data and metadata. SQL is ubiquitous in enterprise-class DBMS products. SQL programming is a critical skill. KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-9

SQL DDL, DML, and SQL/PSM SQL statements can be divided into three categories: –Data definition language (DDL) statements Used for creating tables, relationships, and other structures Covered in Chapter 7 –Data manipulation language (DML) statements Used for queries and data modification Covered in this chapter (Chapter 2) KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-10

SQL DDL, DML, and SQL/PSM –SQL/Persistent Stored Modules (SQL/PSM) statements Add procedural programming capabilities –Variables –Control-of-flow statements Covered in Chapters: –7 (general introduction) –10A (SQL Server 2012) –10B (Oracle Database 11g Release 2) –10C (MySQL 5.6) KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-11

SQL TCL –Transaction control language (TCL) statements Used to mark transaction boundaries and control transaction behavior Covered in Chapters: –9 (general introduction) –10A (SQL Server 2012) –10B (Oracle Database 11g Release 2) –10C (MySQL 5.6) KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-12

SQL DCL –Data control language (DCL) statements Used to grant (or revoke) database permissions to (from) users and groups Covered in Chapters: –9 (general introduction) –10A (SQL Server 2012) –10B (Oracle Database 11g Release 2) –10C (MySQL 5.6) KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-13

Cape Codd Outdoor Sports Cape Codd Outdoor Sports is a fictitious company based on an actual outdoor retail equipment vendor. Cape Codd Outdoor Sports: –Has 15 retail stores in the United States and Canada. –Has an online Internet store. –Has a (postal) mail order department. All retail sales are recorded in an Oracle Database 11g database. KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-14

Cape Codd Retail Sales Structure KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-15

Cape Codd Retail Sales Data Extraction The Cape Codd marketing department needs an analysis of in-store sales. The entire database is not needed for this, only an extraction of retail sales data. The data is extracted by the IS department from the operational database into a separate, off-line database for use by the marketing department. Three tables are used: RETAIL_ORDER, ORDER_ITEM, and SKU_DATA (SKU = Stock Keeping Unit). The extracted data is converted as necessary: –Into a different DBMS—Microsoft SQL Server –Into different columns—OrderDate becomes OrderMonth and OrderYear. KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-16

Extracted Retail Sales Data Format KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-17

Retail Sales Extract Tables [in Microsoft Access 2010] KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-18

The SQL SELECT Statement The fundamental framework for an SQL query is the SQL SELECT statement. –SELECT{ColumnName(s)} –FROM{TableName(s)} –WHERE{Condition(s)} All SQL statements end with a semi-colon (;). KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-19

Specific Columns on One Table SELECTDepartment, Buyer FROMSKU_DATA; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-20

Specifying Column Order SELECTBuyer, Department FROMSKU_DATA; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-21

The DISTINCT Keyword SELECTDISTINCT Buyer, Department FROMSKU_DATA; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-22

Selecting All Columns: The Asterisk (*) Wildcard Character SELECT* FROMSKU_DATA; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-23

Specific Rows from One Table SELECT* FROMSKU_DATA WHEREDepartment = 'Water Sports'; NOTE:SQL wants a plain ASCII single quote: ' NOT ‘ ! KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-24

Specific Columns and Rows from One Table SELECTSKU_Description, Buyer FROMSKU_DATA WHEREDepartment = 'Climbing'; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-25

Using Microsoft Access I KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-26

Using Microsoft Access II KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-27

Using Microsoft Access III KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-28

Using Microsoft Access IV KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-29

Using Microsoft Access V KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-30

Using Microsoft Access—Results KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-31

Using Microsoft Access Saving the Query KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-32

Using Microsoft Access The Named and Saved Query KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-33

Using Microsoft SQL Server 2012 The Microsoft SQL Server Management Studio I KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-34

Using Microsoft SQL Server 2012 The Microsoft SQL Server Management Studio II KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-35

Using Oracle Database 11g Release 2 SQL Developer I KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-36

Using Oracle Database 11g Release 2 SQL Developer II KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-37

Using MySQL 5.6 MySQL Workbench I KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-38

Using MySQL 5.6 MySQL Workbench II KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-39

Sorting the Results—ORDER BY SELECT* FROMORDER_ITEM ORDER BYOrderNumber, Price; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-40

Sort Order: Ascending and Descending SELECT* FROMORDER_ITEM ORDER BYPrice DESC, OrderNumber ASC; NOTE: The default sort order is ASC—does not have to be specified. KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-41

WHERE Clause Options—AND SELECT* FROMSKU_DATA WHEREDepartment = 'Water Sports' ANDBuyer = 'Nancy Meyers'; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-42

WHERE Clause Options—OR SELECT* FROMSKU_DATA WHEREDepartment = 'Camping' ORDepartment = 'Climbing'; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-43

WHERE Clause Options—IN SELECT* FROMSKU_DATA WHEREBuyer IN ('Nancy Meyers', 'Cindy Lo', 'Jerry Martin'); KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-44

WHERE Clause Options—NOT IN SELECT* FROMSKU_DATA WHEREBuyer NOT IN ('Nancy Meyers', 'Cindy Lo', 'Jerry Martin'); KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-45

WHERE Clause Options— Ranges with BETWEEN SELECT* FROMORDER_ITEM WHEREExtendedPrice BETWEEN 100 AND 200; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-46

WHERE Clause Options— Ranges with Math Symbols SELECT* FROMORDER_ITEM WHEREExtendedPrice >= 100 AND ExtendedPrice <= 200; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-47

WHERE Clause Options— LIKE and Wildcards I The SQL keyword LIKE can be combined with wildcard symbols: –SQL 92 Standard (SQL Server, MySQL, etc.): _ = exactly one character % = any set of one or more characters –Microsoft Access (based on MS DOS) ? = exactly one character * = any set of one or more characters KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-48

WHERE Clause Options— LIKE and Wildcards II SELECT* FROMSKU_DATA WHEREBuyer LIKE 'Pete%'; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-49

WHERE Clause Options— LIKE and Wildcards III SELECT* FROMSKU_DATA WHEREBuyer LIKE '%Tent%'; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-50

WHERE Clause Options— LIKE and Wildcards IV SELECT* FROMSKU_DATA WHERESKU LIKE '%2__'; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-51

SQL Built-In Functions I There are five SQL built-in functions: –COUNT –SUM –AVG –MIN –MAX KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-52

SQL Built-In Functions II SELECTSUM(ExtendedPrice) ASOrder3000Sum FROMORDER_ITEM WHEREOrderNumber = 3000; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-53

SQL Built-In Functions III SELECTSUM(ExtendedPrice) AS OrderItemSum, AVG(ExtendedPrice) AS OrderItemAvg, MIN(ExtendedPrice) AS OrderItemMin, MAX(ExtendedPrice) AS OrderItemMax FROMORDER_ITEM; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-54

SQL Built-In Functions IV SELECTCOUNT(*) AS NumberOfRows FROMORDER_ITEM; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-55

SQL Built-In Functions V SELECTCOUNT (DISTINCT Department) AS DeptCount FROMSKU_DATA; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-56

Arithmetic in SELECT Statements SELECTQuantity * Price AS EP, ExtendedPrice FROMORDER_ITEM; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-57

String Functions in SELECT Statements SELECTDISTINCT RTRIM (Buyer) + ' in ' + RTRIM (Department) AS Sponsor FROMSKU_DATA; NOTE: This SQL statement uses SQL Server 2012 syntax—other DBMS products use different concatenation and character string operators. KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-58

The SQL Keyword GROUP BY I SELECTDepartment, Buyer, COUNT(*) AS Dept_Buyer_SKU_Count FROMSKU_DATA GROUP BY Department, Buyer; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-59

The SQL Keyword GROUP BY II In general, place WHERE before GROUP BY. Some DBMS products do not require that placement; but to be safe, always put WHERE before GROUP BY. The HAVING operator restricts the groups that are presented in the result. There is an ambiguity in statements that include both WHERE and HAVING clauses. The results can vary, so to eliminate this ambiguity SQL always applies WHERE before HAVING. KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-60

The SQL Keyword GROUP BY III SELECTDepartment, COUNT(*) AS Dept_SKU_Count FROMSKU_DATA WHERESKU <> GROUP BY Department ORDER BY Dept_SKU_Count; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-61

The SQL Keyword GROUP BY IV SELECTDepartment, COUNT(*) AS Dept_SKU_Count FROMSKU_DATA WHERESKU <> GROUP BY Department HAVING COUNT (*) > 1 ORDER BYDept_SKU_Count; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-62

Querying Multiple Tables: Subqueries I SELECTSUM (ExtendedPrice) AS Revenue FROMORDER_ITEM WHERESKU IN (SELECT SKU FROM SKU_DATA WHERE Department = 'Water Sports'); Note: The second SELECT statement is a subquery. KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-63

Querying Multiple Tables: Subqueries II SELECT Buyer FROM SKU_DATA WHERE SKU IN (SELECTSKU FROMORDER_ITEM WHEREOrderNumber IN (SELECTOrderNumber FROMRETAIL_ORDER WHEREOrderMonth = 'January' ANDOrderYear = 2013)); KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-64

Querying Multiple Tables: Joins I SELECTBuyer, ExtendedPrice FROMSKU_DATA, ORDER_ITEM WHERESKU_DATA.SKU = ORDER_ITEM.SKU; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-65

Querying Multiple Tables: Joins II SELECTBuyer, SUM(ExtendedPrice) AS BuyerRevenue FROMSKU_DATA, ORDER_ITEM WHERESKU_DATA.SKU = ORDER_ITEM.SKU GROUP BYBuyer ORDER BYBuyerRevenue DESC; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-66

Querying Multiple Tables: Joins III SELECTBuyer, ExtendedPrice, OrderMonth FROMSKU_DATA, ORDER_ITEM, RETAIL_ORDER WHERESKU_DATA.SKU = ORDER_ITEM.SKU ANDORDER_ITEM.OrderNumber = RETAIL_ORDER.OrderNumber; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-67

JOIN ON Syntax SELECT RETAIL_ORDER.OrderNumber, StoreNumber, OrderYear, ORDER_ITEM.SKU, SKU_Description, Department FROM RETAIL_ORDER JOIN ORDER_ITEM ON RETAIL_ORDER.OrderNumber=ORDER_ITEM.OrderNumber JOIN SKU_DATA ON ORDER_ITEM.SKU=SKU_DATA.SKU WHERE OrderYear = '2012' ORDER BY RETAIL_ORDER.OrderNumber, ORDER_ITEM.SKU; KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-68

JOIN ON Syntax Results KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-69

OUTER JOINS I KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-70

OUTER JOINS II KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-71

OUTER JOINS III KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-72

OUTER JOINS IV KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-73

Subqueries versus Joins Subqueries and joins both process multiple tables. A subquery can only be used to retrieve data from the top table. A join can be used to obtain data from any number of tables, including the “top table” of the subquery. In Chapter 7, we will study the correlated subquery. That kind of subquery can do work that is not possible with joins. KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-74

David Kroenke and David Auer Database Processing Fundamentals, Design, and Implementation (13 th Edition) End of Presentation: Chapter Two KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-75

All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. Printed in the United States of America. KROENKE AND AUER - DATABASE PROCESSING, 13th Edition © 2014 Pearson Education, Inc. 2-76