Chapter 4 Querying Based on G. Post, DBMS: Designing & Building Business Applications University of Manitoba Asper School of Business 3500 DBMS Bob Travica.

Slides:



Advertisements
Similar presentations
Advanced SQL (part 1) CS263 Lecture 7.
Advertisements

TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
Concepts of Database Management Seventh Edition
Concepts of Database Management Sixth Edition
Concepts of Database Management Seventh Edition
Database Systems: Design, Implementation, and Management Tenth Edition
Introduction to Structured Query Language (SQL)
The University of Akron Dept of Business Technology Computer Information Systems The Relational Model: Query-By-Example (QBE) 2440: 180 Database Concepts.
5 Chapter 5 Structured Query Language (SQL2) Revision.
Chapter Eight Managing Data Resources File Organization and Concepts.
Introduction to Structured Query Language (SQL)
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
Chapter 2 Database System Design (part II)
DATABASE APPLICATION DEVELOPMENT SAK 3408 Query Statements.
DBSYSTEMS 1 of 13 Chapter 10 DB System Administration (Part II) 1 Based on G. Post, DBMS: Designing & Building Business Applications University of Manitoba.
Introduction to Structured Query Language (SQL)
Jerry Post McGraw-Hill/Irwin Copyright © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Database Management Systems Chapter 4 Data Queries.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
1.NET Web Forms Database Queries © 2002 by Jerry Post.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
Jerry Post Copyright © Database Management Systems Chapter 4 Queries.
Concepts of Database Management, Fifth Edition
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Chapter 3 Single-Table Queries
Chapter 8 Newer Database Topics Based on G. Post, DBMS: Designing & Building Business Applications University of Manitoba Asper School of Business 3500.
Chapter 5 Advanced Querying
1 All Powder Board and Ski Oracle 9i Workbook Chapter 7: Integrity and Transactions Jerry Post Copyright © 2003.
Jerry Post Copyright © Database Management Systems Chapter 4 Queries.
School of Computer & Communication of LNPU 辽宁石油化工大学计算机与通信工程学院 刘旸 1 数据库管理系统 Database Management Systems Chapter4 Queries 第四章 数据查询.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Concepts of Database Management Seventh Edition
Jerry Post Copyright © Database Management Systems Chapter 5 Advanced Queries.
Getting to Know SQL. © Jim Hope 2002 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement TRANSFORM.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
1 Copyright © 2010 Jerry Post & M. E. Kabay. All rights reserved. Queries: Part 2 of 2 IS240 – DBMS Lecture # 7 – M. E. Kabay, PhD, CISSP-ISSMP.
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 All Powder Board and Ski Microsoft Access Workbook Chapter 8: Data Warehouses and Data Mining Jerry Post Copyright © 2003.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
1 MySQL and SQL. 2 Topics  Introducing Relational Databases  Terminology  Managing Databases MySQL and SQL.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
1 Copyright © 2010 Jerry Post & M. E. Kabay. All rights reserved. Queries: Part 1 of 2 IS240 – DBMS Lecture # 6 – M. E. Kabay, PhD, CISSP-ISSMP.
Jerry Post McGraw-Hill/Irwin Copyright © 2005 by The McGraw-Hill Companies, Inc. All rights reserved. Database Management Systems Chapter 4 Data Queries.
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
IMS 4212: Constraints & Triggers 1 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures in SQL Server.
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.
Jerry Post Copyright © Database Management Systems Chapter 4 Queries.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Southern Methodist University CSE CSE 2337 Introduction to Data Management Chapter 2.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
1 Ch4 Summary Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
How to: SQL By: Sam Loch.
SQL Query Getting to the data ……..
Structured Query Language
Queries: Part 1 of 2 IS240 – DBMS Lecture # 6 –
Relational Database Design
M. E. Kabay, PhD, CISSP-ISSMP V:
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Chapter 4 Summary Query.
SQL Fundamentals in Three Hours
Section 4 - Sorting/Functions
Presentation transcript:

Chapter 4 Querying Based on G. Post, DBMS: Designing & Building Business Applications University of Manitoba Asper School of Business 3500 DBMS Bob Travica Updated 2015

DBSYSTEMS Why do we Need Queries  To extract meaningful data from database, which help us answer business questions. Querying databases is where business value from a DB system is drawn.  We need a standardized system so users and developers can learn one method that works on any (most) systems.  SQL vs. Query By Example (QBE) 2 of 35

DBSYSTEMS Four Questions to Create a Query  What data do you need to get? (columns in tables)  What table or tables are involved?  What are the constraints (filtering conditions for data, search terms)?  If more tables needed, how to join them?  Download Sally’s Pet Store 2010 (queries work with it) Download Sally’s Pet Store 2010 (queries work with it) 3 of 35

DBSYSTEMS SupplierID Name ContactName Phone Address ZipCode CityID Supplier PONumber OrderDate ReceiveDate SupplierID EmployeeID ShippingCost Merchandise Order OrderID OrderDate ReceiveDate SupplierID ShippingCost EmployeeID AnimalOrder OrderID AnimalID Cost Animal OrderItem CityID ZipCode City State AreaCode Population1990 Population1980 Country Latitude Longitude City EmployeeID LastName FirstName Phone Address ZipCode CityID TaxPayerID DateHired DateReleased Employee PONumber ItemID Quantity Cost OrderItem Category Registration Category Breed AnimalID Name Category Breed DateBorn Gender Registered Color ListPrice Photo Animal SaleID SaleDate EmployeeID CustomerID SalesTax Sale SaleID ItemID Quantity SalePrice SaleItem ItemID Description QuantityOnHand ListPrice Category Merchandise SaleID AnimalID SalePrice SaleAnimal CustomerID Phone FirstName LastName Address ZipCode CityID Customer * * * * * * * * * * * * * * * * * * * * Your Querying Play Pen: Sally’s Pet Store Database 4 of 32 Know your data!

DBSYSTEMS Sample Questions  1) List all animals with yellow in their color.  2) List all dogs with yellow in their color born after 6/1/01.  3) List all merchandise for cats with a list price greater than $10.  4) List all dogs who are male and registered or who were born before 6/1/01 and have white in their color.  5) What is the average sale price of all animals?  6) What is the value of merchandise sold per transaction record?  7) List the top 10 customers and total amount they spent.  8) How many cats are in the animal list?  9) Show animals in each category with the count above 10.  10) List the CustomerID of the customers that purchased something between 4/1/01 and 5/31/01. 5 of 35

DBSYSTEMS Query By Example vs. SQL Query #1: List all animals with yellow in their color (only or along with other colors). SELECT * FROMAnimal WHERE(Color LIKE “*yellow*”); Which tables? Which data (columns) Which constraints 6 of 35

DBSYSTEMS SQL Query – Structure (Partial) SELECTcolumns- What columns (information) to display? FROMtables- What tables are involved? (INNER JOIN- If more than one table used: What columns are the tables joined on - PK & FK ?) WHEREconstraints- What are the constraints (filtering conditions) on records to be searched and displayed? 7 of 32

DBSYSTEMS More on Sample Query #1  Query question: List IDs, category, breed and color for animals that have yellow in their color.  Query statement: SELECTAnimalID, Category, Breed, Color FROMAnimal WHEREColor LIKE “*yellow*”;  Variations of filtering condition – What do you get?: - WHERE Color like “yellow”, Color = “yellow” - WHERE Color LIKE [Please type color] - Application in parameter query (asking user to enter desired color) 8 of 32

DBSYSTEMS ORDER BY (Sorting) SELECTcolumns FROMtables INNER JOIN columns WHEREconstraints ORDER BY columns ASC {or DESC} SELECT Category, Breed FROM Animal ORDER BY Category, Breed; Category Breed Bird Parakeet Bird Parrot Cat Abyssinian Cat American Short … Output Try to reverse Breed and Category in ORDER BY (and SELECT). 9 of 32

DBSYSTEMS Constraints SELECTAnimalID, Category, Color, DateBorn FROMAnimal WHERE((Category="Dog") AND (Color Like "*Yellow*") AND (DateBorn > #6/1/2001#)); Implemented as operators (Relational, Boolean, String) String Operator Boolean Operator Relational Operator  Query #2: List all dogs with yellow in their color and born after 6/1/ of 35

DBSYSTEMS  Query #3: List IDs and descriptions for the merchandise for cats with a list price greater than $10.  SQL statement: SELECT ItemID, Description, Category, ListPrice FROM Merchandise WHERE Category = "Cat" AND ListPrice > 10; More on AND and > Operators Output: 11 of 35

DBSYSTEMS The AND vs. OR Operators  Query #4: List all dogs that are male and registered or that were born before 6/1/2007 and have white in their color. Output will include some older non-registered dogs of either gender. Note the Registered constraint (a null value = a blank cell, missing data). - cannot be replaced by zero (0) - a reserved word in SQL syntax - can be understood as “we don’t know what the value is” Best to write this query in two parts, test each separately, then put them together. SELECT AnimalID, Category, Gender, Registered, DateBorn, Color FROM Animal WHERE (Category="Dog" AND Gender="Male" AND Registered Is Not Null) OR (Category="Dog" AND DateBorn < #June 1, 2007# AND Color Like "*White*") ; 12 of 35

DBSYSTEMS Boolean Algebra AND:True if both A and B exist. OR: True if either A or B or both exist. NOT:A (or B) does not exist. 2. Query with OR: (a > 4) OR (b < 0) F FT 1. Query with AND: (a > 4) AND (b < 0) = (3 > 4) AND (-1 < 0) FT T 3. Query with NOT: NOT (a > 4) F a = 3 b = -1 Example Record attributes a & b: No match, Record not fethced Match! Match 13 of 32 T

DBSYSTEMS BOO- lean Algebra Parentheses indicate order of operations – start working from within parentheses, then work out with resulting values. a = 7 b = 200 c = 2 F T F T ( (a > 10) AND (b => 200) ) OR (c > 1) T of 32 *

DBSYSTEMS DeMorgan’s Law Customer: "I want to look at a cat, but I don’t want any cats that are registered or that have red in their color." SELECT Category, Registered, Color FROM Animal WHERE (Category="cat") AND NOT ((Registered Is NOT NULL) OR (Color LIKE "*red*")); More… CategoryRegisteredColor CatGray/Blue CatWhite CatWhite CatYellow CatWhite Used for simplifying cumbersome statements using NOT: Output: 15 of 32

Transformed into a simpler form by DeMorgan’s Law: SELECT Category, Registered, Color FROM Animal WHERE Category="cat" AND Registered Is Null AND Color NOT LIKE "*red*"; 16 of 35 More…

DBSYSTEMS  Negation of clauses  NOT (A AND B) becomes NOT A OR NOT B, and the global NOT is deleted  NOT (A OR B) becomes NOT A AND NOT B, global NOT is deleted  IS NOT becomes IS  IS, = become NOT TF T F NOT ((Registered IS NOT null) OR (Color LIKE “*red*”)) Example: Constraints are Registered=ASCF, Color=Black (Registered IS null) AND NOT (Color LIKE “*red*”) F T F OR NOT AND F NOT DeMorgan’s Law applied: 17 of 35

DBSYSTEMS Computations – Row By Row Query #6: What is the value of merchandise sold per transaction record? SELECT SaleID, ItemID, SalePrice, Quantity, SalePrice*Quantity As Sum FROM SaleItem; Partial output: 18 of 32

DBSYSTEMS Computations On All Rows Query #5: What is the average donation price of all animals? SELECT Avg(Donation) AS [Average Donation Price] FROM Animal; DBMS supports math & stats functions:  Sum  Avg  Min  Max  Count  StDev  Var Play with these functions to learn more about animals sold! 19 of 35

DBSYSTEMS Computations On Select Rows (with WHERE clause) Query #8: How many cats are in the Animal list? SELECT Count(AnimalID) AS [Cats Total] FROM Animal WHERE Category = “Cat” ; How would you make a general (parameter) query to find the count of animals in any category the user wants? 20 of 35

DBSYSTEMS Aggregate Functions – GROUP BY Clause Arithmetic and statistical functions that work on an appropriate set of records. Question: What is the average donation price per animal category? The first idea is to build the query can on the previous, with addition of attribute Category: SELECT Category, Avg(Donation) AS [Average Donation Price] FROM Animal; But DB system will report an error that Category is not part of an aggregate function. Correct query is: SELECT Category, Avg(Donation) AS [Average Donation Price] FROM Animal GROUP BY Category ; 21 of 35

DBSYSTEMS Computations On Select Rows (with HAVING clause)  Query #9: Show Categories of animals with the count above 10. SELECTCategory, Count(AnimalID) AS CountOfAnimalID FROMAnimal GROUP BYCategory HAVINGCount(AnimalID) > 10 ORDER BYCount(AnimalID) DESC; optional Processing order: (1) Group rows by attribute Category; (2) Find the average for each group; (3) Count rows in each group; (4) Compare the counts with number 10; (5) select and display Category names and their counts where the count > of 35

DBSYSTEMS SQL Syntax (May Be Useful for later use) 23 of 32

DBSYSTEMS SELECT SELECT DISTINCT table.column {AS alias},... FROM table/query INNER JOIN table/query ON T1.ColA = T2.ColB WHERE (condition) GROUP BY column HAVING (group condition) ORDER BY table.column { UNION, INTERSECT, EXCEPT … } GROUP BY CUBE (dimension1, dimension2, …) TRANSFORM aggfunction{Crosstab values} SELECT... FROM... GROUP BY{Crosstab rows} PIVOT pivot column {Crosstab columns} 24 of 32

DBSYSTEMS CREATE TABLE CREATE TABLE table ( column1datatype (size) [NOT NULL] [index1], column2datatype (size) [NOT NULL] [index2], …, CONSTRAINT pkname PRIMARY KEY (column, …), CONSTRAINT fkname FOREIGN KEY (column) REFERENCES existing_table (key_column), ) See also: ALTER TABLE DROP TABLE 25 of 32

DBSYSTEMS ALTER TABLE ALTER TABLE table ADD COLUMN column datatype (size) DROP COLUMN column See also: CREATE TABLEDROP TABLE SQL Syntax: COMMIT COMMIT WORK See also: ROLLBACK 26 of 32

DBSYSTEMS CREATE INDEX CREATE [UNIQUE] INDEX index ON table (column1, column2, … ) WITH {PRIMARY | DISALLOW NULL | IGNORE NULL} See also: CREATE TABLE 27 of 32

DBSYSTEMS DROP DROP INDEX index ON table DROP TABLE DROP VIEW See also: DELETE 28 of 32

DBSYSTEMS DELETE FROM table WHERE condition See also: DROP 29 of 35 INSERT INSERT INTO table (column1, column2, …) VALUES (value1, value2, … ) INSERT INTO newtable (column1, column2, …) SELECT … 29 of 32

DBSYSTEMS UPDATE UPDATE TABLE table SET column1 = value1, column2 = value2, … WHERE condition See also: DELETE 30 of 32

DBSYSTEMS GRANT GRANT privilege privileges ON object ALL, ALTER, DELETE, INDEX, TO user | PUBLIC INSERT, SELECT, UPDATE See also: REVOKE 31 of 32

DBSYSTEMS REVOKE REVOKE privilege privileges ON object ALL, ALTER, DELETE, INDEX, FROM user | PUBLIC INSERT, SELECT, UPDATE See also: GRANT 32 of 32

DBSYSTEMS ROLLBACK SAVEPOINT savepoint{optional} ROLLBACK WORK TO savepoint See also: COMMIT 33 of 32