Sackling
Sackling

Reputation: 1820

Which table is the "left" in this query

SELECT department_name AS dept_name,
       last_name, project_number
FROM departments dpt
    JOIN employee emp
        ON dpt.department_number = emp.department_number
    LEFT JOIN projects prj
        ON emp.employee_id = prj.employee_id
ORDER BY department_name

So I am just confused about the LEFT JOIN portion and how it is working since it is the second join. Is the "left" table employee or is it departments?

Upvotes: 2

Views: 84

Answers (3)

ruakh
ruakh

Reputation: 183290

The "left" side is actually the result of joining departments and employee. Joins are left-associative, so x JOIN y JOIN z is equivalent to (x JOIN y) JOIN z.

Upvotes: 7

Brian Hoover
Brian Hoover

Reputation: 7991

This query assumes that each department will have at least one employee, and that an employee will have zero or more projects.

Upvotes: 0

Joel Coehoorn
Joel Coehoorn

Reputation: 415735

It's both of them. Or, rather, the "left" side is the result of the join expression(s) that preceded the join.

In practice, the query optimizer may re-order things somewhat. But from a logical standpoint, imagine that the query runs each expression one at a time from the beginning to the end, which each new join expression using the entire results computed so far for the left hand side of the operation. The optimizer may decide to swap two joins, or apply a filter from a where clause earlier, but it will never do this in a way to change the resulting relation.

Upvotes: 3

Related Questions