David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design and Implementation



Similar documents
SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Database Query 1: SQL Basics

Oracle SQL. Course Summary. Duration. Objectives

Introduction to Microsoft Jet SQL

Financial Data Access with SQL, Excel & VBA

P_Id LastName FirstName Address City 1 Kumari Mounitha VPura Bangalore 2 Kumar Pranav Yelhanka Bangalore 3 Gubbi Sharan Hebbal Tumkur

SQL Basics. Introduction to Standard Query Language

Oracle Database: SQL and PL/SQL Fundamentals

SQL. Short introduction

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

David M. Kroenke and David J. Auer Database Processing 11 th Edition Fundamentals, Design, and Implementation. Chapter Objectives

Oracle Database: SQL and PL/SQL Fundamentals NEW

T-SQL STANDARD ELEMENTS

3.GETTING STARTED WITH ORACLE8i

Oracle Database: SQL and PL/SQL Fundamentals

Oracle Database 11g SQL

David M. Kroenke and David J. Auer Database Processing 12 th Edition

VBA and Databases (see Chapter 14 )

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Oracle Database: Introduction to SQL

Instant SQL Programming

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX

Database Administration with MySQL

Structured Query Language. Telemark University College Department of Electrical Engineering, Information Technology and Cybernetics

Database: SQL, MySQL

Oracle Database: Introduction to SQL

SQL SELECT Query: Intermediate

DBMS / Business Intelligence, SQL Server

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Oracle Database 12c: Introduction to SQL Ed 1.1

Information Systems SQL. Nikolaj Popov

History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)

Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

SQL Programming. Student Workbook

Oracle Database: Introduction to SQL

6 CHAPTER. Relational Database Management Systems and SQL Chapter Objectives In this chapter you will learn the following:

Creating QBE Queries in Microsoft SQL Server

How To Create A Table In Sql (Ahem)

MySQL for Beginners Ed 3

Chapter 5. SQL: Queries, Constraints, Triggers

QlikView 11.2 SR5 DIRECT DISCOVERY

Introduction to SQL: Data Retrieving

Exploring Microsoft Office Access Chapter 2: Relational Databases and Multi-Table Queries

Information and Computer Science Department ICS 324 Database Systems Lab#11 SQL-Basic Query

Inquiry Formulas. student guide

SQL Basics for RPG Developers

ICAB4136B Use structured query language to create database structures and manipulate data

SQL Server 2008 Core Skills. Gary Young 2011

Oracle Database 10g: Introduction to SQL

Introduction to SQL and SQL in R. LISA Short Courses Xinran Hu

Relational Database: Additional Operations on Relations; SQL

Programming with SQL

Structured Query Language (SQL)

Learning MySQL! Angola Africa SELECT name, gdp/population FROM world WHERE area > !

Access Queries (Office 2003)

A Brief Introduction to MySQL

Microsoft Access Lesson 5: Structured Query Language (SQL)

Retrieving Data Using the SQL SELECT Statement. Copyright 2006, Oracle. All rights reserved.

Database Applications Microsoft Access

Unit 10: Microsoft Access Queries

Netezza SQL Class Outline

1.204 Lecture 3. SQL: Basics, Joins SQL

Chapter 22 Database: SQL, MySQL,

Oracle Database: SQL and PL/SQL Fundamentals NEW

What is a database? COSC 304 Introduction to Database Systems. Database Introduction. Example Problem. Databases in the Real-World

Using Multiple Operations. Implementing Table Operations Using Structured Query Language (SQL)

Using the SQL Procedure


Databases in Engineering / Lab-1 (MS-Access/SQL)

Discovering SQL. Wiley Publishing, Inc. A HANDS-ON GUIDE FOR BEGINNERS. Alex Kriegel WILEY

Microsoft Access 3: Understanding and Creating Queries

Information Technology NVEQ Level 2 Class X IT207-NQ2012-Database Development (Basic) Student s Handbook

Chapter 1 Overview of the SQL Procedure

MySQL Command Syntax

Web Development using PHP (WD_PHP) Duration 1.5 months

Trading Dashboard Tutorial

Using AND in a Query: Step 1: Open Query Design

SQL Databases Course. by Applied Technology Research Center. This course provides training for MySQL, Oracle, SQL Server and PostgreSQL databases.

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

Advanced Query for Query Developers

Handling Missing Values in the SQL Procedure

IT2305 Database Systems I (Compulsory)

CHAPTER 2 DATABASE MANAGEMENT SYSTEM AND SECURITY

SQL, PL/SQL FALL Semester 2013

ETL Tools. L. Libkin 1 Data Integration and Exchange

Relational Databases. Christopher Simpkins

PeopleSoft Query Training

Database 10g Edition: All possible 10g features, either bundled or available at additional cost.

Boats bid bname color 101 Interlake blue 102 Interlake red 103 Clipper green 104 Marine red. Figure 1: Instances of Sailors, Boats and Reserves

Lab 9 Access PreLab Copy the prelab folder, Lab09 PreLab9_Access_intro

Introducción a las bases de datos SQL Libro de referencia

Comp 5311 Database Management Systems. 3. Structured Query Language 1

Talking to Databases: SQL for Designers

IT2304: Database Systems 1 (DBS 1)

ORM2Pwn: Exploiting injections in Hibernate ORM

A table is a collection of related data entries and it consists of columns and rows.

Introduction to SQL for Data Scientists

Transcription:

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

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. 2-2

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. 2-3

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. 2-4

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. 2-5

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). 2-6

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. 2-7

Cape Codd Retail Sales Structure 2-8

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. 2-9

Extracted Retail Sales Data Format 2-10

Retail Sales Extract Tables [in MS SQL Server] 2-11

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 (;). 2-12

Specific Columns on One Table SELECT FROM Department, Buyer SKU_DATA; 2-13

Specifying Column Order SELECT FROM Buyer, Department SKU_DATA; 2-14

The DISTINCT Keyword SELECT FROM SKU_DATA; DISTINCT Buyer, Department 2-15

Selecting All Columns: The Asterisk (*) Keyword SELECT * FROM SKU_DATA; 2-16

Specific Rows from One Table SELECT * FROM SKU_DATA WHERE Department = 'Water Sports'; NOTE: SQL wants a plain ASCII single quote: ' NOT! 2-17

Specific Columns and Rows from One Table SELECT FROM WHERE SKU_Description, Buyer SKU_DATA Department = 'Climbing'; 2-18

Using MS Access 2-19

Using MS Access (Continued) 2-20

Using MS Access (Continued) 2-21

Using MS Access (Continued) 2-22

Using MS Access (Continued) 2-23

Using MS Access - Results 2-24

Using MS Access Saving the Query 2-25

Using MS SQL Server 2008 The Microsoft SQL Server Management Studio I 2-26

Using MS SQL Server 2008 The Microsoft SQL Server Management Studio II 2-27

Using Oracle Database 11g SQL Developer I 2-28

Using Oracle Database 11g SQL Developer II 2-29

Using MySQL 5.1 MySQL Query Browser I 2-30

Using MySQL 5.1 MySQL Query Browser II 2-31

Sorting the Results ORDER BY SELECT * FROM ORDER BY ORDER_ITEM OrderNumber, Price; 2-32

Sort Order: Ascending and Descending SELECT * FROM ORDER_ITEM ORDER BY Price DESC, OrderNumber ASC; NOTE: The default sort order is ASC does not have to be specified. 2-33

WHERE Clause Options - AND SELECT * FROM SKU_DATA WHERE Department = 'Water Sports' AND Buyer = 'Nancy Meyers'; 2-34

WHERE Clause Options - OR SELECT * FROM SKU_DATA WHERE Department = 'Camping' OR Department = 'Climbing'; 2-35

WHERE Clause Options - IN SELECT * FROM SKU_DATA WHERE Buyer IN ('Nancy Meyers', 'Cindy Lo', 'Jerry Martin'); 2-36

WHERE Clause Options NOT IN SELECT * FROM SKU_DATA WHERE Buyer NOT IN ('Nancy Meyers', 'Cindy Lo', 'Jerry Martin'); 2-37

WHERE Clause Options Ranges with BETWEEN SELECT * FROM ORDER_ITEM WHERE ExtendedPrice BETWEEN 100 AND 200; 2-38

WHERE Clause Options Ranges with Math Symbols SELECT * FROM ORDER_ITEM WHERE ExtendedPrice >= 100 AND ExtendedPrice <= 200; 2-39

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 2-40

WHERE Clause Options LIKE and Wildcards SELECT * FROM SKU_DATA WHERE Buyer LIKE 'Pete%'; 2-41

WHERE Clause Options LIKE and Wildcards SELECT * FROM SKU_DATA WHERE SKU_Descripton LIKE '%Tent%'; 2-42

WHERE Clause Options LIKE and Wildcards SELECT * FROM SKU_DATA WHERE SKU LIKE '%2 '; 2-43

SQL Built-in Functions There are five SQL Built-in Functions: COUNT SUM AVG MIN MAX 2-44

SQL Built-in Functions SELECT SUM (ExtendedPrice) AS Order3000Sum FROM ORDER_ITEM WHERE OrderNumber = 3000; 2-45

SQL Built-in Functions SELECT FROM SUM (ExtendedPrice) AS OrderItemSum, AVG (ExtendedPrice) AS OrderItemAvg, MIN (ExtendedPrice) AS OrderItemMin, MAX (ExtendedPrice) AS OrderItemMax ORDER_ITEM; 2-46

SQL Built-in Functions SELECT FROM COUNT(*) AS NumberOfRows ORDER_ITEM; 2-47

SQL Built-in Functions SELECT FROM COUNT (DISTINCT Department) AS DeptCount SKU_DATA; 2-48

Arithmetic in SELECT Statements SELECT FROM Quantity * Price AS EP, ExtendedPrice ORDER_ITEM; 2-49

String Functions in SELECT Statements SELECT FROM DISTINCT RTRIM (Buyer) + ' in ' + RTRIM (Department) AS Sponsor SKU_DATA; 2-50

The SQL keyword GROUP BY SELECT Department, Buyer, COUNT(*) AS Dept_Buyer_SKU_Count FROM SKU_DATA GROUP BY Department, Buyer; 2-51

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. 2-52

The SQL keyword GROUP BY SELECT Department, COUNT(*) AS Dept_SKU_Count FROM SKU_DATA WHERE SKU <> 302000 GROUP BY Department ORDER BY Dept_SKU_Count; 2-53

The SQL keyword GROUP BY SELECT Department, COUNT(*) AS Dept_SKU_Count FROM SKU_DATA WHERE SKU <> 302000 GROUP BY Department HAVING COUNT (*) > 1 ORDER BY Dept_SKU_Count; 2-54

Querying Multiple Tables: Subqueries SELECT FROM WHERE SUM (ExtendedPrice) AS Revenue ORDER_ITEM SKU IN (SELECT SKU FROM SKU_DATA WHERE Department = 'Water Sports'); Note: The second SELECT statement is a subquery. 2-55

SELECT FROM WHERE Querying Multiple Tables: Buyer SKU_DATA SKU IN (SELECT FROM WHERE Subqueries SKU ORDER_ITEM OrderNumber IN (SELECT OrderNumber FROM RETAIL_ORDER WHERE OrderMonth = 'January' AND OrderYear = 2004)); 2-56

Querying Multiple Tables: Joins SELECT FROM WHERE Buyer, ExtendedPrice SKU_DATA, ORDER_ITEM SKU_DATA.SKU = ORDER_ITEM.SKU; 2-57

Querying Multiple Tables: Joins SELECT FROM WHERE GROUP BY ORDER BY Buyer, SUM(ExtendedPrice) AS BuyerRevenue SKU_DATA, ORDER_ITEM SKU_DATA.SKU = ORDER_ITEM.SKU Buyer BuyerRevenue DESC; 2-58

Querying Multiple Tables: Joins SELECT Buyer, ExtendedPrice, OrderMonth FROM SKU_DATA, ORDER_ITEM, RETAIL_ORDER WHERE SKU_DATA.SKU = ORDER_ITEM.SKU AND ORDER_ITEM.OrderNumber = RETAIL_ORDER.OrderNumber; 2-59

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. 2-60

David Kroenke and David Auer Database Processing Fundamentals, Design, and Implementation (11 th Edition) End of Presentation: Chapter Two 2-61