Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL: 1 SQL Correspondence with Relational Algebra select A from r where B = 1 Assume r(AB) and s(BC). select B from r except select B from s select A as.

Similar presentations


Presentation on theme: "SQL: 1 SQL Correspondence with Relational Algebra select A from r where B = 1 Assume r(AB) and s(BC). select B from r except select B from s select A as."— Presentation transcript:

1 SQL: 1 SQL Correspondence with Relational Algebra select A from r where B = 1 Assume r(AB) and s(BC). select B from r except select B from s select A as “D” from r select A, r.B, C from r, s where r.B = s.B  A  B = 1 r  B r -  B s  D  A  D r  A, r.B, C  r.B = s.B (r  s)

2 SQL: 2 SQL: Basic Retrieval Get full details of rooms. select RoomNr, Name, NrBeds, Cost from Room select * from Room Get costs. select Cost from Room select distinct Cost from Room Cost -------- 90 80 60 50 Cost -------- 50 60 80 90

3 SQL: 3 SQL: Aggregate Operations Get average cost. select avg(Cost) from Room select avg(Cost) as “AVE$” from Room AVE$ ------- 72.0 avg(Cost) ------------- 72.0 Also: min, max, sum, count. select avg(distinct Cost) from Room avg(distinct Cost) ------------------------ 70.0

4 SQL: 4 SQL: Sorting Get distinct costs in descending order. select distinct Cost from Room order by Cost desc Get cost and number of beds, sorted with beds in ascending order and cost in descending order. select distinct NrBeds, Cost from Room order by NrBeds asc, Cost desc Cost -------- 90 80 60 50 NrBeds Cost ------------------- 1 60 1 50 2 90 2 80

5 SQL: 5 SQL: Grouping Get the average cost of rooms with the same number of beds. select NrBeds, avg(Cost) from Room group by NrBeds Get guest numbers of guests who have reservations for a total of 5 or more days. select GuestNr from Reservation group by GuestNr having sum(NrDays) >= 5 NrBeds Avg(Cost) -------------------------- 1 55.00 2 83.33 GuestNr ------------ 101 102 104 105

6 SQL: 6 SQL: Nested Queries Get Names and Addresses of guests who have reservations for a total of 5 or more days. select Name, StreetNr, City from Guest where GuestNr in ( select GuestNr from Reservation group by GuestNr having sum(NrDays) >= 5) Name SreetNr City ------------------------------------- Smith 12 Maple Boston Carter 10 Main Hartford Smith 4 Oak Providence Green 10 Main Boston

7 SQL: 7 SQL: Multiple-Relation Retrieval Get names of guests whose names match room names. select Guest.Name from Guest, Room where Guest.Name = Room.Name Get names of each guest who has a reservation for a room that has the same name as the guest’s name. select g.Name from Guest g, Room r, Reservation s where g.Name = r.Name and g.GuestNr = s.GuestNr and s.RoomNr = r.RoomNr Name --------- Carter Green Name --------- Carter

8 SQL: 8 SQL: Joining a Relation with Itself Get pairs of guest numbers of guests arriving on the same date. (The pairs should be unique.) select s.GuestNr, t.GuestNr from Reservation s, Reservation t where s.ArrivalDate = t.ArrivalDate and s.GuestNr < t.GuestNr GuestNr ------------------------- 101 102 101 104 102 104 101 105

9 SQL: 9 SQL: Value Negation Get names and addresses of guests arriving on 15 May. select distinct Name, StreetNr, City from Guest g, Reservation s where g.GuestNr = s.GuestNr and ArrivalDate != “15 May” … arriving on a date other than 15 May. select distinct Name, StreetNr, City from Guest g, Reservation s where g.GuestNr = s.GuestNr and ArrivalDate = “15 May” Name StreetNr City ------------------------------------- Smith 12 Maple Boston Green 10 Main Boston Name StreetNR City ------------------------------------- Smith 12 Maple Boston Carter 10 Main Hartford Jones 6 Elm Hartford Smith 4 Oak Providence Johnson 15 Main Boston

10 SQL: 10 SQL: Except and Not In Get names and addresses of guests not arriving on 15 May. select Name, StreetNr, City from Guest except select Name, StreetNr, City from Guest g, Reservation s where g.GuestNr = s.GuestNr and ArrivalDate = “15 May” select Name, StreetNr, City from Guest where GuestNr not in ( select GuestNr from Reservation where ArrivalDate = “15 May”) Name StreetNr City ------------------------------------- Carter 10 Main Hartford Jones 6 Elm Hartford Smith 4 Oak Providence Johnson 15 Main Boston

11 SQL: 11 SQL: Nesting Expressions / Division  Name (g |  | (  GuestNr s   GuestNr ((  GuestNr s |  |  RoomNr  NrBeds = 2 r)   GuestNr,RoomNr s))) select Name from Guest g natural join ( select distinct GuestNr from Reservation s except select GuestNr from ( select distinct * from (select distinct GuestNr from Reservation s) natural join (select RoomNr from Room r where NrBeds=2) except select GuestNr, RoomNr from Reservation s )

12 SQL: 12 SQL: “like” and Wild-Card Characters Get reservation information for reservations with arrival dates ending in zero. select * from Reservation where ArrivalDate like '_0%' GuestNr RoomNr ArrivalDate NrDays --------------------------------------------------------- 101 1 10 May 2 101 2 20 May 1 102 3 10 May 5 104 4 10 May 2 The wild-card character “_” stands for a single character, and “%” stands for zero or more characters.


Download ppt "SQL: 1 SQL Correspondence with Relational Algebra select A from r where B = 1 Assume r(AB) and s(BC). select B from r except select B from s select A as."

Similar presentations


Ads by Google