Documente Academic
Documente Profesional
Documente Cultură
SELECT STATEMENT
SQL QUERIES
Slide
5-1
SELECT EMP_NO
FROM EMPLOYEE;
FName
LName
Address
Gender
Salary
EMP_NO
DNO
SELECT *
FROM EMPLOYEE
WHERE DNO=2;
Emp_No
Slide
5-2
111222333
John
Peters
33 Ilam Rd
45,000
111222333
123123123
Craig
Graham
56 Clyde Rd
80,000
123123123
444555666
Mary
Jones
100 Memorial Av
43,000
444555666
321321321
Ann
Stevens
354 Ferry Rd
68,000
321321321
777888999
Ian
Watkins
23 Hoani St
65,000
777888999
5-3
Emp_No
FName
LName
Address
Gender
Salary
DNO
111222333
John
Peters
33 Ilam Rd
45,000
123123123
Craig
Graham
56 Clyde Rd
80,000
444555666
Mary
Jones
100 Memorial Av
43,000
321321321
Ann
Stevens
354 Ferry Rd
68,000
777888999
Ian
Watkins
23 Hoani St
65,000
Slide
Emp_No
FName
LName
Address
Salary
DNO
123123123
Craig
Graham
56 Clyde Rd
Gender
M
80,000
444555666
Mary
Jones
100 Memorial Av
43,000
Slide 1-4
Emp_No
FName
LName
Address
Gender
Salary
DNO
111222333
John
Peters
33 Ilam Rd
45,000
123123123
Craig
Graham
56 Clyde Rd
80,000
444555666
Mary
Jones
100 Memorial Av
43,000
321321321
Ann
Stevens
354 Ferry Rd
68,000
777888999
Ian
Watkins
23 Hoani St
65,000
FName
LName
Ann
Stevens
Slide
5-6
Slide 1-5
8/6/2014
ARITHMETIC OPERATIONS
Slide
5-7
Slide
5-8
Query 9:
Produce a list of all stars that acted in movie no 20.
Slide
5-10
PATTERN MATCHING
REGULAR EXPRESSIONS
Query 13: Retrieve all directors who were born during the 1950s.
Slide
5-11
i' : case-insensitive
'c' : case-sensitive
'x' : ignore whitespace
Query 14: Find all stars whose last name is Marais or Moranis.
Slide
5-12
8/6/2014
THREE-VALUED LOGIC
Meanings of NULL
Unknown value
Unavailable or withheld value
Not applicable attribute
Slide
5-13
ORDER BY
ORDER BY (CONT.)
Slide
5-15
AGGREGATE FUNCTIONS
Slide
5-16
Column names
Aggregate functions
Constants
Expressions containing the above
Slide
5-17
Query 19: Find how many comedies there are and how
many AA they won
Slide
5-18
8/6/2014
GROUPING
Slide
5-19
GROUPING (CONT.)
Slide
5-20
HAVING CLAUSE
Slide
5-21
NESTING OF QUERIES
Slide
5-22
NESTED QUERIES
Slide
5-23
Slide
5-24
8/6/2014
Slide
5-25
Slide
5-26
EXISTS
Slide
5-27
Slide
5-28
MULTITABLE QUERIES
EXISTS (CONT.)
Slide
5-29
Query 26 revisited:
List the titles of all movies directed by Stanley Kubrick.
Slide
5-30
8/6/2014
Query 26 revisited:
List the titles of all movies directed by Stanley Kubrick.
Query 31: Select all combinations of movie titles and DVD codes.
Slide
5-31
Slide
5-32
ALIASES
Some queries need to refer to the same relation twice
In this case, an alias (i.e. alternative name) is given to
the relation
Can also use the AS keyword to specify aliases
Aliasing can also be used in any SQL query for
convenience
Query 33: Find the list of any pairs of stars who have
the same last name.
Slide
5-33
Slide
5-34
Slide
5-35
SELECT TITLE
FROM (MOVIE JOIN DIRECTOR ON
DIRECTOR=DNUMBER) AS MOVDIR
Slide
WHERE movdir.lname=Kubrick and movdir.fname=Stanley;
5-36
8/6/2014
SET OPERATIONS
UNION
EXCEPT (MINUS in Oracle)
INTERSECT
Query 35: List the DVD codes and movie numbers for
all DVDs on which the movie is a comedy or that are
rented by Mark Peterson.
Slide
5-38
Slide
5-39
ANY/ALL PREDICATE
Slide
5-40
DIVISION IN SQL
Query
Slide
5-41
Slide
5-42
8/6/2014
DUAL TABLE
Slide
5-43