Lecture 9: SQL SELECT PART 3 & RA Flashcards
example of cartesian product query
SELECT c.clientNo, c.fname, c.lname, v.clientNo, v.propertyNo, v.viewDate, v.comment
FROM client c, viewing
What are multi-table queries?
- Natural Join or Simple Join
- one of the most difficult operations to implement efficiently
- Must use join if result columns come from more than one table
- perform join, include more than one table in from clause separated by comma
- include WHERE clause to specify join columns
Eg of three table join
SELECT b.branchNo, b.city, s.staffNo, fName, lName, propertyNo
FROM branch b, staff s, propertyForRent
WHERE b.branchNo = s.branchNo AND
s.staffNo = p.staffNo
ORDER BY b.branchNo, s.staffNo, propertyNo;
list the procedures for generating results of a join
- Form Cartesian product of the tables named in FROM clause
- If there is a WHERE clause, apply the search condition to each row of the product table, retaining those rows that satisfy the condition
- For each remaining row, determine value of each item in SELECT list to produce a single row in result table
- If DISTINCT has been specified, eliminate any duplicate rows from the result table
- If there is an ORDER BY clause, sort result table as required
What are outer joins?
outer join operations retain rows that do not satisfy the join condition
What are left outer joins?
includes rows of first (left) table unmatched with rows from second right table
columns from second table are filled nulls
What are right outer joins?
- right outer joins includes those two rows of second (right) table that are unmatched with rows from first (left) table
- columns from first table are filled with NULLS
What are full outer joins?
- includes rows that are unmatched in both tables
* unmatched columns are filled with nulls
ANY and ALL
- any and all may be used with subqueries that produce a single column of numbers
- ALL = true when values produced by subquery
- ANY = true if it is satisfied by any values produced by subquery
- if subquery empty, all returns true, any returns false
- some may be used in place of any
Exists and Not exists
EXISTS and NOT EXISTS are for use only with subqueries
Produce a simple true/false result
True if and only if there exists at least one row in result table returned by subquery
False if subquery returns an empty result table
NOT EXISTS is the opposite of EXISTS
List the relational algebra
theta join equijoin natural join outer join left outer join
also division command