1

I am trying to get the difference between below 2 queries

Query 1:

In which I have given condition 'Orders.OrderID != null' in on clause

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders
ON (Customers.CustomerID=Orders.CustomerID and Orders.OrderID is not null)

Query 2:

In which I have given condition 'Orders.OrderID != null' in where clause

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders
ON Customers.CustomerID=Orders.CustomerID where Orders.OrderID is not null

These 2 queries are generating difference results. Why? What is the execution flow for where clause and on condition clause?

P.S I am using the following link for testing of the queries http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_join

5
  • 1
    OrderID != null is never correct, regardless of where you put it. The correct way to test against nulls would be OrderID IS NOT NULL.
    – Mureinik
    May 5, 2014 at 13:21
  • I am using the following link to test the query.. where above mentioned query is working fine.. w3schools.com/sql/trysql.asp?filename=trysql_select_join
    – Dhruv
    May 5, 2014 at 13:23
  • in both flavors (condition in the on and the where clauses), OrderID != null returns 0 rows, while OrderID IS NOT NULL returns 196 rows. How can it the query be "working fine"?
    – Mureinik
    May 5, 2014 at 13:27
  • Something wrong with your design here, how do you get an order in Orders with a null ID, but it does have a customerID, that would worry me, a lot.. May 5, 2014 at 13:37
  • This website has a related examples: codeproject.com/Articles/231132/… Cases 2 & 3 pertain to your question.
    – Joseph B
    May 5, 2014 at 13:46

6 Answers 6

4

The difference in that in query 1, Orders.OrderID != null (or Orders.OrderId is not null) is being used to filter the rows in the Orders table and in query2, it is being used to filter the rows in the result set.

0

Change the != NULL to IS NOT NULL

2
0

Change the != NULL to IS NOT NULL and do not forget to place parentheses at ON clause.

This way you tell the server that you want all conditions (inside parentheses) to be met when joining.

0

The Q2 in fact it the same as INNER JOIN Orders.

The rows without peer are filtered out.

UPDATE

for the Q1 if the condition is false the row from Customers is included in results. for the Q2 the left joined rows are filtered out because hteir OrderId is null

0

The syntax of your queries look similar to T-Sql, except the comparison "!=". If you are using MSSQL Server, the proper way to test null values is OrderId is not null.

Besides the uses of different operators, the behaviors of your queries are also driven by order of processing. The WHERE clause conditions are evaluated upon the results of all JOIN's. This is true for MSSQL. Because of that, putting conditions in WHERE clause has similar effects as if you do an INNER JOIN, except that you are wasting more resources.

3
  • so why in case of left join.. null values are not getting filtered ?
    – Dhruv
    May 5, 2014 at 13:30
  • 1
    @dhroove in the first query? Because it's a join condition. An left outer join will give you all rows of the first table combined with those rows of the second table that fulfill the join condition - if there isn't one the selected fields from the second table will be null. That's a huge difference to filtering with WHERE.
    – VMai
    May 5, 2014 at 14:16
  • @dhroove JOIN conditions are for matching, not filtering. VMai is correct that LEFT JOIN takes precedence over matching conditions. May 5, 2014 at 14:49
0

The difference is that first query will output all rows from first table and only those rows from second table which match condition. That means that columns that belong to second table will be null, but combined row would still exist in result.

In second case combined rows which have order ID null will be removed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.