lab6db
lab6db
References
Lecture Notes: Topic 6
Elmasri and Navathe, 2017: Chapter 6
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.
PROMOTERBUSINESSNAME PROMOTERPHONENO
-------------------------------------------------- ---------------
CONTACTPERSON
--------------------------------------------------
Walter's Meats 9333 3331
Walter Dreyer
1
Clara's Cafe 9333 3221
Clara Thompson
5. a. Write an SQL query to select client companies in the postcode area 1001.
SELECT *
FROM Clients
WHERE postcode = '1001';
6. Write an SQL query to select client companies who are not in the postcode area 1001.
SELECT *
FROM Clients
WHERE postcode <> '1001';
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.
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.
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.
VENUENAME
--------------------------------
EVENTNAME
--------------------------------
Local Town Community Theatre
Bopping Ballet
10. Write an SQL query to show the dates in July 2018 that the Local Town Community Theatre
is already booked.
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);
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
14. Write an SQL query that will determine which is the least expensive venue that will
accommodate 120 people.
VENUENAME COSTPERDAY
-------------------------------------------------- ----------
St Patricks Church Hall 70
15. Write an SQL query that calculates how many events are being held in July 2018.
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:
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…
COUNT (x) This function returns number of rows returned by a query involving x
For more practice try the following from your text book (Elmasri and Navathe, 2014):