0% found this document useful (0 votes)
9 views

lab6db

Uploaded by

fakeid20678
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

lab6db

Uploaded by

fakeid20678
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Laboratory 6

SQL Queries – Part 1

References
Lecture Notes: Topic 6
Elmasri and Navathe, 2017: Chapter 6

Exercise 1 – Running a Script

Instructions for running a script have been given in the main lab sheet and does not require a
solution.

Exercise 2 – SQL

1. Write the SQL query to list all the venues stored in the database.

SELECT *
FROM Venues;

10 rows selected.

2. Write an SQL query to list all the data in the events table.

SELECT *
FROM Events;

24 rows selected.

3. Write an SQL query to list all the clients stored in the database. Make sure the data presented
is ordered by company name.

SELECT *
FROM Clients
ORDER BY clientCompanyName;

15 rows selected.

4. a. Write an SQL query to list the promoter business names, their phone numbers and contact
person.

SELECT promoterBusinessName, promoterPhoneNo, contactPerson


FROM Promoters;

PROMOTERBUSINESSNAME PROMOTERPHONENO
-------------------------------------------------- ---------------
CONTACTPERSON
--------------------------------------------------
Walter's Meats 9333 3331
Walter Dreyer

1
Clara's Cafe 9333 3221
Clara Thompson

Tandoori Temptations 9333 3111


Gaurav Singh

Vietnamese Cuisine 9333 3001


Anna Nguyen

Coffee by the Lake 9335 2221


Maree Cotti

b. Write the Relational Algebra expression for this query.

π promoterBusinessName, PromoterPhoneNo, contactPerson (Promoters)

5. a. Write an SQL query to select client companies in the postcode area 1001.

SELECT *
FROM Clients
WHERE postcode = '1001';

Six companies – 1, 2, 3, 11, 12, 14

b. Write the Relational Algebra expression for this query.

σ (postcode = ‘1001’) (Clients)

6. Write an SQL query to select client companies who are not in the postcode area 1001.

SELECT *
FROM Clients
WHERE postcode <> '1001';

Nine companies – 4, 5, 6, 7, 8, 9, 10, 13, 15

7. Write an SQL query to list the names of all the client companies and the events that have
been run for them or are currently booked.

SELECT DISTINCT C.clientCompanyName, E.eventName


FROM Clients C, Events E
WHERE C.clientID = E.clientID;

24 rows selected

8. Write an SQL query to list the names of all the venues that have been booked, or are currently
booked and the names of the associated events.

SELECT DISTINCT V.venueName, E.eventName

2
FROM Venues V, EventVenues EV, Events E
WHERE V.venueID = EV.venueID AND EV.eventID = E.eventID;

24 rows selected

9. Write an SQL query to list all the venues and the events they will be hired for in June 2018.

SELECT DISTINCT V.venueName, E.eventName


FROM Venues V, EventVenues EV, Events E
WHERE V.venueID = EV.venueID(+) AND
EV.eventID = E.eventID(+) AND
TO_CHAR(bookingDate, 'MON-YYYY') = 'JUN-2018';

VENUENAME
--------------------------------
EVENTNAME
--------------------------------
Local Town Community Theatre
Bopping Ballet

Davis Street Community Centre


Tappin Tots

Local Town Community Theatre


Mimed Moments

10. Write an SQL query to show the dates in July 2018 that the Local Town Community Theatre
is already booked.

SELECT TO_CHAR(ev.bookingDate, 'DD-MON-YYYY')


FROM Venues V, EventVenues EV
WHERE V.venueID = 'V00003' AND V.venueID = EV.venueID AND
TO_CHAR(EV.bookingDate, 'MON-YYYY') = 'JUL-2018';

TO_CHAR(BOO
-----------
18-JUL-2018
19-JUL-2018
25-JUL-2018
26-JUL-2018

11. Write an SQL query to list those venues that have never been booked.

SELECT V.venueName
FROM Venues V
WHERE V.venueID NOT IN (SELECT venueID
FROM EventVenues);

Market Hill Scout Hall

12. a. Write an SQL query to list all the events that have been sponsored by Gordon's
Greengrocers.

SELECT EV.eventName

3
FROM Events EV, EventSponsors ES, Sponsors S
WHERE S.businessName = 'Gordon''s Greengrocers' AND
S.businessID = ES.businessID AND
ES.eventID = EV.EventID;

Fame

b. Write the Relational Algebra expression for this query.

Temp1 σ(businessName = ‘Gordon’s Greengrocers’) (Sponsors)


Temp2 Temp1 businessID = businessID EventSponsors

Temp3 Temp2 eventID = eventID Events

Result π eventName (Temp3)


13. Write an SQL query that will calculate the average cost per day of hiring a venue for 100 –
120 people (inclusive).

SELECT AVG(costPerDay) AS "Average Cost per Day"


FROM Venues
WHERE venueCapacity BETWEEN 100 AND 120;

Average Cost per Day


--------------------
72.5

14. Write an SQL query that will determine which is the least expensive venue that will
accommodate 120 people.

SELECT V.venueName, V.costPerDay


FROM Venues V
WHERE V.costPerDay = (SELECT MIN(costPerDay)
FROM Venues
WHERE venueCapacity >= 120);

VENUENAME COSTPERDAY
-------------------------------------------------- ----------
St Patricks Church Hall 70

15. Write an SQL query that calculates how many events are being held in July 2018.

SELECT COUNT(DISTINCT eventID) AS "Number of events in July 2018"


FROM EventVenues
WHERE TO_CHAR(bookingDate, 'MON-YYYY') = 'JUL-2018';

Number of events in July 2018


-----------------------------
2

4
A word on dates…

The DATE data type stores two pieces of information, the first value is the year, month and day
and the second value is the time. Therefore you need to take care when comparing dates as two
dates that are the same (year, month and day) may have different times associated with them.
Oracle allows you to format the dates you are comparing to ensure you are only looking at the
desired fields. For example:

To see if any events are being held on May 3rd 2018:

SELECT E.eventName
FROM Event E, EventVenue EV
WHERE E.eventID = EV.eventID and TO_CHAR(EV.bookingDate, 'DD-MON-YYYY) = '03-
MAY-2018’';

The TO_CHAR function takes a date as its argument and returns the date as a character string in
the format given, in this example 'DD-MON-YYYY'. There are many different ways that the date
and time can be formatted. For a comprehensive listing refer to the Oracle documentation.

TO_CHAR can also be used in the SELECT clause to format the date for display.

You can also perform date arithmetic and use SYSDATE to find out the current date and time.

Aggregate Functions…

The following aggregate functions are quite useful in this lab.

You can also refer to your lecture notes for examples.

AVG (x) This function returns the average of x

COUNT (x) This function returns number of rows returned by a query involving x

MAX (x) This function returns the maximum value of x

MIN (x) This function returns the minimum value of x

For more practice try the following from your text book (Elmasri and Navathe, 2014):

Q4.5, Q4.6, Q4.7 (pg 107)

You might also like