+ All Categories
Home > Documents > SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

Date post: 16-Jan-2016
Category:
Upload: lauren-chase
View: 218 times
Download: 0 times
Share this document with a friend
35
SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong
Transcript
Page 1: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

SQL Data Manipulation II

Chapter 5

CIS 458

Sungchul Hong

Page 2: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

Logical Operator

• AND, OR, NOT

A B A AND B A OR B NOT A

F F F F T

T F F T F

F T F T T

T T T T F

Page 3: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

Logical Operator

• DeMorgan’s Law– NOT (A AND B) – NOT (A OR B) – NOT (A AND B AND C)

Page 4: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

Row Selection (WHERE clause)

• Comparison

• Range

• Set membership

• Pattern match

• Null

Page 5: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

Comparison Search Condition

• SELECT staffNo, fName, lName, position, salary

• FROM Staff

• WHERE salary > 10000;

Page 6: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

SQL Comparison Operators

• = equals

• <> is not equal to

• < is less than

• > is greater than

• <= is less than or equal to

• >= is greater than or equal to

Page 7: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

Order of Evaluation

• An expression is evaluated left to right

• Subexpressions in brackets are evaluated first.

• NOTs are evaluated before ANDs and Ors

• ANDs are evaluated before ORs

Page 8: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

Compound Comparison Search

• SELECT *

• FROM Branch

• WHERE city = ‘London’ OR city = ‘Glasgow’;

Page 9: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

Range Search Condition

• BETWEEN / NOT BETWEEN

• SELET staffNo, fName, lName, position, salary

• FROM Staff

• WHERE salary BETWEEN 20000 AND 30000;

• SELET staffNo, fName, lName, position, salary

• FROM Staff

• WHERE salary >= 20000 AND salary <= 30000;

Page 10: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

Set Membership Search Condition

• IN / NOT IN• SELECT staffNO, fName, lName, positon• FROM Staff• WHERE position IN (‘Manager’, ‘Supervisor’);

• SELECT staffNO, fName, lName, positon• FROM Staff• WHERE position =‘Manager’ OR position =

‘Supervisor’;

Page 11: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

Pattern Match Search

• LIKE / NOT LIKE– % (* in MS): wildcard eg) LIKE ‘H*’– _: single character eg) LIKE ‘H___’– If the search string can include the pattern-matching

character itself, then– LIKE ’15#%’ ESCAPE ‘#’

– SELECT ownerNo, fName, lName, address, telNO– FROM PrivateOwner– WHERE address LIKE ‘*Glasgow*’;

Page 12: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

NULL Search Condition

• IS NULL / IS NOT NULL

• SELECT clientNO, viewDate• FROM Viewing• WHERE propertyNO = ‘PG4’ AND comment IS NULL;

• *

Page 13: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

Sorting Results

• ORDER BY column identifiers ( ASC or DESC)

• SELECT staffNO, fName, lName, salary

• FROM Staff

• ORDER BY salary DESC;

Page 14: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

Multiple Column Ordering

• SELECT properyNO, type, rooms, rent

• FROM PropertyForRent

• ORDER BY type, rent DESC;

Page 15: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

Aggregate Functions

• COUNT: numeric/non-numeric– COUNT(*): counts all the rows– DISTINCT: do not count duplicate values

• SUM: numeric

• AVG: numeric

• MIN: numeric/non-numeric

• MAX: numeric/non-numeric

Page 16: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

COUNT

• SELECT COUNT(*) AS count• FROM PropertyForRent• WHERE rent > 350;

• SELECT COUNT (DISTINCT propertyNO) AS count

• FROM Viewing

• WHERE viewDate BETWEEN ‘1-May-01’ AND ’31-May-01’;

Page 17: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

Count & SUM

• SELECT COUNT (StaffNo) AS count, SUM(salary) AS sum

• FROM Staff

• WHERE position = ‘Manager’;

Page 18: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

MIN, MAX, and AVG

• SELECT MIN(salary), MAX(salary), AVG(salary) AS avg

• FROM Staff;

Page 19: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

Grouping

• GROUP BY• In SELECT list, only column names, aggregate

functions, constants, and expression of previous combination.

• All column names in the SELECT list must appear in the GROUP BY clause unless the name is used only in an aggregate function.

• Each item in the SELECT list must be single-valued per group.

Page 20: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

GROUP BY• Find the number of staff working in each branch and the sum of their salaries.

• SELECT branchNO, COUNT(staffNo) AS count, SUM(salary) AS sum• FROM Staff• GROUP BY branchNO• ORDER BY branchNO;

• SELECT branchNO, (SELECT COUNT (staffNo) AS count • FROM Staff s• WHERE s.branchNo = b.branchNO),• (SELECT SUM(salary) AS sum• FROM Staff s• WHERE s.branchNo = b.branchNo)• FROM Branch b• ORDER BY branchNO;

Page 21: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

GROUP BY

• The result of GROUP BY

branchNO staffNo salary

B003

B003

B003

B005

B005

B007

SG37

SG14

SG5

SL21

SL41

SA9

12000.00

18000.00

24000.00

30000.00

9000.00

9000.00

COUNT(staffNo) SUM(salary)

3 54000.00

2 39000.00

1 9000.00

Page 22: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

GROUP BY

branchNO staffNo salary

B003

B003

B003

B005

B005

B007

SG37

SG14

SG5

SL21

SL41

SA9

12000.00

18000.00

24000.00

30000.00

9000.00

9000.00

branchNO staffNo salary

B003

B003

B003

B005

B005

B007

SG37

SG14

SG5

SL21

SL41

SA9

12000.00

18000.00

24000.00

30000.00

9000.00

9000.00

b s

Page 23: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

GROUP BY & HAVING

• For each branch office with more than one member of staff, find the number of staff working in each branch and the sum of their salaries.

SELECT branchNo, COUNT(staffNo) AS count, SUM (salary) AS sumFrom StaffGROUP BY branchNOHAVING COUNT(staffNO)>1ORDER BY branchNO;

Page 24: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

Subqueries

• Inner SELECT statement• Subquery or nested query• SELECT, INSERT, UPDATE, and DELETE• A scalar subquery returns a single column and a

single row.• A row subquery returns multiple columns, but

again only a single row.• A table subquery returns on or more columns and

multiple rows.

Page 25: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

Subquery with Equality

• List the staff who work in the branch at ‘163 Main St’

• SELECT staffNO, fName, lName, position

• FROM Staff

• WHERE branchNO = (SELECT branchNO

• FROM Branch

• WHERE street = ‘163 Main St’);

Page 26: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

Subquery with an Aggregate Function

• List all staff whose salary is greater than the average salary, and show by how much their salary is greater than the average.

• SELECT staffNO, fName, lName, position, salary – (SELECT AVG (salary) FROM Staff) AS salDiff

• FROM Staff

• WHERE salary > (SELECT AVG(salary) FROM Staff);

Page 27: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

Subquery Rules• The ORDER BY clause may not be used in a subquery.• The subquery SELECT list must consist of a single column

name or expression, except for subqueries that use the keyword EXISTS.

• BY default, column names in a subquery refer to the table name in the FROM clause of the subquery. It is possible to refer to a table in a FROM clause of an outer query by qualifying the column name.

• When a subquery is one of the two operands involved in a comparison, the subquery must appear on the right-hand side of the comparison.

Page 28: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

Nested subqueries: Use of IN

• List the properties that are handled by staff who work in the branch at ‘163 Main St’.

• SELECT propertyNo, street, city, postcode, type, rooms, rent

• FROM PropertyForRent

• WHERE staffNo IN (SELECT staffNO

• FROM Staff

• WHERE branchNO = (SELECT branchNO

• FROM Branch

• WHERE street = ‘163 Main St’));

Page 29: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

Use of ANY/SOME

• Find all staff whose salary is larger than the salary of at least one member of staff at branch B003.

• SELECT staffNO, fName, lName, position, salary

• FROM Staff

• WHERE salary > SOME (SELECT salary

• FROM Staff

• WHERE branchNo = ‘B003’);

Page 30: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

Use of ALL

• Find all staff whose salary is larger than the salary of every member of staff at branch B003.

• SELECT staffNO, fName, lName, position, salary• FROM Staff• WHERE salary > ALL (SELECT salary• FROM Staff• WHERE branchNo =‘B003’);

Page 31: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

Multi-Table Queries

• List the names of all clients who have viewed a property along with any comment supplied.

• SELECT c.clientNo, fName, lName, proertyNo, comment

• FROM Client c, Viewing v• WHERE c.clientNo = v.clientNo

Page 32: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

Database Updates

• INSERT

• UPDATE

• DELETE

Page 33: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

INSERT

• INSERT INTO Staff

• VALUES (‘SG16’, ‘Alan’, ‘Brown’, ‘Assistant’, ‘M’, DATE ‘1957-05-25’, 8300, ‘B003’)

Page 34: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

UPDATE

• UPDATE Staff

• SET salary = salary*1.03

Page 35: SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.

DELETE

• DELETE FROM Viewing

• WHERE propertyNo = ‘PG4’;


Recommended