Structured Query Language (SQL) Part A. KSE 521 Topic 10 Mun Yi

Similar documents
Database Processing: Fundamentals, Design, and Implementation

Lecture 2: Chapter Objectives

Lecture 2: Chapter Objectives. Relational Data Structure. Relational Data Model. Introduction to Relational Model & Structured Query Language (SQL)

Database Processing INSTRUCTOR S MANUAL. Fundamentals, Design, and Implementation. Chapter 2 Introduction to Structured Query Language

CSC Web Programming. Introduction to SQL

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

Full file at

Test Bank for Database Processing Fundamentals Design and Implementation 13th Edition by Kroenke

2) SQL includes a data definition language, a data manipulation language, and SQL/Persistent stored modules. Answer: TRUE Diff: 2 Page Ref: 36

Introduction. Sample Database SQL-92. Sample Data. Sample Data. Chapter 6 Introduction to Structured Query Language (SQL)

SQL functions fit into two broad categories: Data definition language Data manipulation language

Oracle Database 11g: SQL and PL/SQL Fundamentals

Oracle Database: SQL and PL/SQL Fundamentals NEW

Database Processing. Fundamentals, Design, and Implementation. Global Edition

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

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

CS 327E Lecture 2. Shirley Cohen. January 27, 2016

SQL stands for Structured Query Language. SQL lets you access and manipulate databases

Structure Query Language (SQL)

Introduction to SQL. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

30. Structured Query Language (SQL)

tablename ORDER BY column ASC tablename ORDER BY column DESC sortingorder, } The WHERE and ORDER BY clauses can be combined in one

CS 582 Database Management Systems II

DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

CGS 3066: Spring 2017 SQL Reference

12. MS Access Tables, Relationships, and Queries

Lesson 2. Data Manipulation Language

Lecture 06. Fall 2018 Borough of Manhattan Community College

MIS2502: Data Analytics SQL Getting Information Out of a Database. Jing Gong

Relational Database Management Systems for Epidemiologists: SQL Part I

Chapter # 7 Introduction to Structured Query Language (SQL) Part II

Introduction to SQL. From Rob and Coronel (2004), Database Systems: Design, Implementation, and Management

The Relational Model and Normalization

HKTA TANG HIN MEMORIAL SECONDARY SCHOOL SECONDARY 3 COMPUTER LITERACY. Name: ( ) Class: Date: Databases and Microsoft Access

Oracle Syllabus Course code-r10605 SQL

MIS2502: Data Analytics SQL Getting Information Out of a Database Part 1: Basic Queries

Chapter-14 SQL COMMANDS

In This Lecture. Yet More SQL SELECT ORDER BY. SQL SELECT Overview. ORDER BY Example. ORDER BY Example. Yet more SQL

Jarek Szlichta

Oracle Database 11g: SQL Fundamentals I

Database Management Systems by Hanh Pham GOALS

Oracle Database: Introduction to SQL Ed 2

Data Manipulation Language (DML)

RDBMS-Day3. SQL Basic DDL statements DML statements Aggregate functions

1. Data Definition Language.

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

1Z Oracle Database 11g - SQL Fundamentals I Exam Summary Syllabus Questions

GIFT Department of Computing Science Data Selection and Filtering using the SELECT Statement

The Structured Query Language Get Started

Oracle Database: SQL and PL/SQL Fundamentals

4. SQL - the Relational Database Language Standard 4.3 Data Manipulation Language (DML)

Introduction to SQL Part 1 By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Access 2007: Advanced Instructor s Edition

SQL Data Query Language

Announcements (September 14) SQL: Part I SQL. Creating and dropping tables. Basic queries: SFW statement. Example: reading a table

ASSIGNMENT NO Computer System with Open Source Operating System. 2. Mysql

Oracle Database: Introduction to SQL

Institute of Aga. Microsoft SQL Server LECTURER NIYAZ M. SALIH

1 Writing Basic SQL SELECT Statements 2 Restricting and Sorting Data

Oracle Database 10g: Introduction to SQL

SIT772 Database and Information Retrieval WEEK 6. RELATIONAL ALGEBRAS. The foundation of good database design

Institute of Aga. Network Database LECTURER NIYAZ M. SALIH

SQL. Structured Query Language

618 Index. BIT data type, 108, 109 BIT_LENGTH, 595f BIT VARYING data type, 108 BLOB data type, 108 Boolean data type, 109

ITCertMaster. Safe, simple and fast. 100% Pass guarantee! IT Certification Guaranteed, The Easy Way!

Querying Data with Transact SQL

Oracle Database: Introduction to SQL

NCSS: Databases and SQL

Simple SQL Queries (2)

STIDistrict Query (Basic)

Chapter # 7 Introduction to Structured Query Language (SQL) Part I

You can write a command to retrieve specified columns and all rows from a table, as illustrated

Chapter 3: Introduction to SQL

SQL Server Administration Class 4 of 4. Activant Prophet 21. Basic Data Manipulation

1) Introduction to SQL

5 SQL (Structured Query Language)

WEEK 3 TERADATA EXERCISES GUIDE

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

ORACLE TRAINING CURRICULUM. Relational Databases and Relational Database Management Systems

COMP 244 DATABASE CONCEPTS & APPLICATIONS

SQL for MySQL A Beginner s Tutorial

SQL. SQL Functions. SQL Aggregate Functions. SQL Scalar functions. SQL Aggregate Functions. The AVG() Function

CS1100: Data, Databases, and Queries QUERY CONSTRUCTION. CS1100 Microsoft Access 1

SQL Interview Questions

Key Points. COSC 122 Computer Fluency. Databases. What is a database? Databases in the Real-World DBMS. Database System Approach

Chapter 16: Databases


COMP102: Introduction to Databases, 4

Carnegie Mellon Univ. Dept. of Computer Science Database Applications. General Overview - rel. model. Overview - detailed - SQL

Database Systems SQL SL03

SQL. Char (30) can store ram, ramji007 or 80- b

DATABASE MANAGEMENT SYSTEMS PREPARED BY: ENGR. MOBEEN NAZAR

Oracle Database 10g: SQL Fundamentals I

Oracle Database 11g: Introduction to SQLRelease 2

Exact Numeric Data Types

3. Getting data out: database queries. Querying

Database Systems SQL SL03

MTA Database Administrator Fundamentals Course

Lab # 4 Hands-On. DDL and DML Advance SQL Statements Institute of Computer Science, University of Tartu, Estonia

Transcription:

Structured Query Language (SQL) Part A KSE 521 Topic 10 Mun Yi 1

Agenda SQL Background Cape Codd Outdoor Sports SQL Select Statement Syntax Joining Tables 2

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]. SQL is ubiquitous in enterprise-class DBMS products. 3

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. Data manipulation language (DML) statements. Used for data retrieval and data modification 4

Cape Codd Retail Sales Structure Cape Codd Outdoor Sports is a fictitious company based on an actual outdoor retail equipment vendor. 5

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 6

Extracted Retail Sales Data Format 7

Retail Sales Extract Tables 8

The SQL SELECT Statement The fundamental framework for SQL query states is the SQL SELECT statement: SELECT FROM WHERE {ColumnName(s)} {TableName(s)} {Conditions} All SQL statements end with a semicolon (;). 9

Specific Columns on One Table SELECT FROM Department, Buyer SKU_DATA; 10

Specifying Column Order SELECT FROM Buyer, Department SKU_DATA; 11

The DISTINCT Keyword SELECT FROM DISTINCT Buyer, Department SKU_DATA; 12

Selecting All Columns: The Asterisk (*) Keyword SELECT * FROM SKU_DATA; 13

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

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

Sorting the Results: ORDER BY SELECT * FROM ORDER_ITEM ORDER BY OrderNumber, Price; 16

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

WHERE Clause Options: AND SELECT * FROM SKU_DATA WHERE Department = 'Water Sports' AND Buyer = 'Nancy Meyers'; 18

WHERE Clause Options: OR SELECT * FROM SKU_DATA WHERE Department = 'Camping' OR Department = 'Climbing'; 19

WHERE Clause Options: IN SELECT * FROM SKU_DATA WHEREBuyer IN ('Nancy Meyers', 'Cindy Lo', 'Jerry Martin'); 20

WHERE Clause Options: NOT IN SELECT * FROM SKU_DATA WHEREBuyer NOT IN ('Nancy Meyers', 'Cindy Lo', 'Jerry Martin'); 21

WHERE Clause Options: Ranges with BETWEEN SELECT * FROM ORDER_ITEM WHERE ExtendedPrice BETWEEN 100 AND 200; 22

WHERE Clause Options: Ranges with Math Symbols SELECT * FROM ORDER_ITEM WHERE ExtendedPrice >= 100 AND ExtendedPrice <= 200; 23

WHERE Clause Options: LIKE and Wildcards The SQL keyword LIKE can be combined with wildcard symbols: SQL 92 Standard (SQL Server, Oracle, Teradata): _ = Exactly one character % = Any set of zero or more characters MS Access (based on MS DOS)? * = Exactly one character = Any set of one or more characters 24

WHERE Clause Options: LIKE and Wildcards (Continued) SELECT * FROM SKU_DATA WHERE Buyer LIKE 'Pete%'; 25

WHERE Clause Options: LIKE and Wildcards (Continued) SELECT * FROM SKU_DATA WHERE SKU_Description LIKE '%Tent%'; 26

WHERE Clause Options: LIKE and Wildcards SELECT * FROM SKU_DATA WHERE SKU LIKE '%2 '; 27

SQL Built-in Functions There are five SQL Built-in Functions: COUNT SUM AVG MIN MAX You cannot combine a table column name with a built-in function without GROUP BY. You cannot use a built-in function in a WHERE clause. 28

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

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

SQL Built-in Functions (Continued) SELECT FROM COUNT(*) AS NumRows ORDER_ITEM; 31

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

Arithmetic in SELECT Statements SELECT FROM Quantity * Price AS EP, ExtendedPrice ORDER_ITEM; 33

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

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. When using GROUP BY, only the column(s) in the GROUP BY expression and the SQL built-in functions can be used in the SELECT expression. 35

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

The SQL keyword GROUP BY (Continued) Any SQL built-in function can be used in the having clause. For example, SELECT COUNT(*) AS SKU_Count, SUM(Price) AS TotalRev, SKU FROM ORDER_ITEM GROUP BY SKU HAVING SUM(Price) = 100; 37

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

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

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

Querying Multiple Tables: Joins (Continued) 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; 41

Querying Multiple Tables: Joins (Continued) 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; 42

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