Lab 3: Basic SELECT Statements: 3.1 Using Arithmetic Operators in SQL Statements
Lab 3: Basic SELECT Statements: 3.1 Using Arithmetic Operators in SQL Statements
SQL commands are often used in conjunction with arithmetic operators. As you perform
suggests, the rules of precedence are the rules that establish the order in which
computations are completed. For example, note the order of the following computational
sequence:
Task 3.1 Suppose the owners of all the theme parks wanted to compare the current ticket
prices, with an increase in the price of each ticket by 10%. To generate this query type:
1
FROM TICKET;
The output for this query is shown in Figure 19. The ROUND function is used to ensure
You will see in Figure 19 that the last column is named after the arithmetic expression in
the query. To rename the column heading, a column alias needs to be used. Modify the
query as follows and note that the name of the heading has changed to
FROM TICKET;
2
Note
When dealing with column names that require spaces, the optional keyword AS can
AS “PRICE INCREASE”
FROM TICKET;
Numerous conditional restrictions can be placed on the selected table contents in the
WHERE clause of the SELECT statement. For example, the comparison operators
SYMBOL MEANING
= Equal to
3
LIKE Used to check if an attribute value matches a given string pattern.
Greater than
The following example uses the “greater than” operator to display the theme park code,
ticket price and ticket type of all tickets where the ticket price is greater than €20.00.
FROM TICKET
Task 3.2 Type in and execute the query and test out the greater than operator. Do you get
4
Task 3.3 Modify the query you have just executed to display tickets that are less than
€30.00.
Character comparisons
based attributes.
Task 3.4 Execute the following query which produces a list of all rows in which the
PARK_CODE is alphabetically less than UK2262. (Because the ASCII code value for
the letter B is greater than the value of the letter A, it follows that A is less than B.)
FROM THEMEPARK
5
BETWEEN
The operator BETWEEN may be used to check whether an attribute value is within a
range of values. For example, if you want to see a listing for all tickets whose prices are
SELECT *
FROM TICKET
Figure 22 shows the output you should see for this query.
Task 3.5 Write a query which displays the employee number, attraction no, the hours
worked per attraction and the date worked where the hours worked per attraction is
between 5 and 10. Hint you will need to select data from the HOURS table. The output
6
Figure 23: Output for Task 3.5
IN
The IN operator is used to test for values which are in a list. The following query finds
only the rows in the SALES_LINE table that match up to a specific sales transaction. i.e.
SELECT *
FROM SALES_LINE
7
Figure 24 Selecting rows using the IN command
Task 3.6 Write a query to display all tickets that are of type Senior or Child. Hint:
Use the TICKET table. The output you should see is shown in Figure 25.
LIKE
The LIKE operator is used to find patterns within string attributes. Standard SQL allows
you to use the percent sign (%) and underscore (_) wildcard characters to make matches
8
when the entire string is not known. % means any and all following characters are eligible
while _ means any one character may be substituted for the underscore.
Task 3.7 Enter the following query which finds all EMPLOYEE rows whose first
FROM EMPLOYEE
Figure 26 shows the output you should see for this query.
Task 3.8 Write a query which finds all Theme Parks that have a name ending in ‘Land’.
9
Figure 27 Solution to Task 4.8
IS NULL is used to check for a null attribute value. In the following example, the query
lists all attractions that do not have an attraction name assigned (ATTRACT_NAME is
SELECT *
FROM ATTRACTION
10
Figure 28 Listing all Attractions with no name
Logical Operators
SQL allows you to have multiple conditions in a query through the use of logical
operators: AND, OR and NOT. NOT has the highest precedence, followed by AND, and
then followed by OR. However, you are strongly recommended to use parentheses to
AND
This logical AND connective is used to set up a query where there are two conditions
which must be met for the query to return the required row(s). The following query
by the employee is greater than 3 and the date worked (DATE_WORKED) is after 18th
May 2007.
FROM HOURS
11
Figure 29 Query results using the AND operator
Task 3.9 Enter the query above and check you results with those shown in Figure 29.
Task 3.10 Write a query which displays the details of all attractions which are
suitable for children aged 10 or under and have a capacity of less than 100. You
should not display any information for attractions which currently have no name.
12
OR
If you wanted to list the names and countries of all Theme parks where of invoice
FROM THEMEPARK
OR PARK_COUNTRY = 'UK';
When using AND and OR in the same query it is advisable to use parentheses to
Task 3.11 Test the following query and check your output with that shown in Figure 32.
13
SELECT *
FROM ATTRACTION
NOT
The logical operator NOT is used to negate the result of a conditional expression. If you
want to see a listing of all rows for which EMP_NUM is not 106, the query would look
like:
SELECT *
FROM EMPLOYEE
The results of this query are shown in Figure 33. Note that the condition is enclosed in
14
Figure 33: Listing all employees except EMP_NUM=106
The ORDER BY clause is especially useful when the listing order of the query is
important. Although you have the option of declaring the order type—ascending (ASC)
or descending (DESC) —the default order is ascending. For example, if you want to
display all employees listed by EMP_HIRE_DATE in descending order you would write
SELECT *
FROM EMPLOYEE
15
Figure 34: Displaying all employees in descending order of EMP_HIRE_DATE.
The ORDER BY command can also be used to produce a cascading order sequence. This
Task 3.12 Enter the following query which contains an example of a cascading order
sequence, by ordering the rows in the employee table by the employee’s last then
first names.
SELECT *
FROM EMPLOYEE
16
It is worth noting that if the ordering column has nulls, they are listed either first or last
(depending on the RDBMS). The ORDER BY clause can be used in conjunction with
other SQL commands and is listed last in the SELECT command sequence.
Task 3.13 Enter the following query and check your output against the results shown
in Figure 35. Describe in your own words what this query is actually doing.
FROM TICKET
17
3.4 Listing Unique Values
The SQL command DISTINCT is used to produce a list of only those values that are
different from one another. For example to list only the different Theme parks from within
the ATTRACTION table, you would enter the following query.
SELECT DISTINCT(PARK_CODE)
FROM ATTRACTION;
Figure 36 shows that the query only displays the rows that are different.
Exercises
E3.1 Write a query to display all Theme Parks except those in the UK.
E3.2 Write a query to display all the sales that occurred on the 18th May 2007.
E3.3 Write a query to display the ticket prices between €20 AND €30.
E3.4 Display all attractions that have a capacity of more than 60 at the Theme Park
FR1001.
18
E3.5 Write a query to display the hourly rate for each attraction where an employee had
worked, along with the hourly rate increased by 20%. Your query should only
Display the ATTRACT_NO, HOUR_RATE and the HOUR_RATE with the 20%
Increase.
E.3.7 Write a query to display all unique employees that exist in the HOURS table.
E.3.8 Display all information from the SALES table in descending order of the sale date.
E.3.9 Write a query to show the transaction numbers and lineprices (in the SALES_LINE
table) that are greater than €50.
E.3.10 Write a query to display only the last two Employee Record
(EMP_NUM,EMP_FNAME) in descending order.
19
Bonus Task
1. Display the employee numbers of all employees and the total number of hours they
have worked Check your result with those shown in below figure 37.
2. Write a query that displays the employees first and last name (EMP_FNAME
and EMP_LNAME), the attraction number(ATTRACT_NO) and the date worked.
Hint:
You will have to join the HOURS and the EMPLOYEE tables. Check your results with
those shown in below figure 38.
20