DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-1 COS 346 Day 2.

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 3-1 COS 346 Day5.
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.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 4-1 COS 346 Day6.
Introduction to Database Processing
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-1 David M. Kroenke’s Chapter One: Introduction Part One Database Processing:
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.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-1 David M. Kroenke’s Database Processing: Fundamentals, Design, and.
David M. Kroenke and David J. Auer Database Processing—12 th Edition Fundamentals, Design, and Implementation Chapter One: Introduction KROENKE AND AUER.
David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design and Implementation Chapter Two: Introduction to Structured Query Language.
Concepts of Database Management Sixth Edition
Structured Query Language
1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
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’s Chapter One: Introduction Part Two Database Processing: Fundamentals, Design, and Implementation.
INTRODUCTION TO DATABASES Chapter 1. What is a Database?  Forget the glossary! (see pages 11-12)  The purpose of a database is to help people track.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-1 Microsoft Access Microsoft Access is a low-end product intended for.
Introduction to databases and SQL. What is a database?  A database is an organized way of holding together pieces of information  A database refers.
SQL – Structured Query Language CIS 324 – Chapter 5.
David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design, and Implementation Chapter Two: Introduction to Structured Query Language.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 11-1 David M. Kroenke’s Chapter Eleven: Managing Databases with SQL Server.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh 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.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
STRUCTURED QUERY LANGUAGE SQL-II IST 210 Organization of Data IST210 1.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
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
Database Queries aka SQL (pronounced “ sequel ” ).
DAVID M. KROENKE’S DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-1 David M. Kroenke’s Chapter Two: Introduction to Structured Query.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
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 Two: Introduction to Structured Query Language.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
Structured Query Language SQL-II IST 210 Organization of Data IST2101.
1 Structured Query Language (SQL) Pertemuan 09 Matakuliah: F0712 / Lab MS Access Tahun: 2007.
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.
COS 346 Day 3 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
CHAPTER 7 DATABASE ACCESS THROUGH WEB
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
Teknologji Informative
Using SQL to Prepare Data for Analysis
Database Processing: David M. Kroenke’s Chapter One: Introduction
David M. Kroenke and David J
Database Processing: David M. Kroenke’s Chapter One: Introduction
Database Processing: David M. Kroenke’s Chapter One: Introduction
Database Principles Constructed by Hanh Pham based on slides from: “Database Processing, Fundamentals, Design, and Implementation”, D. Kroenke, D. Auer,
Presentation transcript:

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 1-2 Agenda Questions? Assignment 1 is posted in BlackBoard –Due Jan 29, 2009 at beginning of Class Finish Introduction To Database Processing Begin Intro to SQL

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-3 David M. Kroenke’s Chapter One: Introduction Part Two Database Processing: Fundamentals, Design, and Implementation

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-4 Three Types of Database Design

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-5 Database Design from Existing Data

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-6 Data Import: One or Two Tables? This is an important decision, and based on a set of rules known as normalization (which is covered in Chapter Three)

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-7 Database Design from New Systems Development Entity-Relationship data modeling is covered in Chapter Five, and data model transformations to database designs are covered in Chapter Six

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-8 Database Design from Database Redesign Database redesign is covered in Chapter Eight, after coverage of DDL SQL in Chapter Seven

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-9 What You Need to Learn

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-10 Knowledge Priorities

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-11 A Brief History of Database Processing

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-12 The Relational Database Model The dominant database model is the relational database model – all current major DBMS products are based on it Created by IBM engineer E. F. Codd in 1970 It was based on mathematics called relational algebra This text examines and explains the relational database model

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-13 David M. Kroenke’s Database Processing Fundamentals, Design, and Implementation (10 th Edition) End of Presentation: Chapter One – Part Two

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-14 David M. Kroenke’s Chapter Two: Introduction to Structured Query Language Part One Database Processing: Fundamentals, Design, and Implementation

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-15 Structured Query Language Structured Query Language (SQL) was developed by the IBM Corporation in the late 1970s. SQL was endorsed as a United States national standard by the American National Standards Institute (ANSI) in 1992 [SQL-92]. A newer version [SQL3] exists and incorporates some object-oriented concepts, but is not widely used in commercial DBMS products.

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-16 SQL as a Data Sublanguage SQL is not a full featured programming language as are C, C#, and 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.

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-17 SQL DDL and DML SQL statements can be divided into two categories: –Data definition language (DDL) statements Used for creating tables, relationships, and other structures. Covered in Chapter Seven. –Data manipulation language (DML) statements. Used for queries and data modification Covered in this chapter (Chapter Two)

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-18 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 on-line Internet store. –Has a (postal) mail order department. All retail sales recorded in an Oracle database.

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-19 Cape Codd Retail Sales Structure

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-20 Accessing the Cape Codd Database Download an MS access version from WebCT under Database section (chapter_2.mdb) –Use with Access 2003 or 2007

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-21 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  MS SQL Server -> Oracle 10G –Into different columns  OrderDate becomes OrderMonth and OrderYear

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-22 Extracted Retail Sales Data Format

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-23 Retail Sales Extract Tables [in MS SQL Server]

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-24 Running SQL statements For MS access

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-25 Running SQL statements For MS access

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-26 Running SQL statements For MS access

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-27 Running SQL statements For MS access

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-28 Running SQL statements For Oracle Capcodd_littleblack.advtech.local

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-29 Running SQL statements For Oracle __________

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-30 The SQL SELECT Statement The fundamental framework for SQL query states is the SQL SELECT statement: –SELECT{ColumnName(s)} –FROM{“Tablespace.”TableName(s)} –WHERE{Conditions} All SQL statements end with a semi-colon (;). Select statements return a temporary database table

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-31 Specific Columns on One Table SELECTDepartment, Buyer FROMChapter_2.SKU_DATA;

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-32 Specifying Column Order SELECTBuyer, Department FROMChapter_2.SKU_DATA;

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-33 The DISTINCT Keyword SELECTDISTINCT Buyer, Department FROMChapter_2.SKU_DATA;

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-34 Selecting All Columns: The Asterisk (*) Keyword SELECT* FROMChapter_2.SKU_DATA;

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-35 Specific Rows from One Table SELECT* FROM Chapter_2. SKU_DATA WHEREDepartment = 'Water Sports'; NOTE:SQL wants a plain ASCII single quote: ' NOT “ !

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-36 Specific Columns and Rows from One Table SELECTSKU_Description, Buyer FROMChapter_2.SKU_DATA WHEREDepartment = 'Climbing';

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-37 David M. Kroenke’s Database Processing Fundamentals, Design, and Implementation (10 th Edition) End of Presentation: Chapter Two Part One

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-38 David M. Kroenke’s Chapter Two: Introduction to Structured Query Language Part Two Database Processing: Fundamentals, Design, and Implementation

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-39 Using MS Access - Results

Using Access 2007 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-40

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-41 Using MS SQL Server [SQL Query Analyzer]

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-42 Using Oracle [SQL*Plus]

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-43 Using Oracle [Quest Software’s TOAD]Quest Software’s TOAD

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-44 Using MySQL [MySQL Command Line Client]

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-45 Using MySQL [MySQL Query Browser]MySQL Query Browser

Connecting SQL server 2005 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-46 Start Microsoft SQL server management Studio Select Littleblack\”lastname” If you are connecting from outside the UMFK network use littleblack.umfk.maine.edu\”lastname”

SQL 2005 DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-47

Executing SQL DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-48

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-49 Sorting the Results: ORDER BY SELECT* FROMChapter_2.ORDER_ITEM ORDER BYOrderNumber, Price;

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-50 Sort Order: Ascending and Descending SELECT* FROM Chapter_2. ORDER_ITEM ORDER BYPrice DESC, OrderNumber ASC; NOTE: The default sort order is ASC – does not have to be specified.

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-51 WHERE Clause Options: AND SELECT* FROMChapter_2.SKU_DATA WHEREDepartment = 'Water Sports' ANDBuyer = 'Nancy Meyers';

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-52 WHERE Clause Options: OR SELECT* FROMChapter_2.SKU_DATA WHEREDepartment = 'Camping' ORDepartment = 'Climbing';

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-53 WHERE Clause Options:- IN SELECT* FROMChapter_2.SKU_DATA WHEREBuyer IN ('Nancy Meyers', 'Cindy Lo', 'Jerry Martin');

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-54 WHERE Clause Options: NOT IN SELECT* FROMChapter_2.SKU_DATA WHEREBuyer NOT IN ('Nancy Meyers', 'Cindy Lo', 'Jerry Martin');

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-55 WHERE Clause Options: Ranges with BETWEEN SELECT* FROMChapter_2.ORDER_ITEM WHEREExtendedPrice BETWEEN 100 AND 200;

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-56 WHERE Clause Options: Ranges with Math Symbols SELECT* FROMChapter_2.ORDER_ITEM WHEREExtendedPrice >= 100 AND ExtendedPrice <= 200;

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-57 WHERE Clause Options: LIKE and Wildcards The SQL keyword LIKE can be combined with wildcard symbols: –SQL 92 Standard (SQL Server, Oracle, etc.): _ = Exactly one character % = Any set of one or more characters –MS Access (based on MS DOS) ? = Exactly one character * = Any set of one or more characters

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-58 WHERE Clause Options: LIKE and Wildcards (Continued) SELECT* FROM Chapter_2.SKU_DATA WHEREBuyer LIKE 'Pete%'; MS ACCESS Buyer LIKE ‘Pete*’;

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-59 WHERE Clause Options: LIKE and Wildcards (Continued) SELECT* FROMChapter_2.SKU_DATA WHERESKU_Description LIKE '%Tent%';

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-60 WHERE Clause Options: LIKE and Wildcards SELECT* FROM Chapter_2.SKU_DATA WHERESKU LIKE '%2_ _'; MS ACCESS SKU LIKE ‘*2??’;

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-61 SQL Built-in Functions There are five SQL Built-in Functions: –COUNT –SUM –AVG –MIN –MAX

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-62 SQL Built-in Functions (Continued) SELECTSUM (ExtendedPrice) ASOrder3000Sum FROMORDER_ITEM WHEREOrderNumber = 3000;

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-63 SQL Built-in Functions (Continued) SELECTSUM (ExtendedPrice) AS OrderItemSum, AVG (ExtendedPrice) AS OrderItemAvg, MIN (ExtendedPrice) AS OrderItemMin, MAX (ExtendedPrice) AS OrderItemMax FROMChapter_2.ORDER_ITEM;

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-64 SQL Built-in Functions (Continued) SELECTCOUNT(*) AS NumRows FROMORDER_ITEM;

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-65 SQL Built-in Functions (Continued) SELECTCOUNT (DISTINCT Department) AS DeptCount FROMChapter_2.SKU_DATA;

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-66 Arithmetic in SELECT Statements SELECTQuantity * Price AS EP, ExtendedPrice FROMChapter_2.ORDER_ITEM;

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-67 String Functions in SELECT Statements SELECTDISTINCT RTRIM (Buyer) + ' in ' + RTRIM (Department) AS Sponsor FROMChapter_2.SKU_DATA;

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-68 The SQL keyword GROUP BY SELECTDepartment, Buyer, COUNT(*) AS Dept_Buyer_SKU_Count FROMChapter_2.SKU_DATA GROUP BY Department, Buyer;

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-69 The SQL keyword GROUP BY (Continued) 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.

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-70 The SQL keyword GROUP BY (Continued) SELECTDepartment, COUNT(*) AS Dept_SKU_Count FROMSKU_DATA WHERESKU <> GROUP BY Department ORDER BY Dept_SKU_Count;

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-71 The SQL keyword GROUP BY (Continued) SELECTDepartment, COUNT(*) AS Dept_SKU_Count FROMChapter_2.SKU_DATA WHERESKU <> GROUP BY Department HAVING COUNT (*) > 1 ORDER BYDept_SKU_Count;

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-72 Querying Multiple Tables: Subqueries SELECTSUM (ExtendedPrice) AS Revenue FROMORDER_ITEM WHERESKU IN (SELECTSKU FROMSKU_DATA WHERE Department = 'Water Sports'); Note: The second SELECT statement is a subquery.

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-73 Querying Multiple Tables: Subqueries (Continued) SELECTBuyer FROMSKU_DATA WHERESKU IN (SELECTSKU FROMORDER_ITEM WHEREOrderNumber IN (SELECTOrderNumber FROMRETAIL_ORDER WHEREOrderMonth = 'January' ANDOrderYear = 2004));

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-74 Querying Multiple Tables: Joins SELECTBuyer, ExtendedPrice FROMSKU_DATA, ORDER_ITEM WHERESKU_DATA.SKU = ORDER_ITEM.SKU;

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-75 Querying Multiple Tables: Joins (Continued) SELECTBuyer, SUM(ExtendedPrice) AS BuyerRevenue FROMSKU_DATA, ORDER_ITEM WHERESKU_DATA.SKU = ORDER_ITEM.SKU GROUP BYBuyer ORDER BYBuyerRevenue DESC;

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-76 Querying Multiple Tables: Joins (Continued) SELECTBuyer, ExtendedPrice, OrderMonth FROMSKU_DATA, ORDER_ITEM, RETAIL_ORDER WHERESKU_DATA.SKU = ORDER_ITEM.SKU ANDORDER_ITEM.OrderNumber = RETAIL_ORDER.OrderNumber;

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-77 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.

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 1-78 David M. Kroenke’s Database Processing Fundamentals, Design, and Implementation (10 th Edition) End of Presentation: Chapter Two Part Two