The relational-algebra equivalents are: cross ×, then select σ, then project π.
first, take the cartesian product R1 × R2 × …
then, perform a selection that selects tuples from the cross product that satisfy the predicate in the WHERE clause
then, perform a projection of attributes a1, a2, … from the tuples selected in step 2, leaving duplicates alone by default (unlike relational algebra!)
(These steps tell us what tuples will appear in the resulting relation, but the command may be executed differently for the sake of efficiency.)
Note: the SELECT clause by itself specifies a projection! The WHERE clause specifies a selection.
Example Query
Given these relations:
Student(id, name)
Enrolled(student_id, course_name, credit_status)
MajorsIn(student_id, dept_name)
We want to find the major of the student John Tukey*.
Here’s a query that will give us the answer:
SELECT dept_nameFROM Student, MajorsInWHERE name ='John Tukey'ANDid= student_id;
SELECT dept_name FROM Student, MajorsInWHERE name ='John Tukey'ANDid= student_id;
Join Conditions
Here’s the query from the previous problem:
SELECT dept_nameFROM Student, MajorsInWHERE name ='John Tukey'ANDid= student_id;
id = student_id is a join condition – a condition that is used to match up “related” tuples from the two tables.
it selects the tuples in the Cartesian product that “make sense”
for N tables, you typically need N – 1 join conditions
The LIKE Operator and Wildcards
Use LIKE whenever we need to match a pattern.
Form the pattern using one or more wildcard characters:
% stands for 0 or more arbitrary characters
_ stands for a single arbitrary character
How could we use pattern matching to get info about movies rated PG or PG-13?
A. SELECT * FROM Movie WHERE rating LIKE 'PG%';← starts with PG, followed by 0 or more arbitrary characters
B. SELECT * FROM Movie WHERE rating LIKE 'PG_';← starts with PG, followed by exactly 1 arbitrary character
C. SELECT * FROM Movie WHERE rating LIKE '_G%';← starts with an arbitrary character, then G, then 0 or more characters
What about these patterns for finding PG and PG-13?
A. SELECT * FROM Movie WHERE rating LIKE '%G%';← no. would also match G
B. SELECT * FROM Movie WHERE rating LIKE 'PG';← no. would not match PG-13
C. SELECT * FROM Movie WHERE rating = 'PG-%';← no. need to use LIKE with patterns! won’t match any rating!
Comparisons Involving NULL
Because NULL is a special value, any comparison involving NULL that uses the standard operators is always false.
For example, all of the following will always be false:
room = NULL
NULL != 10
room != NULL
NULL = NULL
This is useful for cases like the following:
assume that we add a country column to Student
use NULL for students whose country is unknown
to get all students from a foreign country:
SELECT name FROM StudentWHERE country !='USA'; -- won't include NULLs
Comparisons Involving NULL (cont.)
To test for the presence or absence of a NULL value, use special operators: IS NULLIS NOT NULL
Example: find students whose country is unknown
SELECT name FROM Student WHERE country ISNULL;
Removing Duplicates
By default, a SELECT command may produce duplicates.
To eliminate them, add the DISTINCT keyword:
SELECTDISTINCT column1, column2, …
The COUNT Function
In what follows, we’ll use the COUNT function.
COUNT is an aggregate function. It counts the number of values of an attribute or the number of tuples in a relation.
We will cover aggregate functions in the next lecture.
COUNT(*) vs. COUNT(attribute)
SELECT COUNT(*) counts the number of tuples in a result.
example: find the total number of courses
SELECTCOUNT(*)FROM Course;
result: COUNT(*) = 6
SELECT COUNT(attribute) counts the number of non-NULL values of that attribute in a result.
example: find the number of courses that meet in a room
SELECTCOUNT(room_id)FROM Course;
result: COUNT(room_id) = 5
How could we determine how many people have won Best Actor?
A. SELECT COUNT(person_id) FROM Oscar WHERE type = 'BEST-ACTOR';← double counts repeat winners
B. SELECT TOTAL(person_id) FROM Oscar WHERE type = 'BEST-ACTOR';← invalid aggregate function
C. SELECT COUNT(*) FROM Oscar WHERE type = 'BEST-ACTOR';← double counts repeat winners
D. two or more of the queries above would work
E. none of the queries above would work
Removing Duplicates
By default, a SELECT command may produce duplicates.