Presentation is loading. Please wait.

Presentation is loading. Please wait.

Information Resources Management February 27, 2001.

Similar presentations


Presentation on theme: "Information Resources Management February 27, 2001."— Presentation transcript:

1 Information Resources Management February 27, 2001

2 Agenda n Administrivia n Exam n SQL Part 1 n Homework #5

3 Administrivia n Homework #4

4 Exam n Results n Review

5 SQL Structured Query Language n The standard relational database language n Two Parts n DDL - Data Definition Language n DML - Data Manipulation Language

6 SQL - DDL n Data Definition: Define schemas, delete relations, create indices, modify schemas n View Definition n Authorization n Integrity

7 SQL - DML n Select, Insert, Modify, Delete Tuples n Interactive n Embedded n Transaction Control

8 SQL - DML n SELECT n Single table n Multiple tables n INSERT n UPDATE n DELETE

9 SELECT SELECT attributes FROM table(s) WHERE conditions Result is a relation/table

10 SELECT Example n List all information in the Office table SELECT * FROM Office

11 SELECT Example n List all offices in New York state SELECT * FROM Office WHERE State = ‘NY’

12 SELECT Example n List owners with at least 75% of some property SELECT * FROM PctOwned WHERE PctOwned >= 75

13 Select Example n List all offices in Springfield, IL

14 Select Example n List all offices in Springfield, IL SELECT * FROM Office WHERE City = ‘Springfield’ AND State = ‘IL’

15 Select Example n List all properties listed by office 100 or 150

16 Select Example n List all properties listed by office 100 or 150 SELECT * FROM Property WHERE OfficeNbr = 100 OR OfficeNbr = 150

17 Select Example n List the office number and phone number for all offices in New York

18 Select Example n List the office number and phone number for all offices in New York SELECT OfficeNbr, PhoneNbr FROM Office WHERE State = ‘NY’

19 String Operations - LIKE n % - match any substring (*) n __ - match any character (?) SELECT EmpID, Name FROM Employee WHERE Name LIKE ‘Tom%’

20 LIKE Examples n List all properties whose description includes a fireplace

21 LIKE Examples n List all properties whose description includes a fireplace SELECT * FROM Property WHERE Description LIKE ‘%fireplace%’

22 LIKE Examples n List all employees whose name is Rita and whose last name is 4 characters long

23 LIKE Examples n List all employees whose name is Rita and whose last name is 4 characters long SELECT * FROM Employee WHERE Name LIKE ‘Rita _ _ _ _’

24 LIKE Examples n List all employees whose name is Rita and whose last name is at least 4 characters long

25 LIKE Examples n List all employees whose name is Rita and whose last name is at least 4 characters long SELECT * FROM Employee WHERE Name LIKE ‘Rita _ _ _ _%’

26 Nulls n An attribute that does not have any value is assigned a value of NULL n not the same as zero n not the same as empty string n indicates no or unknown value

27 Testing for Nulls n WHERE attribute IS NULL n WHERE attribute IS NOT NULL

28 Selecting Expressions n A mathematical expression can be selected instead of an attribute SELECT col1, col2*col3, col4+100 FROM table WHERE conditions

29 Expressions Example n Assuming 5% commission, give the address and potential commission for all Erie properties SELECT address, price*0.05 FROM property WHERE city=‘Erie’ AND state = ‘PA’

30 Selecting Expressions - 2 n Selected expressions can be given a name using AS SELECT col1, col2*col3 AS newname FROM table WHERE conditions

31 Expressions Example - 2 n Assuming 5% commission, give the address and potential commission for all Erie properties SELECT address, price*0.05 AS comm FROM property WHERE city=‘Erie’ AND state = ‘PA’

32 Renaming with AS n Can also use AS in FROM n SELECT * FROM somelongtbl AS A n rename table n useful with multiple tables

33 Eliminating Duplicates n SELECT DISTINCT … n Entire tuple must be the same to be eliminated as a duplicate

34 Specifying the Order n SELECT … ORDER BY attribute(s) n SELECT … ORDER BY attrib ASC, attrib DESC n attributes must be SELECTed

35 Order & Duplicate Example n List all office numbers for offices with employees; sort by office number SELECT DISTINCT OfficeNbr FROM Employee ORDER BY OfficeNbr

36 Order & Duplicate Example n List the name, city, and state for all owners that own at least 50% of a property; sort by state (descending) and city

37 Order & Duplicate Example n List the name, city, and state for all owners that own at least 50% of a property; sort by state (descending) and city SELECT DISTINCT Name, City, State FROM Owner AS O, PctOwned AS P WHERE O.OwnerSSN = P.OwnerSSN AND PctOwned >= 50 ORDER BY State DESC, City

38 Set Operations n UNION (  ) n INTERSECT (  ) n EXCEPT (-) n Add ALL to keep duplicates

39 Aggregation Functions n AVG n MIN n MAX n SUM n COUNT

40 Aggregation Examples n How many employees work at office 200? SELECT COUNT(*) FROM Employee WHERE OfficeNbr = 200

41 Aggregation Examples n What is the average price of the listings for office 225?

42 Aggregation Examples n What is the average price of the listings for office 225? SELECT AVG(Price) FROM Property WHERE OfficeNbr = 225

43 GROUP BY & HAVING SELECT attributes, aggregation(attributes) FROM table WHERE conditions GROUP BY attributes HAVING condition n HAVING only applies to each group (optional)

44 GROUP BY Example n What is the average price of a listing for each office? SELECT OfficeNbr, AVG(Price) FROM Property GROUP BY OfficeNbr

45 GROUP BY Example n For offices with more than 100 listings, what is the total value of each office’s listings?

46 GROUP BY Example n For offices with more than 100 listings, what is the total value of each office’s listings? SELECT OfficeNbr, SUM(Price) FROM Property GROUP BY OfficeNbr HAVING COUNT(PropertyID) > 100

47 Cartesian Product Example n List the names of all employees who work in offices in PA SELECT name FROM Employee, Office WHERE state = ‘PA’

48 Employee

49 Office

50 FROM Employee, Office

51 FROM Employee,Office WHERE state = ‘PA’ What is needed to fix this?

52 Multiple Tables Example n List the names of all employees who work in offices in PA SELECT E.Name FROM Employee AS E, Office AS O WHERE E.OfficeNbr = O.OfficeNBR AND O.State = ‘PA’

53 Multiple Tables Example n List the employee ID and name of all employees (not managers) who have ‘Full’ access to PC # 173

54 Multiple Tables Example n List the employee ID and name of all employees (not managers) who have ‘Full’ access to PC # 173 SELECT E.EmpID, E.Name FROM Employee AS E, PCAccess AS P WHERE E.EmpID = P.EmpID AND P.AccessType = ‘Full’ AND P.PC# = 173

55 Multiple Tables - Join Conditions n Usually joined via foreign keys n A - B - C - D (4 tables) n 3 join conditions (in WHERE) n A - B n B - C or A - C n C - D or A - D or B - D n composite keys - more conditions

56 Duplicate Attribute Names n Office x Property n Attributes are OfficeNbr, Address, City, etc. and PropertyID, Address, City, etc. n To distinguish office address from property address, attach relation name (dot) to the front of those attributes that are duplicated

57 Duplicate Attribute Names n OfficeNbr, Office.Address, Office.City, Office.State, etc… n PropertyID, Property.Address, Property.City, Property.State, etc… n Can also use AS

58 Subqueries n Result of a SELECT is a new table n Can compare an attribute against a table (one attribute only) n Is it there? n How does it compare to the values in the table?

59 Subqueries n Can also test how many rows are in a table n Are there any? n Is there only one?

60 Subqueries n IN n NOT IN n EXISTS n NOT EXISTS n UNIQUE n NOT UNIQUE n SOME n ( =, =, <>) n ALL n ( =, =, <>)

61 Subquery Example n List property for sale in PA at a price greater than the average price in PA SELECT * FROM Property WHERE state = ‘PA’ AND price > ALL (SELECT avg(price) FROM Property WHERE state = ‘PA’)

62 Using Subqueries n Often, either a join or a subquery can be used for the same result n Subquery may be easier n If single value compared against list n If existence (unique) needed n If simple query identified & “nested”

63 Subquery Example n List the names of all managers whose offices have at least 1 property listed for at least $1,000,000

64 Subquery Example n List the names of all managers whose offices have at least 1 property listed for at least $1,000,000 SELECT OfficeNbr FROM Property WHERE Price >= 1000000

65 Subquery Example n List the names of all managers whose offices have at least 1 property listed for at least $1,000,000 SELECT E.Name FROM Employee AS E, Manager AS M WHERE E.EmpID = M.EmpID AND M.OfficeNbr IN (SELECT OfficeNbr FROM Property WHERE Price >= 1000000)

66 Subquery Example n List the names of all managers whose offices have at least 1 property listed for at least $1,000,000 SELECT E.Name FROM Employee AS E, Manager AS M WHERE E.EmpID = M.EmpID AND EXISTS (SELECT * FROM Property as P WHERE P.OfficeNbr = M.OfficeNbr AND Price >= 1000000)

67 Subquery Example n List the names of all managers whose offices have at least 1 property listed for at least $1,000,000 SELECT E.Name FROM Employee AS E, Manager AS M WHERE E.EmpID = M.EmpID AND NOT UNIQUE (SELECT * FROM Property as P WHERE P.OfficeNbr = M.OfficeNbr AND Price >= 1000000) What’s wrong with this query?

68 Subquery Example n List the names of all managers whose offices only have one listing whose price is at least $1,000,000

69 Subquery Example n List the names of all managers whose offices only have one listing whose price is at least $1,000,000 SELECT E.Name FROM Employee AS E, Manager AS M WHERE E.EmpID = M.EmpID AND UNIQUE (SELECT P.OfficeNbr FROM Property as P WHERE P.OfficeNbr = M.OfficeNbr AND P.Price >= 1000000)

70 Subqueries n A subquery can also be used in the HAVING clause of a SELECT statement SELECT AVG(price), state FROM Property GROUP BY state HAVING AVG(price) > ALL (SELECT AVG(price) FROM Property)

71 In-Class Exercise n SQL n All queries

72 Homework #5 n Do SQL n First 10 n Choose 10 of next 15 n Do remaining 5 with next week’s HW n Keep problem numbers n May want to implement in Access


Download ppt "Information Resources Management February 27, 2001."

Similar presentations


Ads by Google