Fit2094-3171 Clayton Lecture 7 SQL - Part 1
Fit2094-3171 Clayton Lecture 7 SQL - Part 1
INFORMATION
TECHNOLOGY
Week 7 -
Structured Query Language (SQL) –
Part 1
2
[Clayton] Audience Q&A
3
[Clayton] Marc’s anecdote
Predicate / search
condition
6
SQL SELECT Statement - Usage
[Clayton] Q&A - recall relational algebra? PI (project) and SIGMA (select) -- note that SIGMA
“select” is NOT EXACTLY the same as the diagram/code shown. 7
So what is the actual algebraic expr.?
[Clayton] Audience Q&A
Refer to Coronel & Morris for examples of how certain syntax differs
from ‘brand’ to ‘brand’ e.g. Oracle vs MySQL
8
SQL Predicates or Search Conditions
▪ The search conditions are applied on each row, and the row is
returned if the search conditions are evaluated to be TRUE for that
row.
▪ Comparison
– Compare the value of one expression to the value of another
expression.
– Operators (note: = not the same as prog. lang.’s double ==)
• =, < >, <, >, !=, <=, >=
– Example: salary > 5000
▪ Range
– Test whether the value of an expression falls within a specified
range of values.
– Operators:
• BETWEEN
– Example: salary BETWEEN 1000 AND 3000 (both are inclusive)
9
SQL Predicates or Search Conditions
▪ Set Membership
– To test whether the value of expression equals one of a set of values.
– Operator:
• IN
– Example : city IN (’Melbourne’, ’Sydney’)
▪ Pattern Match
– To test whether a string (text) matches a specified pattern.
– Operator:
• LIKE
– Patterns:
• % character represents any sequence of zero or more character.
• _ character represents any single character.
– Example:
• WHERE city LIKE ‘M%’
• WHERE unit_code LIKE ‘FIT20__’
[Clayton] Q&A - note that % and _ are different from, say, your OS wildcards or RegEx (? *) 10
SQL Predicates or Search Conditions
▪ NULL
11
What row will be retrieved?
12
CLAYTON Thursday 6TIBDU Friday 4AJ0Y6
Q1. Consider the predicate "enrol_mark >= 50", what row(s) will be
selected for this predicate by the DBMS?
a. 1, 4 and 6
b. All rows
c. 1 and 6
d. All rows except row 4
13
Combining Predicates
▪ Logical operators
– AND, OR, NOT
▪ Rules:
– An expression is evaluated LEFT to RIGHT.
– Sub-expression in brackets are evaluated first.
– NOTs are evaluated before AND and OR
– ANDs are evaluated before OR.
– NB: similar to many prog. languages.
14
Truth Table
• AND is evaluated to be TRUE if and only if both conditions are
TRUE
• OR is evaluated to be TRUE if and only if at least one of the
conditions is TRUE
AND
B T = TRUE
A T U F
F = FALSE
T T U F U = Unknown
U U U F
F F F F
OR
B
A T U F
Unknown = NULL in
T T T T relational database
U T U U
F T U F
15
CLAYTON Thursday 6TIBDU Friday 4AJ0Y6
Q2. What row will be retrieved when the WHERE clause predicate is
written as
V_CODE = 21344 AND V_CODE = 24288
V_CODE
a. 1,3,5
1 21344
b. 1
2 20001 c. 3,5
d. No rows will be retrieved
3 24288
4 20001
5 24288
16
CLAYTON Thursday 6TIBDU Friday 4AJ0Y6
Q3. What row will be retrieved when the WHERE clause predicate is
written as
V_CODE <> 21344 OR V_CODE <> 24288
V_CODE
a. 1,3,5
1 21344
b. 2,4
2 20001 c. 3,5
d. 1,2,3,4,5
3 24288
4 20001
5 24288
17
18
CLAYTON Thursday 6TIBDU Friday 4AJ0Y6
19
Arithmetic Operations
20
Oracle NVL function
SELECT stu_nbr,
NVL(enrol_mark,0),
NVL(enrol_grade,'WH')
FROM enrolment;
21
[Clayton] Audience Q&A
ADVANCED question...
22
AS - Column Aliases
23
Sorting Query Result
24
CLAYTON Thursday 6TIBDU Friday 4AJ0Y6
25
[Clayton] Audience Q&A
26
Removing Duplicate Rows in the Query Result
27
Coffee break - see you in 10 minutes.
STUDENT QUALIFICATION
29
JOIN-ing Multiple Tables
30
JOIN-ing Multiple Tables - Step by Step
unit is now called ‘u’ as a table alias same goes for enrolment ‘e’
FROM ((unit u JOIN enrolment e ON u.unit_code=e.unit_code)
JOIN student s ON e.stu_nbr=s.stu_nbr)
31
[Clayton] Audience Q&A
32
Oracle Reference: Date Data Type
35
Oracle: Current Date
36
Practice
38
Summary
39