|
|
|
|
|
|
Solution:
We can first find all the male employees: select fname, lname, salary from employee where sex='M' |
Notice that we must use an alias R1 to give the result relation produced by the SELECT command a name (here: R1)
|
Solution:
select fname, lname from employee, works_on, project, (select dnumber from department where dname='Research') X where project.dnum = X.dnumber and works_on.pno = project.pnumber and employee.ssn = works_on.essn |
|
WITH ALIAS as ( SELECT query ) SELECT query |
Effect:
|
|
Solution:
with R1 as (select * from employee where sex='M') select fname, lname from R1 where salary > 30000 |
Processing:
|
|
|
|
Solution:
with R1 as (select * from employee, department where dno=dnumber and dname='Research') , /* Research employees */ R2 as (select * from R1 where sex='M') /* ... that are male */ select fname, lname from R2 where salary > 30000 /* ... who earn > 30000 */ |
Find all information on dependents of John Smith |
with R1 as (SELECT ssn FROM employee WHERE fname='John' AND lname='Smith') select * from dependent where essn IN (select ssn from R1) |
|
|
(With the JOIN operator, the SQL query will look a lot like a Relational Algebra query.)
|
Relational Algebra query:
πfname, lname ( σdname='Research' ( Employee ⋈dno=dnumber Department ) ) |
select fname, lname from employee join department on dno=dnumber where dname='Research' |
|
You could have formulated the above query as followed (without the inner join operator):
select fname, lname from (select * from employee, department where dno=dnumber) where dname='Research' Or even: select fname, lname from employee, department where dno=dnumber and dname='Research' |
select ssn, fname, lname, essn, pno from employee join works_on on ssn=essn 123456789 John Smith 123456789 1 123456789 John Smith 123456789 2 333445555 Frank Wong 333445555 2 333445555 Frank Wong 333445555 3 333445555 Frank Wong 333445555 10 333445555 Frank Wong 333445555 20 999887777 Alice Miller 999887777 30 999887777 Alice Miller 999887777 10 987654321 Jack Wallace 987654321 30 987654321 Jack Wallace 987654321 20 666884444 John Doe 666884444 3 453453453 Joyce English 453453453 1 453453453 Joyce English 453453453 2 987987987 Jake Jones 987987987 30 987987987 Jake Jones 987987987 10 888665555 James Borg 888665555 20 |
select ssn, fname, lname, essn, pno, pnumber, pname from (employee join works_on on ssn=essn) join project on pno=pnumber SSN FNAME LNAME ESSN PNO PNUMBER PNAME --------- ---------- ---------- --------- ---------- ---------- --------------- 123456789 John Smith 123456789 2 2 ProductY 123456789 John Smith 123456789 1 1 ProductX 333445555 Frank Wong 333445555 20 20 Reorganization 333445555 Frank Wong 333445555 10 10 Computerization 333445555 Frank Wong 333445555 3 3 ProductZ 333445555 Frank Wong 333445555 2 2 ProductY 999887777 Alice Miller 999887777 10 10 Computerization 999887777 Alice Miller 999887777 30 30 Newbenefits 987654321 Jack Wallace 987654321 20 20 Reorganization 987654321 Jack Wallace 987654321 30 30 Newbenefits 666884444 John Doe 666884444 3 3 ProductZ 453453453 Joyce English 453453453 2 2 ProductY 453453453 Joyce English 453453453 1 1 ProductX 987987987 Jake Jones 987987987 10 10 Computerization 987987987 Jake Jones 987987987 30 30 Newbenefits 888665555 James Borg 888665555 20 20 Reorganization |
I.e., it's the same as the join operation that we are accustomed to
r1 INNER JOIN r2 ON join-condition |
Find fname, lname of employees in the 'Research' department |
select fname, lname from employee inner join department on dno=dnumber where dname='Research' |
select ssn, fname, lname from employee; SSN FNAME LNAME --------- ---------- ---------- 123456789 John Smith 333445555 Frank Wong 987654321 Jack Wallace 999887777 Alice Miller 666884444 John Doe 453453453 Joyce English 987987987 Jake Jones 888665555 James Borg |
Notice that the non-matching tuples are NOT select
The non-matching tuples will have NULL values in other attributes
|
r1 LEFT [OUTER] JOIN r2 ON join-condition |
Employee: SSN FNAME LNAME --------- ---------- ---------- 123456789 John Smith 333445555 Frank Wong 987654321 Jack Wallace 999887777 Alice Miller 666884444 John Doe 453453453 Joyce English 987987987 Jake Jones 888665555 James Borg |
r1 FULL [OUTER] JOIN r2 ON join-condition |
r1 RIGHT [OUTER] JOIN r2 ON join-condition |
|
|
|
r1 NATURAL JOIN r2 |
The common attrubute will appear ONCE in the result
(You can't have 2 attributes (columns) withe the same name !!!)
select * from works_on; ESSN PNO HOURS --------- ---------- ---------- 123456789 1 32.5 123456789 2 7.5 333445555 2 10 333445555 3 10 333445555 10 10 333445555 20 10 999887777 30 30 999887777 10 10 987654321 30 20 987654321 20 15 666884444 3 40 453453453 1 20 453453453 2 20 987987987 30 5 987987987 10 35 888665555 20 1 |
Notice that the common attribute name ESSN appears only once in the result.
r1 CROSS JOIN r2 ON join-condition |
select ssn, fname, lname, dno, dnumber, dname from employee cross join department SSN FNAME LNAME DNO DNUMBER DNAME --------- ---------- ---------- ---------- ---------- --------------- 123456789 John Smith 5 5 Research 333445555 Frank Wong 5 5 Research 999887777 Alice Miller 4 5 Research 987654321 Jack Wallace 4 5 Research 666884444 John Doe 5 5 Research 453453453 Joyce English 5 5 Research 987987987 Jake Jones 4 5 Research 888665555 James Borg 1 5 Research 123456789 John Smith 5 4 Administration 333445555 Frank Wong 5 4 Administration 999887777 Alice Miller 4 4 Administration 987654321 Jack Wallace 4 4 Administration 666884444 John Doe 5 4 Administration 453453453 Joyce English 5 4 Administration 987987987 Jake Jones 4 4 Administration 888665555 James Borg 1 4 Administration 123456789 John Smith 5 1 Headquarters 333445555 Frank Wong 5 1 Headquarters 999887777 Alice Miller 4 1 Headquarters 987654321 Jack Wallace 4 1 Headquarters 666884444 John Doe 5 1 Headquarters 453453453 Joyce English 5 1 Headquarters 987987987 Jake Jones 4 1 Headquarters 888665555 James Borg 1 1 Headquarters |