WHERE filters rows based on conditions and can reference columns from the tables participating in the JOIN.
Filtering joined data is essential for retrieving only the records relevant to a business question.
Return orders from customers located in Pune.
SELECT
c.CustomerName,
c.City,
o.OrderID,
o.OrderAmount
FROM Customers c
INNER JOIN Orders o
ON c.CustomerID = o.CustomerID
WHERE c.City = 'Pune';
Used in almost every practical SQL report involving multiple tables.
- WHERE filters rows.
- WHERE can reference columns from joined tables.
- JOIN defines the relationship.
- WHERE defines additional filtering.
- Filter conditions should reflect the business requirement.
Can WHERE reference columns from both joined tables?
Yes. WHERE can reference columns from any table available in the query.
What is the role of WHERE in a JOIN query?
WHERE restricts the rows returned after the JOIN based on specified conditions.