Difference visibility in subquery join and where

Jordi Reina

I had problems with a simple join:

SELECT * 
FROM worker wo
WHERE EXISTS (
    SELECT wp.id_working_place
    FROM working_place wp 
    JOIN working_place_worker wpw ON ( wp.id_working_place = wpw.id_working_place
        AND wpw.id_worker = wo.id_worker)
)

The error I had was ORA-00904: "WO"."ID_WORKER": not valid identifier.

Then I decided to move the union of tables from join clause to the where clause:

SELECT * 
FROM worker wo
WHERE EXISTS (
    SELECT wp.id_working_place
    FROM working_place wp 
    JOIN working_place_worker wpw ON ( wp.id_working_place = wpw.id_working_place)
    WHERE wpw.id_worker = wo.id_worker
)

And this last query works perfect.

Why is not possible to make it in the join? The table should be visible like it is in the where clause. Am I missing something?

James K. Lowden

In

FROM working_place wp 
JOIN working_place_worker wpw ON ... 
WHERE ...

the ON clause refers only to the two tables participating in the join, namely wp and wpw. Names from the outer query are not visible to it.

The WHERE clause (and its cousin HAVING is the means by which the outer query is correlated to the subquery. Names from the outer query are visible to it.

To make it easy to remember,

  • ON is about the JOIN, how two tables relate to form a row (or rows)
  • WHERE is about the selection criteria, the test the rows must pass

While the SQL parser will admit literals (which aren't column names) in the ON clause, it draws the line at references to columns outside the join. You could regard this as a favor that guards against errors.

In your case, the wo table is not part of the JOIN, and is rejected. It is part of the whole query, and is recognized by WHERE.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Difference visibility in subquery join and where

From Dev

Difference between ON and WHERE in subquery

From Dev

SQL join subquery where condition

From Dev

Difference in where clause and join

From Dev

Subquery or Join a table where a certain value is minimum

From Dev

What is difference between Join and doing same thing with subquery?

From Dev

SQL join to correlated subquery where tables are related by overlapping ranges

From Dev

Is scalar subquery on where condition executed for each join in MySQL?

From Dev

Oracle SQL: Is it more efficient to use a WHERE clause in a subquery or after the join?

From Dev

Difference between using "ON .. AND" and "WHERE" in LEFT JOIN

From Dev

Difference between IS NULL criteria in JOIN and WHERE in a query

From Dev

Difference between filtering queries in JOIN and WHERE?

From Dev

QueryDSL difference between explicit join and where clause?

From Dev

SQL - Difference between WHERE with two tables and a JOIN

From Dev

Difference between using "ON .. AND" and "WHERE" in LEFT JOIN

From Dev

difference in with/without "left join" and matching in "where" or "on"?

From Dev

Join on field vs join on subquery

From Dev

MySQL subquery JOIN

From Dev

Join with subquery in Django ORM

From Dev

ActiveRecord Subquery Inner Join

From Dev

Subquery in Join in CodeIgnitier

From Dev

JPA CriteriaBuilder for join in subquery

From Dev

Rails Subquery join

From Dev

MySQL inner join on subquery

From Dev

MySQL Left Join Subquery with *

From Dev

SubQuery in INNER JOIN (MySQL)

From Dev

SubQuery inner join with condition

From Dev

SQL Subquery with JOIN

From Dev

SQL to LINQ with JOIN and SubQuery

Related Related

HotTag

Archive