Presentation is loading. Please wait.

Presentation is loading. Please wait.

Jerry Post Copyright © 1998 1 Database Management Systems Chapter 5 Advanced Queries.

Similar presentations


Presentation on theme: "Jerry Post Copyright © 1998 1 Database Management Systems Chapter 5 Advanced Queries."— Presentation transcript:

1 Jerry Post Copyright © 1998 1 Database Management Systems Chapter 5 Advanced Queries

2 DATABASE 2 Tables

3 DATABASE 3 Organization  Harder Questions  Subqueries  Not In, LEFT JOIN  UNION, Multiple JOIN columns, Recursive JOIN  Crosstab  Other SQL Commands  DDL: Data Definition Language  DML: Data Manipulation Language

4 DATABASE 4 Harder Questions  How many cats are “in- stock” on 10/1/98?  Which cats sold for more than the average price?  Which animals sold for more than the average price of animals in their category?  Which animals have not been sold?  Which customers (who bought something at least once) did not buy anything between 11/1/98 and 12/31/98?  Which customers who bought Dogs also bought products for Cats (at any time)?

5 DATABASE 5 Sub-query for Calculation  Which cats sold for more than the average sale price of cats?  Assume we know the average price is $170.  Usually we need to compute it first. SELECT SaleAnimal.AnimalID, Animal.Category, SaleAnimal.SalePrice FROM Animal INNER JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalID WHERE ((Animal.Category="Cat") AND (SaleAnimal.SalePrice>170)); SELECT SaleAnimal.AnimalID, Animal.Category, SaleAnimal.SalePrice FROM Animal INNER JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalID WHERE ((Animal.Category="Cat") AND (SaleAnimal.SalePrice> (SELECT AVG(SalePrice) FROM Animal INNER JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalID WHERE (Animal.Category=“Cat”) ) ) );

6 DATABASE 6 Query Sets (IN)  List all customers (Name) who purchased one of the following items: 1, 2, 30, 32, 33. SELECT Customer.LastName, Customer.FirstName, SaleItem.ItemID FROM (Customer INNER JOIN Sale ON Customer.CustomerID = Sale.CustomerID) INNER JOIN SaleItem ON Sale.SaleID = SaleItem.SaleID WHERE (SaleItem.ItemID In (1,2,30,32,33)) ORDER BY Customer.LastName, Customer.FirstName;  Could use Or, but harder to write.  In (...) matches any in the list. Query04_13

7 DATABASE 7 Using IN with a Sub-query  List all customers who bought items for cats. SELECT Customer.LastName, Customer.FirstName, SaleItem.ItemID FROM (Customer INNER JOIN Sale ON Customer.CustomerID = Sale.CustomerID) INNER JOIN SaleItem ON Sale.SaleID = SaleItem.SaleID WHERE (SaleItem.ItemID In (SELECT ItemID FROM Merchandise WHERE Category="Cat") );

8 DATABASE 8 SubQuery (IN: Look up a Set)  List all of the customers who bought something in March and who bought something in May. (Two tests on the same data!)  LastNameFirst  AdkinsInga  McCainSam  GrimesEarl SELECT Customer.LastName, Customer.FirstName FROM Customer INNER JOIN Sale ON Customer.CustomerID = Sale.CustomerID WHERE ((Month([SaleDate])=3)) And Customer.CustomerID In (SELECT CustomerID FROM Sale WHERE (Month([SaleDate])=5) ); Query04_14

9 DATABASE 9 SubQuery (ANY, ALL)  Any: value is compared to each item in the list. If it is True for any of the items, the statement is evaluated to True.  All: value is compared to each item in the list. If it is True for every item in the list, the statement is evaluated to True (much more restrictive than any. SELECT DISTINCTROW Animal.AnimalID, Name,.SalePrice, ListPrice FROM Animal INNER JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalID WHERE (((SalePrice) > Any (SELECT 0.80*ListPrice FROM Animal INNER JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalID WHERE Category = "Cat")) AND ((Category)="Cat")); Query04_15

10 DATABASE 10 SubQuery: NOT IN (Subtract)  Which animals have not been sold?  Start with list of all animals.  Subtract out list of those who were sold.  AnimalIDNameCategory  12LeishaDog  19GeneDog  25VivianDog  34RhondaDog  88BrandyDog  181Fish SELECT Animal.AnimalID, Animal.Name, Animal.Category FROM Animal WHERE (Animal.AnimalID Not In (SELECT AnimalID From SaleAnimal)); Query04_16

11 DATABASE 11 SubQuery: NOT IN (Data) IDNameCategoryBreed 2FishAngel 4GaryDogDalmation 5FishShark 6RosieCatOriental Shorthair 7EugeneCatBombay 8MirandaDogNorfolk Terrier 9FishGuppy 10SherriDogSiberian Huskie 11SusanDogDalmation 12LeishaDogRottweiler IDSaleIDSalePrice 235$10.80 480$156.66 627$173.99 725$251.59 84$183.38 1018$150.11 1117$148.47 AnimalSaleAnimal Which animals have not been sold?

12 DATABASE 12 Left Outer Join  Which animals have not been sold?  LEFT JOIN includes all rows from left table (Animal)  But only those from right table (SaleAnimal) that match a row in Animal.  Rows in Animal without matching data in Sale Animal will have Null. SELECT Animal.AnimalID, Animal.Name, Animal.Category FROM Animal LEFT JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalID WHERE (SaleAnimal.SaleID Is Null);  AnimalIDNameCategory  12LeishaDog  19GeneDog  25VivianDog  34RhondaDog  88BrandyDog  181Fish Query04_17

13 DATABASE 13 Left Outer Join (Example) IDNameCategoryBreed 2FishAngel 4GaryDogDalmation 5FishShark 6RosieCatOriental Shorthair 7EugeneCatBombay 8MirandaDogNorfolk Terrier 9FishGuppy 10SherriDogSiberian Huskie 11SusanDogDalmation 12LeishaDogRottweiler IDSaleIDSalePrice 235$10.80 480$156.66 NullNullNull 627$173.99 725$251.59 84$183.38 NullNullNull 1018$150.11 1117$148.47 NullNullNull

14 DATABASE 14 Older Syntax for Left Join  Which animals have not been sold? SELECT ALL FROM Animal, SaleAnimal WHERE Animal.AnimalID *= SaleAnimal.AnimalID And SaleAnimal.SaleID Is Null; SELECT ALL FROM Animal, SaleAnimal WHERE Animal.AnimalID = (+) SaleAnimal.AnimalID And SaleAnimal.SaleID Is Null; Oracle syntax--note that the (+) symbol is on the reversed side.

15 DATABASE 15 SubQuery for Computation  Don’t know the average, so use a subquery to look it up.  Watch parentheses. Query04_18 SELECT SaleAnimal.AnimalID, Animal.Category, SaleAnimal.SalePrice FROM Animal INNER JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalID WHERE ((Animal.Category="Cat") AND (SaleAnimal.SalePrice> (SELECT AVG(SalePrice) FROM Animal INNER JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalID WHERE (Animal.Category=“Cat”) ) ) );

16 DATABASE 16 Correlated Subquery SELECT AnimalID, Name, Category, SalePrice FROM Animal INNER JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalID WHERE (SaleAnimal.SalePrice> (SELECT Avg(SaleAnimal.SalePrice) FROM Animal INNER JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalID WHERE (Animal.Category = Animal.Category) ) ) ORDER BY SaleAnimal.SalePrice DESC;  List the Animals that have sold for a price higher than the average for animals in that Category.  The subquery needs to compute the average for a given category.  Problem: Which category?  Answer: the category that matches the category from the main part of the query.  Problem: How do we refer to it? Both tables are called Animal. This query will not work yet.

17 DATABASE 17 Correlated SubQuery (Avoid)  List the Animals that have sold for a price higher than the average for animals in that Category.  Match category in subquery with top level  Rename tables (As)  Correlated Subquery  Recompute subquery for every row in top level-- slow!  Better to compute and save Subquery, then use in join. SELECT A1.AnimalID, A1.Name, A1.Category, SaleAnimal.SalePrice FROM Animal As A1 INNER JOIN SaleAnimal ON A1.AnimalID = SaleAnimal.AnimalID WHERE (SaleAnimal.SalePrice> (SELECT Avg(SaleAnimal.SalePrice) FROM Animal As A2 INNER JOIN SaleAnimal ON A2.AnimalID = SaleAnimal.AnimalID WHERE (A2.Category = A1.Category) ) ) ORDER BY SaleAnimal.SalePrice DESC; Query04_19

18 DATABASE 18 Correlated Subquery Problem Fish$10.80 Dog$156.66 Fish$19.80 Cat$173.99 Cat$251.59 Dog$183.38 Fish$1.80 Dog$150.11 Dog$148.47 CategorySalePrice Animal + SaleAnimal Compute Avg: $37.78 Compute Avg: $174.20 Compute Avg: $37.78 Compute Avg: $169.73 Recompute average for every row in the main query!  Assume small query  100,000 rows  5 categories of 20,000 rows  100,000 * 20,000 = 1 billion rows to read!

19 DATABASE 19 More Efficient Solution: 2 queries  Compute the averages once and save query  JOIN saved query to main query  Two passes through table: 1 billion / 200,000 => 10,000 Fish$10.80 Dog$156.66 Fish$19.80 Cat$173.99 Cat$251.59 Dog$183.38 Fish$1.80 Dog$150.11 Dog$148.47 CategorySalePrice Animal + SaleAnimal Bird$176.57 Cat$169.73 Dog$174.20 Fish$37.78 Mammal$80.72 Reptile$181.83 Spider$118.16 CategoryAvgOfSalePrice Saved Query JOIN Animal.Category = Query1.Category

20 DATABASE 20 UNION Operator  Offices in Los Angeles and New York.  Each has an Employee table (East and West).  Need to search data from both tables.  Columns in the two SELECT lines must match. SELECT EID, Name, Phone, Salary, ‘East’ AS Office FROM EmployeeEast UNION SELECT EID, Name, Phone, Salary, ‘West’ AS Office FROM EmployeeWest EIDNamePhoneSalaryOffice 352Jones335245,000East 876Inez873647,000East 372Stoiko763238,000East 890Smythe980362,000West 361Kim773673,000West

21 DATABASE 21 UNION, INTERSECT, EXCEPT T1T2 ABC SELECT EID, Name FROM EmployeeEast INTERSECT SELECT EID, Name FROM EmployeeWest List the name of any employee who has worked for both the East and West regions.

22 DATABASE 22 Multiple JOIN Columns  Sometimes need to JOIN tables on more than one column.  PetStore: Category and Breed. AnimalID Name Category Breed DateBorn Gender... Category Breed Animal SELECT * FROM Breed INNER JOIN Animal ON Breed.Category = Animal.Category AND Breed.Breed = Animal.Breed

23 DATABASE 23 Reflexive Join  Need to connect a table to itself.  Common example: Employee(EID, Name,..., Manager)  A manager is also an employee.  Use a second copy of the table and an alias. SELECT Employee.EID, Employee.Name, Employee.Manager, E2.Name FROM Employee INNER JOIN Employee AS E2 ON Employee.Manager = E2.EID EIDName...Manager 115Sanchez765 462Miller115 523Hawk115 765Munoz886 Employee EIDNameManagerName 115Sanchez765Munoz 462Miller115Sanchez 523Hawk115Sanchez SQL Result

24 DATABASE 24 CASE Function  Used to change data to a different context.  Example: Define age categories for the animals.  Less than 3 months  Between 3 months and 9 months  Between 9 months and 1 year  Over 1 year Select AnimalID, CASE WHEN Date()-DateBorn < 90 Then “Baby” WHEN Date()-DateBorn >= 90 AND Date()-DateBorn < 270 Then “Young” WHEN Date()-DateBorn >= 270 AND Date()-DateBorn < 365 Then “Grown” ELSE “Experienced” END FROM Animal; Not available in Microsoft Access. It is in SQL Server 6.5.

25 DATABASE 25 Inequality Join AR(TransactionID, CustomerID, Amount, DateDue)  AccountsReceivable  Categorize by Days Late  30, 90, 120+  Three queries?  New table for business rules LateCategory(Category, MinDays, MaxDays, Charge, …) Month30903% Quarter901205% Overdue120999910% SELECT * FROM AR INNER JOIN LateCategory ON ((Date() - AR.DateDue) >= Category.MinDays) AND ((Date() - AR.DateDue) < Category.MaxDays)

26 DATABASE 26 Crosstab Result MonthBirdCatDogFishMammalReptileSpider 1$217.51$1,655.01 2$324.87$597.74$1,281.81$39.60$127.78 3$364.18$198.85$650.17$378.25$40.50 4$334.50$1,221.10$172.88 5$396.84$335.48$1,192.56$126.20 6$119.71$459.91$1,607.46$5.40$19.80 7$573.60$1,644.73$319.07$19.80$182.31 8$578.35$1,444.75$1,859.71$27.90$320.52 9$538.07$792.46$1,219.03 10$173.50$942.89$1,429.27$10.80$11.70$229.73$313.97 11$153.07$1,308.97$1,784.06$112.50 12$770.87$1,178.47

27 DATABASE 27 Crosstab  Total animal sales by Category for each month. TRANSFORM Sum(SaleAnimal.SalePrice) AS SumOfSalePrice SELECT Month([Sale].[SaleDate]) AS SaleMonth FROM Sale INNER JOIN (Animal INNER JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalID) ON Sale.SaleID = SaleAnimal.SaleID GROUP BY Month([Sale].[SaleDate]) PIVOT Animal.Category; Query04_20

28 DATABASE 28 SQL 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 second select } TRANSFORM aggfunction{Crosstab values} SELECT... FROM... GROUP BY{Crosstab rows} PIVOT pivotfield {Crosstab columns}

29 DATABASE 29 SQL Mnemonic Someone From Ireland Will Grow Horseradish and Onions SELECT FROM INNER JOIN WHERE GROUP BY HAVING ORDER BY SQL is picky about putting the commands in the proper sequence. If you have to memorize the sequence, this mnemonic may be helpful.

30 DATABASE 30 SQL Data Definition  Create Schema Authorization dbName password  Create Table TableName (Column Type,...)  Alter Table Table {Add, Column, Constraint, Drop}  Drop {Table Table | Index Index On table}  Create Index IndexName ON Table (Column {ASC|DESC})

31 DATABASE 31 Syntax Examples CREATE TABLE Customer (CustomerID INTEGER NOT NULL, LastName CHAR (10), more columns ); ALTER TABLE Customer DROP COLUMN ZipCode; ALTER TABLE Customer ADD COLUMN CellPhone CHAR(15);

32 DATABASE 32 Queries with “Every” Need EXISTS List the employees who have sold animals from every category. By hand: List the employees and the categories. Go through the SaleAnimal list and check off the animals they have sold.

33 DATABASE 33 Query With EXISTS List the Animal categories that have not been sold by an employee (#5). SELECT Category FROM Category WHERE (Category <> "Other") And Category NOT IN (SELECT Animal.Category FROM Animal INNER JOIN (Sale INNER JOIN SaleAnimal ON Sale.SaleID = SaleAnimal.SaleID) ON Animal.AnimalID = SaleAnimal.AnimalID WHERE Sale.EmployeeID = 5) If this query returns any rows, then the employee has not sold every animal. So list all the employees for whom the above query returns no rows: SELECT EmployeeID, LastName FROM Employee WHERE NOT EXISTS (above query slightly modified.)

34 DATABASE 34 Query for Every SELECT Employee.EmployeeID, Employee.LastName FROM Employee WHERE Not Exists (SELECT Category FROM Category WHERE (Category <> "Other") And Category NOT IN (SELECT Animal.Category FROM Animal INNER JOIN (Sale INNER JOIN SaleAnimal ON Sale.SaleID = SaleAnimal.SaleID) ON Animal.AnimalID = SaleAnimal.AnimalID WHERE Sale.EmployeeID = Employee.EmployeeID) ); Result: 3 Reasoner

35 DATABASE 35 Simpler Query for Every Sometimes it is easier to use Crosstab and the Count function. But some systems do not have Crosstab, and sometimes the lists would be too long. So you need to know both techniques.

36 DATABASE 36 SQL: Foreign Key CREATE TABLE Order (OrderID INTEGER NOT NULL, OrderDate DATE, CustomerID INTEGER CONSTRAINT pkorder PRIMARY KEY (OrderID), CONSTRAINT fkorder FOREIGN KEY (CustomerID) REFERENCES Customer (CustomerID) ); OrderID OrderDate CustomerID LastName FirstName Address … OrderCustomer *

37 DATABASE 37 SQL Data Manipulation Commands Insert Into target (column1...) VALUES (value1...) Insert Into target (column1...) SELECT... FROM... Delete From table WHERE condition Update table SET Column=Value,... Where condition Note the use of the Select and Where conditions. Synatx is the same--only learn it once. You can also use subqueries.

38 DATABASE 38 Copy Old Animal Data INSERT INTO OldAnimals SELECT * FROM Animals WHERE AnimalID IN (SELECT AnimalOrderItem.AnimalID FROM AnimalOrder INNER JOIN AnimalOrderItem ON AnimalOrder.OrderID = AnimalOrderItem.OrderId WHERE (AnimalOrder.OrderDate<#1/1/98#) );

39 DATABASE 39 Delete Old Animal Data DELETE FROM Animals WHERE AnimalID IN (SELECT AnimalOrderItem.AnimalID FROM AnimalOrder INNER JOIN AnimalOrderItem ON AnimalOrder.OrderID = AnimalOrderItem.OrderId WHERE (AnimalOrder.OrderDate<#1/1/98#) );

40 DATABASE 40 Update Example  Change the ListPrice of Animals at the PetStore.  For cats, increase the ListPrice by 10%.  For dogs, increase the ListPrice by 20%.  Typically use two similar UPDATE statements.  With the CASE function, the statements can be combined. UPDATE Animal SET ListPrice = ListPrice*1.10 WHERE Category = “Cat” ; UPDATE Animal SET ListPrice = ListPrice*1.20 WHERE Category = “Dog” ;

41 DATABASE 41 Quality: Building Queries  Break questions into smaller pieces.  Test each query.  Check the SQL.  Look at the data.  Check computations  Combine into subqueries.  Use cut-and-paste to avoid errors.  Check for correlated subqueries.  Test sample data.  Identify different cases.  Check final query and subqueries.  Verify calculations. Dogs and cat products on the same sale. Dogs and cat products at different times. Dogs and never any cat products. Cat products and never any Dogs. Which customers who bought Dogs also bought products for Cats (at any time)? Who bought dogs? Who bought cat products?  Test SELECT queries before executing UPDATE queries.

42 DATABASE 42 Quality Queries: Example Which customers who bought Dogs also bought products for Cats? SELECT DISTINCT Animal.Category, Sale.CustomerID FROM Sale INNER JOIN (Animal INNER JOIN SaleAnimal ON Animal.AnimalID = SaleAnimal.AnimalID) ON Sale.SaleID = SaleAnimal.SaleID WHERE (((Animal.Category)="Dog")) SELECT DISTINCT Sale.CustomerID FROM Sale INNER JOIN (Merchandise INNER JOIN SaleItem ON Merchandise.ItemID = SaleItem.ItemID) ON Sale.SaleID = SaleItem.SaleID WHERE (((Merchandise.Category)="Cat")) A. Which customers bought dogs? B. Which customers bought cat products? AND Sale.CustomerID IN ( );

43 DATABASE 43 SQL Syntax: ALTER TABLE ALTER TABLE table ADD COLUMN column datatype (size) DROP COLUMN column See also: CREATE TABLE DROP TABLE

44 DATABASE 44 SQL Syntax: COMMIT COMMIT WORK See also: ROLLBACK

45 DATABASE 45 SQL Syntax: CREATE INDEX CREATE [UNIQUE] INDEX index ON table (column1, column2, … ) WITH {PRIMARY | DISALLOW NULL | IGNORE NULL} See also: CREATE TABLE

46 DATABASE 46 SQL Syntax: 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

47 DATABASE 47 SQL Syntax: CREATE VIEW CREATE VIEW viewname AS SELECT … See also: SELECT

48 DATABASE 48 SQL Syntax: DELETE DELETE FROM table WHERE condition See also: DROP

49 DATABASE 49 SQL Syntax: DROP DROP INDEX index ON table DROP TABLE DROP VIEW See also: DELETE

50 DATABASE 50 SQL Syntax: INSERT INSERT INTO table (column1, column2, …) VALUES (value1, value2, … ) INSERT INTO newtable (column1, column2, …) SELECT … See also: SELECT

51 DATABASE 51 SQL Syntax: GRANT GRANT privilege privileges ON object ALL, ALTER, DELETE, INDEX, TO user | PUBLIC INSERT, SELECT, UPDATE See also: REVOKE

52 DATABASE 52 SQL Syntax: REVOKE REVOKE privilege privileges ON object ALL, ALTER, DELETE, INDEX, FROM user | PUBLIC INSERT, SELECT, UPDATE See also: GRANT

53 DATABASE 53 SQL Syntax: ROLLBACK SAVEPOINT savepoint{optional} ROLLBACK WORK TO savepoint See also: COMMIT

54 DATABASE 54 SQL Syntax: 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 … } TRANSFORM aggfunction{Crosstab values} SELECT... FROM... GROUP BY{Crosstab rows} PIVOT pivot column {Crosstab columns}

55 DATABASE 55 SQL Syntax: SELECT INTO SELECT column1, column2, … INTO newtable FROM tables WHERE condition See also: SELECT

56 DATABASE 56 SQL Syntax: UPDATE UPDATE TABLE table SET column1 = value1, column2 = value2, … WHERE condition See also: DELETE


Download ppt "Jerry Post Copyright © 1998 1 Database Management Systems Chapter 5 Advanced Queries."

Similar presentations


Ads by Google