chapter 5 sql. agenda data manipulation language (dml) –select –union compatible operations...

57
Chapter 5 SQL

Post on 19-Dec-2015

225 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

Chapter 5

SQL

Page 2: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

Agenda

• Data Manipulation Language (DML)– SELECT– Union compatible operations– Update database

Page 3: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

SQL DML - SELECT

• SELECT [DISTINCT|ALL] {* | [col-expr [AS newname]][,...]

FROM table-name [alias] [,...] [WHERE condition] [GROUP BY colm [, colm] [HAVING condition]] ORDER BY colm [, colm]

Page 4: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

SQL DML - SELECT

• SELECT attributes (or calculations: +, -, /, *)

FROM relation

• SELECT DISTINCT attributes FROM relation

Page 5: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

Examples

• SELECT stunameFROM student;

• SELECT stuid, stuname, creditFROM student;

• SELECT stuid, stuname, credit+10FROM student;

• SELECT DISTINCT majorFROM student;

Page 6: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

SQL DML - SELECT

• SELECT attributes (or * wild card) FROM relation WHERE condition

Page 7: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

Examples

• SELECT *FROM student;

• SELECT stuname, major, creditFROM student

WHERE stuid = ‘S114’;

• SELECT *FROM faculty

WHERE dept = ‘MIS’;

Page 8: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

SELECT - WHERE condition

• AND OR• NOT IN• NOT IN BETWEEN• IS NULL IS NOT NULL• SOME ALL• LIKE '%' multiple characters • LIKE ‘_’ single character

Page 9: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

Examples• SELECT *

FROM facultyWHERE dept = ‘MIS’

AND rank = ‘full professor’;

• SELECT *FROM faculty

WHERE dept = ‘MIS’ OR rank = ‘full professor’;

• SELECT *FROM faculty

WHERE dept = ‘MIS’ NOT rank = ‘full professor’;

Page 10: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

• SELECT *FROM class

WHERE room LIKE ‘B_S%’;

• SELECT *FROM class

WHERE room NOT LIKE ‘BUS%’;

• SELECT productid, productnameFROM inventory

WHERE onhand BETWEEN 50 and 100;

Page 11: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

• SELECT companyid, companynameFROM companyWHERE companyname BETWEEN

‘G’ AND ‘K’;

• SELECT productid, productnameFROM inventoryWHERE onhand NOT BETWEEN

50 and 100;

• SELECT companyid, companynameFROM companyWHERE companyname NOT

BETWEEN ‘G’ AND ‘K’;

Page 12: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

• SELECT facnameFROM facultyWHERE dept IN (‘MIS’, ‘ACT’);

• SELECT facnameFROM facultyWHERE rank NOT IN (‘assistant’, ‘lecture’);

• SELECT customernameFROM customerWHERE emailadd IS NOT NULL;

Page 13: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

• SELECT customernameFROM customer

WHERE creditlimit IS NULL;

Page 14: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

SELECT - aggregate functions

• COUNT (*)• COUNT• SUM• AVG• MIN• MAX

Page 15: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

Examples

• SELECT COUNT(*)FROM student;

• SELECT COUNT(major)FROM student;

• SELECT COUNT(DISTINCT major)FROM student;

Page 16: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

• SELECT COUNT(stuid), SUM(credit), AVG(credit), MAX(credit),

MIN(credit)FROM student;

Page 17: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

• How many different guests have made bookings for August 2004?

• Hotel (hotelno, hotelname, city)

• Room (roomno, hotelno, type, price)

• Booking (hotelno, guestno, datefrom, dateto, roomno)

• Guest (guestno, guestname, guestaddress)

Page 18: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

• How many different guests have made bookings for August 2004?

•  

• SELECT COUNT(DISTINCT guestno)

• FROM booking

• WHERE (datefrom <= ‘8/31/04’) AND

• (dateto >= ‘8/1/04’);

Page 19: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

SELECT - GROUP

• GROUP BY• HAVING

Page 20: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

Examples

• SELECT major, AVG(credit)FROM student

GROUP BY major

• SELECT course#, COUNT(stuid)FROM enrollment

GROUP BY course#

Page 21: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

Examples

• SELECT major, AVG(credit)FROM studentGROUP BY major

HAVING COUNT(*) > 2;• SELECT course#, COUNT(stuid)

FROM enrollmentGROUP BY course#

HAVING COUNT(*) > 2;

Page 22: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

• SELECT major, AVG(credit)FROM student

WHERE major IN (‘mis’, ‘act’) GROUP BY major

HAVING COUNT(*) > 2;

Page 23: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

SELECT - ORDER BY

• ORDER BY• ORDER BY ... DESC

Page 24: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

Examples

• SELECT facname, rank FROM faculty

ORDER BY facname;

• SELECT facname, rank FROM faculty

ORDER BY rank DESC, facname;

Page 25: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

SELECT - JOIN Tables

• Multiple tables in FROM clause• MUST have join conditions!!!

Page 26: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

Examples

• SELECT stuname, gradeFROM student, enrollmentWHERE student.stuid =

enrollment.stuid;

Page 27: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

• SELECT enrollment.course#, stuname, major

FROM class, enrollment, studentWHERE class.course# =

enrollment.course#AND enrollment.stuid = student.stuidAND facid = ‘F114’

ORDER BY enrollment.course#;

Page 28: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

OUTER JOINS

• RIGHT JOIN• LEFT JOIN• FULL JOIN• Appending (+) to the optional column

(null) in the join condition (Oracle)

Page 29: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

Examples

• SELECT f.facid, f.facname, c.course# FROM class c, faculty fWHERE c.facid (+) = f.facid

ORDER BY f.facname; (rightouterjoin)

• SELECT s.stuname, major, gradeFROM student s, enrollment eWHERE s.stuid = e.stuid (+)

ORDER BY s.stuname; (leftouterjoin)

Page 30: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

• SELECT f.facid, f.facname, c.course# FROM class c RIGHT JOIN faculty fON c.facid = f.facid

ORDER BY f.facname; (rightouterjoin)• SELECT s.stuname, major, grade

FROM student s LEFT JOIN enrollment e

ON s.stuid = e.stuid

ORDER BY s.stuname; (leftouterjoin)

Page 31: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

• SELECT f.facid, f.facname, c.course#, c.room

FROM class c, faculty fWHERE c.facid (+) =

f.facid (+) ORDER BY f.facname; (fullouterjoin)

Page 32: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

• SELECT f.facid, f.facname, c.course#, c.room

FROM class c FULL JOIN faculty f ON c.facid = f.facid

ORDER BY f.facname; (fullouterjoin)

Page 33: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

Example

• List the number of room in each hotel in London.

• Hotel (hotelno, hotelname, city)

• Room (roomno, hotelno, type, price)

• Booking (hotelno, guestno, datefrom, dateto, roomno)

• Guest (guestno, guestname, guestaddress)

Page 34: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

Example

• List the number of room in each hotel in London.

• SELECT r.hotelno, COUNT(roomno)• FROM room r, hotel h• WHERE r.hotelno=h.hotelno AND• city = ‘London'• GROUP BY hotelno;

Page 35: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

Union Compatible Operations

• UNION

• MINUS or EXCEPT

• INTERSECT

• Union compatible operator [ALL] [CORRESPONGIND][BY column,..] (ALL includes duplicated rows in the result)

• Used between SELECT commands

Page 36: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

Examples

• SELECT stuid, stunameFROM sacstudent

UNIONSELECT stuid, stuname

FROM chicostudent;• SELECT *• FROM sacstudentUNION CORRESPONGIND BY stuid, stuname

SELECT *FROM chicostudent;

Page 37: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

• SELECT stuid, stunameFROM sacstudent

EXCEPT

SELECT stuid, stunameFROM chicostudent;

• (SELECT stuid, stunameFROM sacstudent)

INTERSECT

(SELECT stuid, stunameFROM chicostudent)ORDER BY 2;

Page 38: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

Column Alias

• SELECT prodid, prodname,(salesprice - goodofcost) profit

FROM productORDER BY prodid;

• SELECT prodid, prodname,(salesprice - goodofcost) AS

profit FROM productORDER BY prodid;

Page 39: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

SUBQUERY

• SELECT stuid, stuname, credit

• FROM student

• WHERE credit > (SELECT AVG(credit)

• FROM student);

Page 40: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

• SELECT stuid, stuname, major

• FROM student

• WHERE stuid IN (SELECT stuid

• FROM enrollment);

Page 41: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

• SELECT stuid, stuname, major

• FROM student

• WHERE stuid NOT IN (SELECT stuid

• FROM enrollment)

Page 42: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

Example

• What is the most commonly booked room type for all hotels in London?

• Hotel (hotelno, hotelname, city)• Room (roomno, hotelno, type, price)• Booking (hotelno, guestno, datefrom,

dateto, roomno)• Guest (guestno, guestname, guestaddress)

Page 43: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

• What is the most commonly booked room type for all hotels in London?

•  • SELECT type, MAX(y)• FROM• (SELECT type, COUNT(type) AS y • FROM booking b, hotel h, room r• WHERE r.roomno = b.roomno AND r.hotelno = b.hotelno AND• b.hotelno = h.hotelno AND • city = 'London' • GROUP BY type)• GROUP BY type;

Page 44: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

EXIST

• SELECT s.stuname, majorFROM student s

WHERE EXIST(SELECT *

FROM enrollment e WHERE

s.stuid = e.stuid);

Page 45: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

NOT EXIST

• SELECT s.stuname, majorFROM student s

WHERE NOT EXIST(SELECT *

FROM enrollment e WHERE

s.stuid = e.stuid);

Page 46: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

SOME

• SELECT stuid, stuname, major, credit

• FROM student

• WHERE credit > SOME (SELECT credit

• FROM student

• WHERE major=‘mis’);

Page 47: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

ANY

• SELECT stuid, stuname, major, credit

• FROM student

• WHERE credit > ANY (SELECT credit

• FROM student

• WHERE major=‘mis’);

Page 48: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

ALL

• SELECT stuid, stuname, major, credit

• FROM student

• WHERE credit > ALL (SELECT credit

• FROM student

• WHERE major=‘mis’);

Page 49: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

• What is the lost income from unoccupied rooms at the Grosvenor Hotel today?

• Hotel (hotelno, hotelname, city)• Room (roomno, hotelno, type, price)• Booking (hotelno, guestno, datefrom, dateto, roomno)

• Guest (guestno, guestname, guestaddress)

Page 50: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

• What is the lost income from unoccupied rooms at the Grosvenor Hotel today?

• SELECT SUM(price)• FROM room r, hotel h• WHERE r.hotelno = h.hotelno AND• h.hotelname = 'Grosvenor’ AND• r.roomno NOT IN• (SELECT roomno FROM booking b, hotel h• WHERE b.hotelno = h.hotelno AND• (datefrom <= ‘SYSTEM DATE’• AND dateto >= ‘SYSTEM DATE’) AND• h.hotelname = 'Grosvenor');

Page 51: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

• What is the lost income from unoccupied rooms at each hotel today?

• Hotel (hotelno, hotelname, city)• Room (roomno, hotelno, type, price)• Booking (hotelno, guestno, datefrom,

dateto, roomno)• Guest (guestno, guestname, guestaddress)

Page 52: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

• What is the lost income from unoccupied rooms at each hotel today?

• SELECT h.hotelno, SUM(price)• FROM room r• WHERE roomno NOT EXIST• (SELECT *• FROM booking b, hotel h, room r• WHERE b.hotelno = h.hotelno AND• r.roomno = b.roomno AND• r.hotelno = b.hotelno AND• datefrom <= ‘SYSTEM DATE’ AND• dateto >= ‘SYSTEM DATE’ )• GROUP BY hotelno;

Page 53: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

SQL DML - UPDATE, INSERT, DELETE

• INSERT INTO table-name [(colm [, colm])] VALUES (const [, const] )

• UPDATE table-name SET colm = expr [colm = expr] ... [WHERE condition]

• DELETE FROM table-name [WHERE condition]

Page 54: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

Examples

• INSERT INTO student

(stuid, stuname, major, credit)

VALUES (‘S114’, ‘Grace’, ‘MIS’, 60);

• UPDATE student

SET major = ‘Database’, credit = 100

WHERE stuid = ‘S114’;

• UPDATE student

SET major = ‘MIS’;

Page 55: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

• DELETE FROM student

WHERE stuid = ‘S114’;

• DELETE FROM student;

Page 56: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

Points To Remember

• Data Manipulation Language (DML)– SELECT– Union compatible operations– Update database

Page 57: Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database

Assignment

• Review chapter 1 – 5, appendix C

• Read chapter 6

• Homework Assignment– 5.7 – 5.28 (not 5.18)– Due date: