Aliases
Aliases rename
columns or tables to
Make names more
meaningful
Make names shorter
and easier to type
Resolve ambiguous
names
Example
Two forms:
Column alias
SELECT column
AS newName...
Table
SELECT
FROM
[AS]
alias
...
table
newName
Employee
WorksIn
ID
Name
ID
Dept
123
124
John
Mary
123
124
124
Marketing
Sales
Marketing
empID Name Dept
123
124
124
John
Mary
Mary
Marketing
Sales
Marketing
G52DBS Database Systems
www.cs.nott.ac.uk/~smx/DBS
G52DBS Database Systems
www.cs.nott.ac.uk/~smx/DBS
Aliases and Self-Joins
Aliases can be used to
copy a table, so that a
it can be combined
with itself:
SELECT A.Name FROM
Employee A,
Employee B
WHERE A.Dept=B.Dept
AND B.Name=Andy
SELECT
E.ID AS empID,
E.Name, W.Dept
FROM
Employee E
WorksIn W
WHERE
E.ID = W.ID
Aliases and Self-Joins
Employee A
Employee B
Employee
Name Dept
John
Mary
Peter
Andy
Anne
Name Dept
Name Dept
John
Mary
Peter
Andy
Anne
John
Mary
Peter
Andy
Anne
Marketing
Sales
Sales
Marketing
Marketing
B
Marketing
Sales
Sales
Marketing
Marketing
G52DBS Database Systems
www.cs.nott.ac.uk/~smx/DBS
Aliases and Self-Joins
SELECT FROM Employee A, Employee B
A.Name
A.Dept
B.Name
John
Mary
Peter
Andy
Anne
John
Mary
Peter
Andy
Anne
Marketing
Sales
Sales
Marketing
Marketing
Marketing
Sales
Sales
Marketing
Marketing
John
Marketing
John
Marketing
John
Marketing
John
Marketing
John
Marketing
Mary
Sales
Mary
Sales
Mary
Sales
Mary
Sales
Mary
Sales
G52DBS
Database Systems
B.Dept
www.cs.nott.ac.uk/~smx/DBS
Marketing
Sales
Sales
Marketing
Marketing
G52DBS Database Systems
www.cs.nott.ac.uk/~smx/DBS
Aliases and Self-Joins
SELECT FROM Employee A, Employee B
WHERE A.Dept = B.Dept
A.Name
A.Dept
B.Name
John
Andy
Anne
Mary
Peter
Mary
Peter
John
Andy
Anne
Marketing
Marketing
Marketing
Sales
Sales
Sales
Sales
Marketing
Marketing
Marketing
John
Marketing
John
Marketing
John
Marketing
Mary
Sales
Mary
Sales
Peter
Sales
Peter
Sales
Andy
Marketing
Andy
Marketing
Andy
Marketing
G52DBS
Database Systems
B.Dept
www.cs.nott.ac.uk/~smx/DBS
Aliases and Self-Joins
SELECT FROM Employee A, Employee B
WHERE A.Dept = B.Dept AND B.Name = Andy
A.Name
A.Dept
B.Name
B.Dept
John
Andy
Anne
Marketing
Marketing
Marketing
Andy
Andy
Andy
Marketing
Marketing
Marketing
Aliases and Self-Joins
SELECT A.Name FROM Employee A, Employee B
WHERE A.Dept = B.Dept AND B.Name = Andy
A.Name
John
Andy
Anne
The result is the names of all employees who work in the
same department as Andy.
G52DBS Database Systems
www.cs.nott.ac.uk/~smx/DBS
Subqueries
A SELECT statement
can be nested inside
another query to
form a subquery
The results of the
subquery are passed
back to the
containing query
E.g. Get the names
of people who are in
Andys department:
SELECT Name
FROM Employee
WHERE Dept =
(SELECT Dept
FROM Employee
WHERE Name=Andy)
G52DBS Database Systems
www.cs.nott.ac.uk/~smx/DBS
Subqueries
SELECT Name
FROM Employee
WHERE Dept =
(SELECT Dept
FROM Employee
WHERE
Name=Andy)
G52DBS Database Systems
www.cs.nott.ac.uk/~smx/DBS
Subqueries
Often a subquery will
return a set of
values rather than a
single value
You cant directly
compare a single
value to a set
Options
IN - checks to see if a
value is in the set
EXISTS - checks to
see if the set is empty
or not
ALL/ANY - checks to
see if a relationship
holds for every/one
member of the set
G52DBS Database Systems
www.cs.nott.ac.uk/~smx/DBS
First the subquery is
evaluated, returning
the value Marketing
This result is passed
to the main query
SELECT Name
FROM Employee
WHERE Dept =
Marketing
G52DBS Database Systems
www.cs.nott.ac.uk/~smx/DBS
(NOT) IN
Using IN we can see
if a given value is in
a set of values
NOT IN checks to
see if a given value
is not in the set
The set can be given
explicitly or from a
subquery
SELECT
FROM
WHERE
IN
<columns>
<tables>
<value>
<set>
SELECT
FROM
WHERE
NOT
<columns>
<tables>
<value>
IN <set>
G52DBS Database Systems
www.cs.nott.ac.uk/~smx/DBS
(NOT) IN
Employee
Name Department
Manager
John
Mary
Chris
Peter
Jane
Chris
Chris
Jane
Jane
Marketing
Marketing
Marketing
Sales
Management
(NOT) IN
SELECT *
FROM Employee
WHERE Department IN
(Marketing,
Sales)
Name Department
Manager
John
Mary
Chris
Peter
Chris
Chris
Jane
Jane
Marketing
Marketing
Marketing
Sales
Employee
Name Department
Manager
John
Mary
Chris
Peter
Jane
Chris
Chris
Jane
Jane
Marketing
Marketing
Marketing
Sales
Management
G52DBS Database Systems
www.cs.nott.ac.uk/~smx/DBS
(NOT) IN
First the subquery
SELECT Manager
FROM Employee
is evaluated giving
Chris
Chris
Jane
Jane
G52DBS Database Systems
www.cs.nott.ac.uk/~smx/DBS
(NOT) EXISTS
This gives
SELECT
FROM
WHERE
IN
Manager
*
Employee
Name NOT
(Chris,
Jane)
Name Department
Manager
John
Mary
Peter
Chris
Chris
Jane
Marketing
Marketing
Sales
Using EXISTS we see
if there is at least
one element in a set
NOT EXISTS is true if
the set is empty
The set is always
given by a subquery
G52DBS Database Systems
www.cs.nott.ac.uk/~smx/DBS
(NOT) EXISTS
Employee
Name Department
Manager
John
Mary
Chris
Peter
Jane
Chris
Chris
Jane
Jane
Marketing
Marketing
Marketing
Sales
Management
SELECT *
FROM Employee
WHERE Name NOT IN
(SELECT Manager
FROM Employee)
SELECT <columns>
FROM <tables>
WHERE NOT EXISTS
<set>
G52DBS Database Systems
www.cs.nott.ac.uk/~smx/DBS
ANY and ALL
SELECT *
FROM Employee AS E1
WHERE EXISTS (
SELECT * FROM
Employee AS E2
WHERE E2.Name =
E1.Manager)
Name Department
Manager
Chris
Jane
Jane
Marketing
Management
SELECT <columns>
FROM <tables>
WHERE EXISTS <set>
G52DBS Database Systems
www.cs.nott.ac.uk/~smx/DBS
ANY and ALL
compare a single
value to a set of
values
They are used with
comparison
operators like =, >,
<, <>, >=, <=
val = ANY (set) is
true if there is at
least one member of
the set equal to the
value
val = ALL (set) is
true if all members of
the set are equal to
the value
G52DBS Database Systems
www.cs.nott.ac.uk/~smx/DBS
ALL
Name Salary
Mary
John
Jane
Paul
20,000
15,000
25,000
30,000
ANY
Find the names of the
employee(s) who earn
the highest salary
SELECT Name
FROM Employee
WHERE Salary >=
ALL (
SELECT Salary
FROM Employee)
Name Salary
Mary
John
Jane
Paul
20,000
15,000
25,000
30,000
G52DBS Database Systems
www.cs.nott.ac.uk/~smx/DBS
Word Searches
Word Searches
Commonly used for
searching product
catalogues etc.
Want to be able to
search by keyword
Want to be able to
use word stemming
for flexible searching
EG: Given a
database of my
books
Searching for crypt
would return
Cryptonomicon by
Neil Stephenson
Applied
Cryptography by
Bruce Schneier
To search we can use queries like
SELECT * FROM Items
WHERE itmID IN (
SELECT itmID FROM ItemKey
WHERE keyID IN (
SELECT keyID FROM Keywords
WHERE keyWord LIKE 'crypt%))
G52DBS Database Systems
www.cs.nott.ac.uk/~smx/DBS
SELECT Name
FROM Employee
WHERE Salary >
ANY (
SELECT Salary
FROM Employee)
G52DBS Database Systems
www.cs.nott.ac.uk/~smx/DBS
Word Searches
To do a word search
we can keep
A table of items to be
searched
A table of keywords
A linking table saying
which keywords
belong to which items
G52DBS Database Systems
www.cs.nott.ac.uk/~smx/DBS
Word Indexes
Find the names of
employee(s) who
earn more than
someone else
Items
itmID itmTitle
Keywords
keyID keyWord
ItemKey
itmID keyID
G52DBS Database Systems
www.cs.nott.ac.uk/~smx/DBS
Word Searches
Sometimes you need
to search for a set of
words
To find entries with all
words you can link
conditions with AND
To find entries with
any of the words use
OR
SELECT * FROM Items
WHERE itmID IN (
SELECT itmID FROM ItemKey
WHERE keyID IN (
SELECT keyID FROM Keywords
WHERE keyWord LIKE
'word1%'))
AND
itmID IN (
SELECT itmID FROM ItemKey
WHERE keyID IN (
SELECT keyID FROM Keywords
WHERE keyWord LIKE
'word2%'))
G52DBS Database Systems
www.cs.nott.ac.uk/~smx/DBS