Quote:
|
Originally Posted by FAC51
Code:
Select ename, job, sal
from emp join dept using (deptno)
where loc = 'NEW YORK';
Select ename, job, sal
from emp
where deptno = (select deptno from dept where loc = 'NEW YORK');
|
Oh, yeah, in principle a subquery using "=" is the same as one using "IN" except that I'm not sure what SQL is supposed to do if your subquery returns two rows. I also think it violates common sense to say that a table is equal to a number, but that's SQL for you.
Anyhw, your query is roughly the same as saying:
Code:
SELECT emp.ename, emp.job, emp.sal
FROM emp INNER JOIN dept ON emp.deptno = dept.deptno
WHERE dept.loc = 'New York'
Except that I know what would happen if there were two entries in dept for a given deptno. (You might have to put DISTINCT in there...) I'm guessing that deptno is a candidate key (meaning it's either a primary key or a UNIQUE constraint) and that that's not an issue.
There's also "EXISTS" type subqueries. These are SQL's inheritance from the relational calculus.
Code:
SELECT * FROM X
WHERE EXISTS (SELECT * FROM P WHERE P.a = X.a)
We can still get that behavior like so:
Code:
SELECT X.*
FROM X INNER JOIN P ON X.a = P.a