The from Clause • The from clause lists the relations involved in the query – corresponds to the Cartesian product operation of the relational algebra.
• Find the Cartesian product employee x department select from employee, department ■ Find the employee name, deptno and deptname of all employees select name, employee.eno, deptname from employee, dept where employee. eno = dept.eno
Figure 3.3: Tuples inserted into
Figure 3.4:
Tuple Variables
• Tuple variables are defined in the from clause via the use of the as clause. • Find the customer names and their loan numbers for all customers having a loan at some branch.
select customername, T.loannumber, S.amount from borrower as T, loan as S where T.loannumber = S.loannumber ■ Find the names of all branches that have greater assets than some branch located in Brooklyn. select distinct T.branchname from branch as T, branch as S where T.assets > S.assets and S.branchcity = ‘Brooklyn’
Set Operations
• Find all customers who have a loan, an account, or both: (select customername from depositor) union (select customername from borrower) ■ Find all customers who have both a loan and an (select customername from depositor) intersect (select customername from borrower) ■ Find all customers who have an account but no loan. (select customername from depositor) except (select customername from borrower)
•
Ordering the Display of List in alphabeticTuples order the names of all customers having a loan in Perryridge branch
select distinct customer-name from borrower, loan where borrower loan-number = loan.loan-number and branch-name = ‘Perryridge’ order by customer-name • We may specify desc for descending order or asc for ascending order, for each attribute; ascending order is the default. – E.g. order by customer-name desc
Example 2 • Sailors (sid,sname, rating,age) • Boats(bid, bname,color) • Reserves(sid,bid ,date) • Refer to attached list for rows