Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design and Implementation Chapter Two: Introduction to Structured Query Language."— Presentation transcript:

1 David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design and Implementation Chapter Two: Introduction to Structured Query Language KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-1

2 Chapter Objectives To understand the use of extracted data sets. To understand the use of ad-hoc queries. 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 be able to write queries in SQL to retrieve data from a single table. To be able to write queries in SQL to use the SQL SELECT, FROM, WHERE, ORDER BY, GROUP BY, and HAVING clauses. KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-2

3 Chapter Objectives To be able to write queries in SQL to use SQL DISTINCT, AND, OR, NOT, BETWEEN, LIKE, and IN keywords. To be able to use the SQL built-in functions of SUM, COUNT, MIN, MAX, and AVG with and without the use of a GROUP BY clause. To be able to write queries in SQL to retrieve data from a single table but restricting the data based upon data in another table (subquery). To be able to write queries in SQL to retrieve data from multiple tables using an SQL join. KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-3

4 Structured Query Language Structured Query Language (SQL) was developed by the IBM Corporation in the late 1970’s. SQL was endorsed as a United States national standard by the American National Standards Institute (ANSI) in 1992 [SQL-92]. Newer versions exist, and incorporate XML and some object-oriented concepts. KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-4

5 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, 11th Edition © 2010 Pearson Prentice Hall 2-5

6 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). KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-6

7 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 US and Canada. –Has a on-line Internet store. –Has a (postal) mail order department. All retail sales recorded in an Oracle database. KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-7

8 Cape Codd Retail Sales Structure KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-8

9 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 –Into different columns – OrderDate becomes OrderMonth and OrderYear. KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-9

10 Extracted Retail Sales Data Format KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-10

11 Retail Sales Extract Tables [in MS SQL Server] KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-11

12 The SQL SELECT Statement The fundamental framework for SQL query states is the SQL SELECT statement. –SELECT{ColumnName(s)} –FROM{TableName(s)} –WHERE{Conditions} All SQL statements end with a semi-colon (;). KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-12

13 Specific Columns on One Table SELECTDepartment, Buyer FROMSKU_DATA; KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-13

14 Specifying Column Order SELECTBuyer, Department FROMSKU_DATA; KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-14

15 The DISTINCT Keyword SELECTDISTINCT Buyer, Department FROMSKU_DATA; KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-15

16 Selecting All Columns: The Asterisk (*) Keyword SELECT* FROMSKU_DATA; KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-16

17 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, 11th Edition © 2010 Pearson Prentice Hall 2-17

18 Specific Columns and Rows from One Table SELECTSKU_Description, Buyer FROMSKU_DATA WHEREDepartment = 'Climbing'; KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-18

19 Using MS Access KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-19

20 Using MS Access (Continued) KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-20

21 Using MS Access (Continued) KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-21

22 Using MS Access (Continued) KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-22

23 Using MS Access (Continued) KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-23

24 Using MS Access - Results KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-24

25 Using MS Access Saving the Query KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-25

26 Using MS SQL Server 2008 The Microsoft SQL Server Management Studio I KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-26

27 Using MS SQL Server 2008 The Microsoft SQL Server Management Studio II KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-27

28 Using Oracle Database 11g SQL Developer I KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-28

29 Using Oracle Database 11g SQL Developer II KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-29

30 Using MySQL 5.1 MySQL Query Browser I KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-30

31 Using MySQL 5.1 MySQL Query Browser II KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-31

32 Sorting the Results – ORDER BY SELECT* FROMORDER_ITEM ORDER BYOrderNumber, Price; KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-32

33 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, 11th Edition © 2010 Pearson Prentice Hall 2-33

34 WHERE Clause Options - AND SELECT* FROMSKU_DATA WHEREDepartment = 'Water Sports' ANDBuyer = 'Nancy Meyers'; KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-34

35 WHERE Clause Options - OR SELECT* FROMSKU_DATA WHEREDepartment = 'Camping' ORDepartment = 'Climbing'; KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-35

36 WHERE Clause Options - IN SELECT* FROMSKU_DATA WHEREBuyer IN ('Nancy Meyers', 'Cindy Lo', 'Jerry Martin'); KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-36

37 WHERE Clause Options – NOT IN SELECT* FROMSKU_DATA WHEREBuyer NOT IN ('Nancy Meyers', 'Cindy Lo', 'Jerry Martin'); KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-37

38 WHERE Clause Options – Ranges with BETWEEN SELECT* FROMORDER_ITEM WHEREExtendedPrice BETWEEN 100 AND 200; KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-38

39 WHERE Clause Options – Ranges with Math Symbols SELECT* FROMORDER_ITEM WHEREExtendedPrice >= 100 AND ExtendedPrice <= 200; KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-39

40 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 zero or more characters –MS Access (based on MS DOS) ? = Exactly one character * = Any set of zero or more characters KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-40

41 WHERE Clause Options – LIKE and Wildcards SELECT* FROMSKU_DATA WHEREBuyer LIKE 'Pete%'; KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-41

42 WHERE Clause Options – LIKE and Wildcards SELECT* FROMSKU_DATA WHERESKU_Descripton LIKE '%Tent%'; KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-42

43 WHERE Clause Options – LIKE and Wildcards SELECT* FROMSKU_DATA WHERESKU LIKE '%2__'; KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-43

44 SQL Built-in Functions There are five SQL Built-in Functions: –COUNT –SUM –AVG –MIN –MAX KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-44

45 SQL Built-in Functions SELECTSUM (ExtendedPrice) ASOrder3000Sum FROMORDER_ITEM WHEREOrderNumber = 3000; KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-45

46 SQL Built-in Functions SELECTSUM (ExtendedPrice) AS OrderItemSum, AVG (ExtendedPrice) AS OrderItemAvg, MIN (ExtendedPrice) AS OrderItemMin, MAX (ExtendedPrice) AS OrderItemMax FROMORDER_ITEM; KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-46

47 SQL Built-in Functions SELECTCOUNT(*) AS NumberOfRows FROMORDER_ITEM; KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-47

48 SQL Built-in Functions SELECTCOUNT (DISTINCT Department) AS DeptCount FROMSKU_DATA; KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-48

49 Arithmetic in SELECT Statements SELECTQuantity * Price AS EP, ExtendedPrice FROMORDER_ITEM; KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-49

50 String Functions in SELECT Statements SELECTDISTINCT RTRIM (Buyer) + ' in ' + RTRIM (Department) AS Sponsor FROMSKU_DATA; KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-50

51 The SQL keyword GROUP BY SELECTDepartment, Buyer, COUNT(*) AS Dept_Buyer_SKU_Count FROMSKU_DATA GROUP BY Department, Buyer; KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-51

52 The SQL keyword GROUP BY 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, 11th Edition © 2010 Pearson Prentice Hall 2-52

53 The SQL keyword GROUP BY SELECTDepartment, COUNT(*) AS Dept_SKU_Count FROMSKU_DATA WHERESKU <> 302000 GROUP BY Department ORDER BY Dept_SKU_Count; KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-53

54 The SQL keyword GROUP BY SELECTDepartment, COUNT(*) AS Dept_SKU_Count FROMSKU_DATA WHERESKU <> 302000 GROUP BY Department HAVING COUNT (*) > 1 ORDER BYDept_SKU_Count; KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-54

55 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. KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-55

56 Querying Multiple Tables: Subqueries SELECTBuyer FROMSKU_DATA WHERESKU IN (SELECTSKU FROMORDER_ITEM WHEREOrderNumber IN (SELECTOrderNumber FROMRETAIL_ORDER WHEREOrderMonth = 'January' ANDOrderYear = 2004)); KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-56

57 Querying Multiple Tables: Joins SELECTBuyer, ExtendedPrice FROMSKU_DATA, ORDER_ITEM WHERESKU_DATA.SKU = ORDER_ITEM.SKU; KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-57

58 Querying Multiple Tables: Joins 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, 11th Edition © 2010 Pearson Prentice Hall 2-58

59 Querying Multiple Tables: Joins 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, 11th Edition © 2010 Pearson Prentice Hall 2-59

60 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, 11th Edition © 2010 Pearson Prentice Hall 2-60

61 David Kroenke and David Auer Database Processing Fundamentals, Design, and Implementation (11 th Edition) End of Presentation: Chapter Two KROENKE AND AUER - DATABASE PROCESSING, 11th Edition © 2010 Pearson Prentice Hall 2-61


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

Similar presentations


Ads by Google