Filtering with WHERE while using JOIN?
I want to make a query that returns all customers, but match orders only
if they were placed in February 2008. This requires a join of tables
Customers and Orders.
I used two queries and got different answers. The second one is right, but
why ? Logically speaking, I was expecting the first one to be right, but
its not.
SELECT Cust.custid, Cust.contactname, Ord.orderid, Ord.orderdate
FROM Sales.Customers AS Cust LEFT OUTER JOIN
Sales.Orders Ord
ON Cust.custid = Ord.custid
WHERE Ord.orderdate >= '2008-02-01'
AND Ord.orderdate < '2008-03-01'
order by Cust.custid, Ord.orderdate asc
Second query -
SELECT C.custid, C.companyname, O.orderid, O.orderdate
FROM Sales.Customers AS C
LEFT OUTER JOIN Sales.Orders AS O
ON C.custid = O.custid
AND O.orderdate >= '20080201'
AND O.orderdate < '20080301';
No comments:
Post a Comment