Title: Information Resources Management
1Information Resources Management
2Agenda
- Administrivia
- Exam
- SQL Part 1
- Homework 5
3Administrivia
4Exam
5SQLStructured Query Language
- The standard relational database language
- Two Parts
- DDL - Data Definition Language
- DML - Data Manipulation Language
6SQL - DDL
- Data Definition Define schemas, delete
relations, create indices, modify schemas - View Definition
- Authorization
- Integrity
7SQL - DML
- Select, Insert, Modify, Delete Tuples
- Interactive
- Embedded
- Transaction Control
8SQL - DML
- SELECT
- Single table
- Multiple tables
- INSERT
- UPDATE
- DELETE
9SELECT
- SELECT attributes
- FROM table(s)
- WHERE conditions
- Result is a relation/table
10SELECT Example
- List all information in the Office table
- SELECT
- FROM Office
11SELECT Example
- List all offices in New York state
- SELECT
- FROM Office
- WHERE State NY
12SELECT Example
- List owners with at least 75 of some property
- SELECT
- FROM PctOwned
- WHERE PctOwned 75
13Select Example
- List all offices in Springfield, IL
14Select Example
- List all offices in Springfield, IL
- SELECT
- FROM Office
- WHERE City Springfield
- AND State IL
15Select Example
- List all properties listed by office 100 or 150
16Select Example
- List all properties listed by office 100 or 150
- SELECT
- FROM Property
- WHERE OfficeNbr 100
- OR OfficeNbr 150
17Select Example
- List the office number and phone number for all
offices in New York
18Select Example
- List the office number and phone number for all
offices in New York - SELECT OfficeNbr, PhoneNbr
- FROM Office
- WHERE State NY
19String Operations - LIKE
- - match any substring ()
- __ - match any character (?)
- SELECT EmpID, Name
- FROM Employee
- WHERE Name LIKE Tom
20LIKE Examples
- List all properties whose description includes a
fireplace
21LIKE Examples
- List all properties whose description includes a
fireplace - SELECT
- FROM Property
- WHERE Description LIKE fireplace
22LIKE Examples
- List all employees whose name is Rita and whose
last name is 4 characters long
23LIKE Examples
- List all employees whose name is Rita and whose
last name is 4 characters long - SELECT
- FROM Employee
- WHERE Name LIKE Rita _ _ _ _
24LIKE Examples
- List all employees whose name is Rita and whose
last name is at least 4 characters long
25LIKE Examples
- List all employees whose name is Rita and whose
last name is at least 4 characters long - SELECT
- FROM Employee
- WHERE Name LIKE Rita _ _ _ _
26Nulls
- An attribute that does not have any value is
assigned a value of NULL - not the same as zero
- not the same as empty string
- indicates no or unknown value
27Testing for Nulls
- WHERE attribute IS NULL
- WHERE attribute IS NOT NULL
28Selecting Expressions
- A mathematical expression can be selected instead
of an attribute - SELECT col1, col2col3, col4100
- FROM table
- WHERE conditions
29Expressions Example
- Assuming 5 commission, give the address and
potential commission for all Erie properties - SELECT address, price0.05
- FROM property
- WHERE cityErie AND state PA
30Selecting Expressions - 2
- Selected expressions can be given a name using AS
- SELECT col1, col2col3 AS newname
- FROM table
- WHERE conditions
31Expressions Example - 2
- Assuming 5 commission, give the address and
potential commission for all Erie properties - SELECT address, price0.05 AS comm
- FROM property
- WHERE cityErie AND state PA
32Renaming with AS
- Can also use AS in FROM
- SELECT FROM somelongtbl AS A
- rename table
- useful with multiple tables
33Eliminating Duplicates
- SELECT DISTINCT
- Entire tuple must be the same to be eliminated as
a duplicate
34Specifying the Order
- SELECT
- ORDER BY attribute(s)
- SELECT
- ORDER BY attrib ASC, attrib DESC
- attributes must be SELECTed
35Order Duplicate Example
- List all office numbers for offices with
employees sort by office number - SELECT DISTINCT OfficeNbr
- FROM Employee
- ORDER BY OfficeNbr
36Order Duplicate Example
- List the name, city, and state for all owners
that own at least 50 of a property sort by
state (descending) and city
37Order Duplicate Example
- 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
38Set Operations
- UNION (?)
- INTERSECT (?)
- EXCEPT (-)
- Add ALL to keep duplicates
39Aggregation Functions
40Aggregation Examples
- How many employees work at office 200?
- SELECT COUNT()
- FROM Employee
- WHERE OfficeNbr 200
41Aggregation Examples
- What is the average price of the listings for
office 225?
42Aggregation Examples
- What is the average price of the listings for
office 225? - SELECT AVG(Price)
- FROM Property
- WHERE OfficeNbr 225
43GROUP BY HAVING
- SELECT attributes, aggregation(attributes)
- FROM table
- WHERE conditions
- GROUP BY attributes
- HAVING condition
- HAVING only applies to each group (optional)
44GROUP BY Example
- What is the average price of a listing for each
office? - SELECT OfficeNbr, AVG(Price)
- FROM Property
- GROUP BY OfficeNbr
45GROUP BY Example
- For offices with more than 100 listings, what is
the total value of each offices listings?
46GROUP BY Example
- For offices with more than 100 listings, what is
the total value of each offices listings? - SELECT OfficeNbr, SUM(Price)
- FROM Property
- GROUP BY OfficeNbr
- HAVING COUNT(PropertyID) 100
47Cartesian Product Example
- List the names of all employees who work in
offices in PA - SELECT name
- FROM Employee, Office
- WHERE state PA
48Employee
49Office
50FROM Employee, Office
51FROM Employee,Office WHERE state PA
What is needed to fix this?
52Multiple Tables Example
- 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
53Multiple Tables Example
- List the employee ID and name of all employees
(not managers) who have Full access to PC 173
54Multiple Tables Example
- 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
55Multiple Tables - Join Conditions
- Usually joined via foreign keys
- A - B - C - D (4 tables)
- 3 join conditions (in WHERE)
- A - B
- B - C or A - C
- C - D or A - D or B - D
- composite keys - more conditions
56Duplicate Attribute Names
- Office x Property
- Attributes are OfficeNbr, Address, City, etc. and
PropertyID, Address, City, etc. - To distinguish office address from property
address, attach relation name (dot) to the front
of those attributes that are duplicated
57Duplicate Attribute Names
- OfficeNbr, Office.Address, Office.City,
Office.State, etc - PropertyID, Property.Address, Property.City,
Property.State, etc - Can also use AS
58Subqueries
- Result of a SELECT is a new table
- Can compare an attribute against a table (one
attribute only) - Is it there?
- How does it compare to the values in the table?
59Subqueries
- Can also test how many rows are in a table
- Are there any?
- Is there only one?
60Subqueries
- IN
- NOT IN
- EXISTS
- NOT EXISTS
- UNIQUE
- NOT UNIQUE
61Subquery Example
- 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)
62Using Subqueries
- Often, either a join or a subquery can be used
for the same result - Subquery may be easier
- If single value compared against list
- If existence (unique) needed
- If simple query identified nested
63Subquery Example
- List the names of all managers whose offices have
at least 1 property listed for at least 1,000,000
64Subquery Example
- 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
65Subquery Example
- 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)
66Subquery Example
- 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)
67Subquery Example
- 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)
- Whats wrong with this query?
68Subquery Example
- List the names of all managers whose offices only
have one listing whose price is at least
1,000,000
69Subquery Example
- 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)
70Subqueries
- 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)
71In-Class Exercise
72Homework 5
- Do SQL
- First 10
- Choose 10 of next 15
- Do remaining 5 with next weeks HW
- Keep problem numbers
- May want to implement in Access