Chapter 5 SQL Homework.

Slides:



Advertisements
Similar presentations
CS 3630 Database Design and Implementation. Where Clause and Aggregate Functions -- List all rooms whose price is greater than the -- average room price.
Advertisements

© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Chapter 4 Hotel (hotelno, hotelname, city)
Oracle 10g Express. Download Oracle 10g Express Oracle 10g Express Edition: – edition/overview/index.htmlhttp://
1 Assignment 2 Relational Algebra Which tables? What operations? Common attributes? What result (attributes)? Syntax (Standard Notations and Symbols) –Product:
COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data.
CS 3630 Database Design and Implementation. SQL Query Clause Select and From Select * From booking; select hotel_no, guest_no, room_no from booking; select.
1 Database Systems Relations as Bags Grouping and Aggregation Database Modification.
Information Resources Management February 27, 2001.
Structured Query Language – Continued Rose-Hulman Institute of Technology Curt Clifton.
Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database.
VIEWS Pertemuan 7 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Chapter 6 SQL Homework.
Using SQL to create tables Ways of using Databases.
Create, Insert, Delete, Update. Create Create database Create table Create index – Primary – Secondary.
Project Phase I Phase II Due Monday, April 15 Groups 1.
Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database.
T-Stats Online information system Immediate results Useful for both you and Cotswolds.
1 Section 5 - Grouping Data u The GROUP BY clause allows the grouping of data u Aggregate functions are most often used with the GROUP BY clause u GROUP.
Chapter 5 SQL. Agenda Data Manipulation Language (DML) –SELECT –Union compatible operations –Update database.
Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform.
Using Special Operators (LIKE and IN)
Getting to Know SQL. © Jim Hope 2002 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement TRANSFORM.
SQL queries ordering and grouping and joins
SQL - DML. Data Manipulation Language(DML) Are used for managing data: –SELECT retrieve data from the a database –INSERT insert data into a table –UPDATE.
Oracle Command Spool Spool C:\temp\Lab9.lst Select Hotel_no, room_no, type, price From Room Order by Hotel_no; Spool Off.
Chapter 5 SQL: Data Manipulation Thomas Connolly, Carolyn Begg, Database System, A Practical Approach to Design Implementation and Management, 4 th Edition,
Subqueries Steve Perry 1.
SQL introduction 2013.
Starter A delegate comes to you (the receptions) and tell you they have lost their timetable (itinerary) What details would you ask them for to check the.
CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,
Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.
Chapter 7, Section 2. Revenue Management Increase Revenue by: Managing the number of rooms filed Managing the number of discounts offered Booking guests.
INF 280 Database Systems SQL:Join
Populating and Querying tables Insert, Update, Delete and View (DML)
WEEK# 12 Haifa Abulaiha November 02,
IST 210 More SQL Todd Bacastow IST 210: Organization of Data.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
CS 3630 Database Design and Implementation. Base Table and View Base Table Stored on disk View Virtual table Records are not stored on disk Query is stored.
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.
CS 3630 Database Design and Implementation. Where Clause and Aggregate Functions -- List all rooms whose price is greater than the -- average room price.
SQL: Data Manipulation. Objectives Describe the purpose and importance of SQL. Demonstrate how to retrieve data from database using SELECT and: ▫Use compound.
SQL. SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a.
CS 3630 Database Design and Implementation. Joins -- For each booking, display the booking -- details with the room type and price Select B.*, rtype,
CS 3630 Database Design and Implementation. Joins Retrieve data from two or more tables Join Conditions PK and FK (Natural Join) Other attributes (Theta.
Database Design lecture 3_2 Slide 1 Database Design Lecture 3_2 Data Manipulation in SQL Simple SQL queries References: Text Chapter 8 Oracle SQL Manual.
SQL: Single Table Queries GROUP BY HAVING D. Christozov / G.Tuparov INF 280 Database Systems: SQL - Single Table queries 1.
Trip to Orlando, Florida Destination: Disney World.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
1 Introduction to Database Systems, CS420 SQL JOIN, Aggregate, Grouping, HAVING and DML Clauses.
Structured Query Language used for defining and manipulating data in Relational DBs aimed at: –reducing training costs –increasing productivity –improve.
CS 3630 Database Design and Implementation
Assignment 2 Relational Algebra Which tables? What operations?
CS3220 Web and Internet Programming More SQL
CS 3630 Database Design and Implementation
CS 3630 Database Design and Implementation
Outerjoins, Grouping/Aggregation Insert/Delete/Update
References: Text Chapters 8 and 9
CS 3630 Database Design and Implementation
Thomas M. Connolly Carolyn E. Begg
Latihan Answer the following questions using the relational schema from the Exercises at the end of Chapter 3: Create the Hotel table using the integrity.
Assignment 2.
SQL Pertemuan 6 9/17/2018 Sistem Basis Data.
CS 3630 Database Design and Implementation
CS 3630 Database Design and Implementation
Chapter 4 Summary Query.
CS 3630 Database Design and Implementation
Higher SDD SQL Practical Tasks.
Query Functions.
Section 4 - Sorting/Functions
Presentation transcript:

Chapter 5 SQL Homework

Hotel (hotelno, hotelname, city) Room (roomno, hotelno, type, price) Booking (hotelno, guestno, datefrom, dateto, roomno) Guest (guestno, guestname, guestaddress)

Simple Queries 7. List full details of all hotels. SELECT * FROM hotel;

8. List all details of all hotels in London. SELECT * FROM hotel WHERE city = 'London';

9. List the names and addresses of all guests in London, alphabetically ordered by name. SELECT guestname, guestaddress FROM guest WHERE guestaddress like ‘London’ ORDER BY guestname;

10. List all double or family rooms with a price below $40 10. List all double or family rooms with a price below $40. 00 per night, in ascending order of price. SELECT * FROM room WHERE price < 40 AND type IN ('Double', 'Family')   ORDER BY price;  

11. List the bookings for which no dateto has been specified. SELECT * FROM booking WHERE dateto IS NULL;

Aggregate Functions 12. How many hotels are there? SELECT COUNT(*)   SELECT COUNT(*) FROM hotel; SELECT COUNT(hotelno)

13. What is the average price of a room?   SELECT AVG(price) FROM room;

14. What is the total revenue per night from all double rooms? SELECT SUM(price) FROM room WHERE type = 'Double' ;

15. How many different guests have made bookings for August?   SELECT COUNT(DISTINCT guestno) FROM booking WHERE (datefrom <= ‘8/31/06’ AND dateto >= ‘8/1/06’);

Subqueries and Joins 16. List the price and type of all rooms at the Grosvenor Hotel. SELECT price, type FROM room WHERE hotelno = (SELECT hotelno FROM hotel WHERE hotelname = 'Grosvenor');

Another Method 16. List the price and type of all rooms at the Grosvenor Hotel. SELECT price, type FROM room, hotel WHERE hotel.hotelno = room.hotelno AND hotelname = 'Grosvenor';

17. List all guests currently staying at the Grosvenor Hotel. SELECT (guestno, guestname, guestaddress) FROM guest, booking, hotel WHERE guest.guestno =booking.guestno AND hotel.hotelno = booking.hotelno AND (datefrom <= ‘SYSTEM DATE’ AND dateto >= ‘SYSTEM DATE’) AND hotelname = ‘Grosvenor’;

17. List all guests currently staying at the Grosvenor Hotel 17. List all guests currently staying at the Grosvenor Hotel. (another method) SELECT * FROM guest WHERE guestno IN (SELECT guestno FROM booking WHERE datefrom <= ‘SYSTEM DATE’ AND dateto >= ‘SYSTEM DATE’ AND hotelno = (SELECT hotelno FROM hotel WHERE hotelname = ‘Grosvenor’));

18. List the details of all rooms at the Grosvenor Hotel, including the name of the guest staying in the room, if the room is occupied.

Create a view with every room having a guest CREATE VIEW roomocp (hotelno, roomno, type, price, guestname) AS SELECT r.hotelno, r.roomno, r.type, r.price, g.guestname FROM hotel h, room r, booking b, guest g WHERE h.name = ‘Grosvenor’ AND (b.datefrom <= ‘SYSTEM DATE’ AND b.dateto >= ‘SYSTEM DATE’) AND h.hotelno = r.hotelno AND r.hotelno = b.hotelno AND r.roomno = b.roomno AND b.guestno = g.guestno;

Create a view of every room CREATE VIEW roomall (hotelno, roomno, type, price) AS SELECT r.hotelno, r.roomno, r.type, r.price FROM hotel h, room r WHERE h.hotelname ='Grosvenor’ AND h.hotelno = r.hotelno;

Find the answer SELECT r.roomno, r.hotelno, r.type, r.price, p.guestname FROM roomall r LEFT JOIN roomocp p ON r.roomno = p.roomno;

19. What is the total income from bookings for the Grosvenor Hotel today ? SELECT SUM(price) FROM booking b, room r, hotel h WHERE (b.datefrom <= ‘SYSTEM DATE’ AND b.dateto >= ‘SYSTEM DATE’) AND r.hotelno = h.hotelno AND r.hotelno = b.hotelno AND r.roomno = b.roomno AND h.hotelname = ‘Grosvenor’;

20. List the rooms which are currently unoccupied at the Grosvenor Hotel. SELECT (r.hotelno, r.roomno, r.type, r.price) FROM room r, hotel h WHERE r.hotelno = h.hotelno AND h.hotelname = 'Grosvenor’ AND roomno NOT IN (SELECT roomno FROM booking b, hotel h WHERE (datefrom <= ‘SYSTEM DATE’ AND dateto >= ‘SYSTEM DATE’) AND b.hotelno=h.hotelno AND hotelname = 'Grosvenor');

20. List the rooms which are currently unoccupied at the Grosvenor Hotel. SELECT (r.hotelno, r.roomno, r.type, r.price) FROM room r, hotel h WHERE r.hotelno = h.hotelno AND h.hotelname = 'Grosvenor’ AND NOT EXIST (SELECT * FROM booking b, hotel h WHERE (datefrom <= ‘SYSTEM DATE’ AND dateto >= ‘SYSTEM DATE’) AND r.hotelno=b.hotelno AND r.roomno=b.roomno AND r.hotelno=h.hotelno AND hotelname = 'Grosvenor');

21. What is the lost income from unoccupied rooms at the Grosvenor Hotel? SELECT SUM(price) FROM room r, hotel h WHERE r.hotelno = h.hotelno AND h.hotelname = 'Grosvenor’ AND roomno NOT IN (SELECT roomno FROM booking b, hotel h WHERE (datefrom <= ‘SYSTEM DATE’ AND dateto >= ‘SYSTEM DATE’) AND b.hotelno = h.hotelno AND r.hotelno=b.hotelno AND r.roomno=b.roomno AND h.hotelname = 'Grosvenor');

Grouping 22. List the number of rooms in each hotel. SELECT hotelno, COUNT(roomno) FROM room GROUP BY hotelno;

23. 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 r.hotelno;

24. What is the average number of bookings for each hotel in August? SELECT hotelno, y/31 FROM (SELECT hotelno, COUNT(hotelno) AS y FROM booking WHERE (datefrom <= ‘8/31/06’ AND dateto >= ‘8/1/06’ GROUP BY hotelno);

25. 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;

25. What is the most commonly booked room type for each hotel in London?   SELECT hotelno, type, MAX(y) FROM (SELECT hotelno, 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 hotelno, type) GROUP BY hotelno, type;

26. What is the lost income from unoccupied rooms at each hotel today? SELECT r.hotelno, SUM(price) FROM room r WHERE NOT EXIST (SELECT * FROM booking b WHERE r.roomno = b.roomno AND r.hotelno = b.hotelno AND (datefrom <= ‘SYSTEM DATE’ AND dateto >= ‘SYSTEM DATE’)) GROUP BY hotelno;

27. Insert rows into each of these tables. INSERT INTO hotel VALUES (‘h11’, ‘hilton’, ‘sacramento’); INSERT INTO room VALUES (‘hr1111’, ‘h11’, ‘single’, 120);

28. Update the price of all room by 5%.   UPDATE room SET price = price*1.05;