Title: Chapter 12 SQL
1Chapter 5
SQL Data Manipulation Thomas Connolly, Carolyn
Begg, Database System, A Practical Approach to
Design Implementation and Management, 4th
Edition, Addison Wesley Pg 113 156
2Learning Outcomes
- Introduction to SQL
- Writing SQL Commands
- Data Manipulation
3SQL DML - SELECT
- SELECT DISTINCTALL column expression AS
newname ,... - FROM table-name alias ,...
- WHERE condition
- GROUP BY column list
- HAVING condition
- ORDER BY column list
4Simple SELECT
- SELECT attributes (or calculations , -, /, )
- FROM relation
- SELECT DISTINCT attributes
- FROM relation
- SELECT attributes (or wild card)
- FROM relation
- WHERE condition
5Example
- SELECT stuname FROM student
- SELECT stuid, stuname, credits FROM student
- SELECT stuid, stuname, credits10 FROM
student - SELECT DISTINCT major FROM student
6- SELECT FROM student
- SELECT stuname, major, credits FROM
student WHERE stuid S114 - SELECT FROM faculty WHERE dept
MIS
7SELECT - WHERE condition
- AND OR
- NOT IN
- NOT IN BETWEEN
- IS NULL IS NOT NULL
- SOME ALL
- NOT BETWEEN
- LIKE '' multiple characters
- LIKE _ single character
- Evaluation rule left to right, brackets, NOT
before AND OR, AND before OR -
8Example
- SELECT FROM faculty WHERE 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
9- SELECT FROM class WHERE room LIKE
b_s - SELECT FROM class WHERE room NOT
LIKE bus - SELECT productid, productname FROM
inventory WHERE onhand BETWEEN 50 and 100 - SELECT customerid, discountrate
- FROM sales
- WHERE
- discountrate LIKE 20 ESCAPE
10- SELECT companyid, companyname FROM
company WHERE companyname BETWEEN G
AND K - SELECT productid, productname FROM
inventory WHERE onhand NOT BETWEEN 50
and 100 - SELECT companyid, companyname FROM
company WHERE companyname NOT BETWEEN
G AND K
11- SELECT facname FROM faculty WHERE
dept IN (MIS, ACT) - SELECT facname FROM faculty WHERE
rank NOT IN (assistant, lecture) - SELECT customername FROM customer WHERE
emailadd IS NOT NULL
12- SELECT customername FROM customer WHERE
creditlimit IS NULL
13SELECT - aggregate functions
14Example
- SELECT COUNT() FROM student
- SELECT COUNT(major) FROM student
- SELECT COUNT(DISTINCT major) FROM student
15- SELECT COUNT(stuid), SUM(credits),
AVG(credits), MAX(credits),
MIN(credits) FROM student
16- How many different guests have made bookings for
August 2006? - Hotel (hotelno, hotelname, city)
- Room (roomno, hotelno, type, price)
- Booking (hotelno, guestno, datefrom, dateto,
roomno) - Guest (guestno, guestname, guestaddress)
17- How many different guests have made bookings for
August 2006? -
- SELECT COUNT(DISTINCT guestno)
- FROM booking
- WHERE (datefrom lt 8/31/06) AND
- (dateto gt 8/1/06)
18SELECT - GROUP
19Example
- SELECT major, AVG(credits) FROM
student GROUP BY major - SELECT course, COUNT(stuid) FROM
enrollment GROUP BY course
20Example
- SELECT major, AVG(credits) FROM
student GROUP BY major HAVING COUNT()
gt 2 - SELECT course, COUNT(stuid) FROM
enrollment GROUP BY course HAVING
COUNT() gt 2
21- SELECT major, AVG(credits) FROM
student WHERE major IN (mis, act)
GROUP BY major HAVING COUNT() gt 2
22SELECT - ORDER BY
- ORDER BY
- ORDER BY ... DESC
23Example
- SELECT facname, rank FROM faculty
- ORDER BY facname
- SELECT facname, rank FROM faculty
- ORDER BY rank DESC, facname
24SELECT - JOIN Tables
- Multiple tables in FROM clause
- MUST have join conditions!!!
25Example
- List stuname and grade
- SELECT stuname, grade FROM student,
enrollment - WHERE student.stuid enrollment.stuid
26Example
- List stuname and grade
- SELECT stuname, grade FROM student s,
enrollment e - WHERE s.stuid e.stuid
27List course, stuname and major for faculty
number is equal to f114
- SELECT enrollment.course, stuname, major FROM
class, enrollment, student WHERE class.course
enrollment.course AND enrollment.stuid
student.stuid AND facid F114 ORDER
BY enrollment.course
28OUTER JOINS
- RIGHT JOIN
- LEFT JOIN
- FULL JOIN
- Appending () to the optional column (null) in
the join condition (Oracle)
29Example
- List the faculty member who does not teach any
class - SELECT f.facid, f.facname
- FROM class c, faculty f WHERE c.facid ()
f.facid - AND c.course is null
- ORDER BY f.facname (right outer join)
30List the faculty member who does not teach any
class
- SELECT f.facid, f.facname
- FROM class c RIGHT JOIN faculty f ON c.facid
f.facid - WHERE c.course is null
- ORDER BY f.facname (right outer join)
31List the student name and major who is not
enrolled in any class
- SELECT s.stuname, major FROM student s,
enrollment e WHERE s.stuid e.stuid () AND
e.stuid is null - ORDER BY s.stuname (left outer join)
32List the student name and major who is not
enrolled in any class
- SELECT s.stuname, major FROM student s LEFT
JOIN enrollment e - ON s.stuid e.stuid
- WHERE e.stuid is null
- ORDER BY s.stuname (left outer join)
33List the faculty member who does not teach any
class
- SELECT f.facid, f.facname
- FROM class c, faculty f WHERE c.facid ()
f.facid () AND c.course is null - ORDER BY f.facname (full outer join)
- Use only when the database contains proper records
34List the faculty member who does not teach any
class
- SELECT f.facid, f.facname
- FROM class c FULL JOIN faculty f ON c.facid
f.facid - WHERE c.course is null
- ORDER BY f.facname (full outer join)
35Example
- 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)
36Example
- List the number of room in each hotel in London.
- SELECT r.hotelno, COUNT(roomno)
- FROM room r, hotel h
- WHERE r.hotelnoh.hotelno AND
- city London'
- GROUP BY hotelno
37Union Compatible Operations
- UNION
- MINUS or EXCEPT
- INTERSECT
- Union compatible operator ALL
CORRESPONDINGBY column,.. (ALL includes
duplicated rows in the result) - Used between SELECT commands
38Example
- List stuid and stuname who attends SAC or CHICO
- SELECT stuid, stuname FROM sacstudent
- UNION
- SELECT stuid, stuname FROM chicostudent
- SELECT
- FROM sacstudent
- UNION CORRESPONDING BY stuid, stuname
- SELECT
- FROM chicostudent
39- List stuid and stuname who attends SAC not CHICO
- SELECT stuid, stuname FROM sacstudent
- EXCEPT
- SELECT stuid, stuname FROM chicostudent
- List stuid and stuname who attends SAC and CHICO
- (SELECT stuid, stuname FROM sacstudent)
- INTERSECT
- (SELECT stuid, stuname FROM
chicostudent) ORDER BY 2
40- A renter viewing list (Rno, Fname, Lname) of
renters who only look property_for_rent at
Sacramento - (Select
- From Viewing v, Property_for_rent p, Renter r
- Where v.Pno p.Pno and r.Rno v.Rno and
CitySacramento) - EXCEPT CORRESPONDING BY Rno, Fname, Lname
- (Select
- From Viewing v, Property_for_rent p, Renter r
- Where v.Pno p.Pno and r.Rno v.Rno and
CitySacramento)
41Column Alias
- SELECT prodid, prodname, (salesprice -
goodofcost) profit FROM product ORDER BY
prodid - SELECT prodid, prodname, (salesprice -
goodofcost) AS profit FROM product ORDER
BY prodid
42SUBQUERY
- List stuid, stuname, and credits for the student
whose credits are larger than the average student
credits - SELECT stuid, stuname, credits
- FROM student
- WHERE credits gt (SELECT AVG(credits)
- FROM student)
43List stuid, stuname, and major of those student
who is enrolled in a class
- SELECT stuid, stuname, major
- FROM student
- WHERE stuid IN (SELECT stuid
- FROM enrollment)
44List stuid, stuname, and major of those student
who is not enrolled in a class
- SELECT stuid, stuname, major
- FROM student
- WHERE stuid NOT IN
- (SELECT stuid FROM enrollment)
45Example
- 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)
46Find the type with its number of rooms of every
hotel in London
- 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
47- 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
48EXIST
- Find student name and major who is enrolled in a
class - SELECT s.stuname, major FROM student
s WHERE EXIST (SELECT FROM
enrollment e WHERE
s.stuid e.stuid)
49NOT EXIST
- Find student name and major who is not enrolled
in a class - SELECT s.stuname, major FROM student
s WHERE NOT EXIST (SELECT
FROM enrollment e WHERE
s.stuid e.stuid)
50SOME
- Find stuid, stuname, major, and credits of the
student whose credits are greater than some mis
students credits - SELECT stuid, stuname, major, credits
- FROM student
- WHERE credits gt SOME (SELECT credits
- FROM student
- WHERE majormis)
51ANY
- Find stuid, stuname, major, and credits of the
student whose credits are greater than any mis
students credits - SELECT stuid, stuname, major, credits
- FROM student
- WHERE credits gt ANY (SELECT credits
- FROM student
- WHERE majormis)
52ALL
- Find stuid, stuname, major, and credits of the
student whose credits are greater than every mis
students credits - SELECT stuid, stuname, major, credits
- FROM student
- WHERE credits gt ALL (SELECT credits
- FROM student
- WHERE majormis)
53- 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)
54Find the rooms that are occupied at the Grosvenor
Hotel today
- SELECT roomno FROM booking b, hotel h
- WHERE b.hotelno h.hotelno AND
- (datefrom lt SYSTEM DATE AND
- dateto gt SYSTEM DATE) AND
- h.hotelname 'Grosvenor'
55Find the total income of all rooms at the
Grosvenor Hotel
- SELECT SUM(price)
- FROM room r, hotel h
- WHERE r.hotelno h.hotelno AND
- h.hotelname 'Grosvenor
56- 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 lt SYSTEM DATE
- AND dateto gt SYSTEM DATE) AND
- h.hotelname 'Grosvenor')
-
57- 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)
58- What is the lost income from unoccupied rooms at
each hotel today? - SELECT h.hotelno, SUM(price)
- FROM room r
- WHERE 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 lt SYSTEM DATE AND
- dateto gt SYSTEM DATE )
- GROUP BY hotelno
59SQL 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
60Example
- INSERT INTO student
- (stuid, stuname, major, credits)
- VALUES (S114, Grace, MIS, 60)
- UPDATE student
- SET major Database, credits 100
- WHERE stuid S114
- UPDATE student
- SET major MIS
61- DELETE FROM student
- WHERE stuid S114
- DELETE FROM student
-