Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

IFB105 Database Management Queensland University of Technology

Week 8 tutorial solution Brisbane Australia

Task 2: SELECT Statement

QUERY RESULT-SET

SELECT (1 + 1 + 1); 3

SELECT 9 * 10 AS NINETY; 90

SELECT NOW() AS CURRENTDATEANDTIME; 2017-08-15 18:05:35

Task 3: Understanding the Database

brisbaneBikeRacks ferryTerminals loganTrafficCounters

What is the primary key in


RackId TerminalId CounterId
this table?
How many attributes are
8 17 9
in this table?
List the different attribute
Int(11), Text, Double Int(11), Text, Tinyint(4) Int(11), Text, Double
type’s in the table

Task 4: Selecting Data from the Database

Now, instead of querying the database for all the columns in the table, try writing a query that will retrieve the
address, location and capacity of the bicycle racks. A partial of the query has been supplied below.

SELECT address, location, capacity FROM brisbaneBikeRacks;

Now, write a query that will select the description and service type for all ferry terminals. You will need to select
your data from a different table. Type your query below once you have tested it.

Query: SELECT description, serviceType FROM ferryTerminals;

1
Task 5: Selecting Distinct Data
Using the DISTINCT clause will indicate to MySQL to only return unique values in a column. For example, the
following query will return a list of suburbs that the traffic counters were used in. Execute the query answer the
questions below.

SELECT DISTINCT suburb FROM loganTrafficCounters;

How many suburbs are listed? 70


How long did the query take to execute (duration)? 0.015 sec – obviously this differs
Modify the query to list distinct street names. How many are 402
listed?

Task 6: Filtering Data


Modify the query and answer the following questions.

Write a query to find pontoons with a length of 65 metres. List the description, suburb, serviceType and
pontoonLength

SELECT Description, Suburb, ServiceType, PontoonLength


FROM ferryTerminals
WHERE PontoonLength = 65;

Write a query to generate a unique list of suburbs with pontoon lengths greater than 20 metres.

SELECT DISTINCT suburb


FROM ferryTerminals
WHERE PontoonLength > 20;

Task 7: Filtering Data


Execute the following query and describe the result set.

SELECT description, serviceType, seatPresent, boardingGates


FROM ferryTerminals
WHERE seatPresent = 1 AND (boardingGates > 2 OR boardingGates = 1);

This query lists the description, service type, whether a seat is present and the number of boarding gates IF there
is a seat present AND there are more than two boarding gates OR if there is a seat present AND there is one
boarding gate.

2
Write a query to answer the following question.
Produce a list containing the address, location and capacity of Brisbane bike racks where the capacity of the
rack is either 4, 6, 10 or 20. You must only use logical operators (and, or).

SELECT address, location, capacity


FROM brisbaneBikeRacks
WHERE capacity = 4 OR capacity = 6 OR capacity = 10 OR capacity = 20;

Task 8: IN, BETWEEN and LIKE


Execute the following query and describe the result set.

SELECT address, capacity, rackType


FROM brisbaneBikeRacks
WHERE rackType IN ('Wheel rack', 'Toaster rack');

This query lists the address, capacity and type of bicycle rack, only if the bicycle rack is a wheel rack or toaster
rack.

Write a query to answer the following question.


Produce a list containing the address, location and capacity of Brisbane bike racks where the capacity of the
rack is either 4, 6, 10 or 20. You must use the IN clause in your query.

SELECT address, location, capacity


FROM brisbaneBikeRacks
WHERE capacity IN (4, 6, 10, 20);

Execute the following query and describe the result set.

SELECT description, suburb, pontoonLength


FROM ferryTerminals
WHERE pontoonLength BETWEEN 10 AND 20;

This query lists the description, suburb and length of a pontoon if the pontoon is between 10 and 20 metres
long.

Write a query to answer the following question.


Produce a list containing the streetName, averageSpeed and speedLimit of Logan traffic counters where the
averageSpeed is between 40 and 60. Also, limit your query to only list traffic counters on Loganlea Rd. You must
use the BETWEEN clause in your query.

SELECT streetName, averageSpeed, speedLimit


FROM loganTrafficCounters
WHERE averageSpeed BETWEEEN 40 AND 60
AND streetName = ‘Loganlea Rd’;

3
Write a query to answer the following question.
Produce a list of traffic counters that were placed in a suburb containing ‘oo’. List the suburb, counterLocation
and speedLimit.
SELECT suburb,counterLocation, speedLimit
FROM loganTrafficCounters
WHERE suburb LIKE ‘%oo%’;

Task 9: Order By
Execute the following query and describe the result set.

SELECT counterLocation, suburb, speedLimit


FROM loganTrafficCounters
ORDER BY speedLimit ASC;

This query lists the counter location, suburb and the roads’ speed limit for automatic traffic counters, ordered by
speed limit in ascending order.

Execute the following query and describe the result set.

SELECT counterLocation, speedLimit, averageSpeed


FROM loganTrafficCounters
WHERE speedLimit BETWEEN 50 and 60
ORDER BY speedLimit ASC, averageSpeed ASC;

This query lists the counterLocation, speedLimit and averageSpeed of traffic counters that were placed on roads
with a speed limit between 50 and 60. The list is sorted by speed limit ascending, then by averageSpeed
ascending.

Write a query to answer the following question.


Produce a list of traffic counters with the volumeUp in ascending order and volumeDown in descending order.
List the streetName, suburb, volumeUp and volumeDown.

SELECT streetName, suburb, volumeUp, volumeDown


FROM loganTrafficCounters
ORDER BY volumeUp ASC, volumeDown DESC;

4
Task 10: Bringing it all together (challenge!)
Write a query to answer the following question.

List the streetName, suburb, averageSpeed and speedLimit for traffic counters that recorded an average speed
that is 10km/h greater than the speedLimit. Hint: you can perform mathematical operations as a condition
(example: WHERE (a - b) > 5).

SELECT streetName, suburb, averageSpeed, speedLimit


FROM loganTrafficCounters
WHERE (speedLimit – averageSpeed) > 10;

Write a query to answer the following question.


Produce a list of ferry terminals that have a seat and also have CCTV security installed. Order the list by suburb in
alphabetical order and ensure the pontoon length is greater than or equal to 25 metres.
List description, pontoonLength, seatPresent and CCTV.

SELECT description, pontoonLength, seatPresent, CCTV


FROM ferryTerminals
WHERE seatPresent = TRUE AND CCTV = TRUE AND pontoonLength >= 25
ORDER BY suburb ASC;

Write a query to answer the following question.


Produce a list of traffic counters in Loganlea, Daisy Hill and Regents Park that have an upstream volume of 200-
2000. Order the list of suburbs, counterLocation’s and volumeUp’s by volumeUp in ascending order.

SELECT Suburb, CounterLocation, VolumeUp


FROM logantrafficCounters
WHERE suburb IN (‘Daisy Hill’, ‘Loganlea’, ‘Regents Park’)
AND VolumeUp BETWEEN 200 AND 2000
ORDER BY VolumeUp;

You might also like