Download presentation
Presentation is loading. Please wait.
Published byAleesha Day Modified over 9 years ago
1
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
2
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
3
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
4
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!
5
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
6
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
7
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
8
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
9
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
10
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/01. 10 of 35
11
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
12
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
13
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
14
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 1 2 3 14 of 32 *
15
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
16
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…
17
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
18
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
19
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
20
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
21
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
22
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 > 10. 22 of 35
23
DBSYSTEMS SQL Syntax (May Be Useful for later use) 23 of 32
24
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
25
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
26
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
27
DBSYSTEMS CREATE INDEX CREATE [UNIQUE] INDEX index ON table (column1, column2, … ) WITH {PRIMARY | DISALLOW NULL | IGNORE NULL} See also: CREATE TABLE 27 of 32
28
DBSYSTEMS DROP DROP INDEX index ON table DROP TABLE DROP VIEW See also: DELETE 28 of 32
29
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
30
DBSYSTEMS UPDATE UPDATE TABLE table SET column1 = value1, column2 = value2, … WHERE condition See also: DELETE 30 of 32
31
DBSYSTEMS GRANT GRANT privilege privileges ON object ALL, ALTER, DELETE, INDEX, TO user | PUBLIC INSERT, SELECT, UPDATE See also: REVOKE 31 of 32
32
DBSYSTEMS REVOKE REVOKE privilege privileges ON object ALL, ALTER, DELETE, INDEX, FROM user | PUBLIC INSERT, SELECT, UPDATE See also: GRANT 32 of 32
33
DBSYSTEMS ROLLBACK SAVEPOINT savepoint{optional} ROLLBACK WORK TO savepoint See also: COMMIT 33 of 32
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.