Difference between and and where in joins
Firstly there is a semantic difference. When you have a join, you are saying that the relationship between the two tables is defined by that condition. So in your first example you are saying that the tables are related by cd.Company = table2.Name AND table2.Id IN (2728)
. When you use the WHERE clause, you are saying that the relationship is defined by cd.Company = table2.Name
and that you only want the rows where the condition table2.Id IN (2728)
applies. Even though these give the same answer, it means very different things to a programmer reading your code.
In this case, the WHERE clause is almost certainly what you mean so you should use it.
Secondly there is actually difference in the result in the case that you use a LEFT JOIN instead of an INNER JOIN. If you include the second condition as part of the join, you will still get a result row if the condition fails - you will get values from the left table and nulls for the right table. If you include the condition as part of the WHERE clause and that condition fails, you won't get the row at all.
Here is an example to demonstrate this.
Query 1 (WHERE):
SELECT DISTINCT field1
FROM table1 cd
LEFT JOIN table2
ON cd.Company = table2.Name
WHERE table2.Id IN (2728);
Result:
field1
200
Query 2 (AND):
SELECT DISTINCT field1
FROM table1 cd
LEFT JOIN table2
ON cd.Company = table2.Name
AND table2.Id IN (2728);
Result:
field1
100
200
Test data used:
CREATE TABLE table1 (Company NVARCHAR(100) NOT NULL, Field1 INT NOT NULL);
INSERT INTO table1 (Company, Field1) VALUES
('FooSoft', 100),
('BarSoft', 200);
CREATE TABLE table2 (Id INT NOT NULL, Name NVARCHAR(100) NOT NULL);
INSERT INTO table2 (Id, Name) VALUES
(2727, 'FooSoft'),
(2728, 'BarSoft');
Difference between inner join and where in select join SQL statement
One difference is that the first option hides the intent by expressing the join condition in the where clause.
The second option, where the join condition is written out is more clear for the user reading the query. It shows the exact intent of the query.
As far as performance or any other difference, there shouldn't be any. Both queries should return the exact same result and perform the same under most RDBMS.
What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?
Reading this original article on The Code Project will help you a lot: Visual Representation of SQL Joins.
Also check this post: SQL SERVER – Better Performance – LEFT JOIN or NOT IN?.
Find original one at: Difference between JOIN and OUTER JOIN in MySQL.
In SQL / MySQL, what is the difference between ON and WHERE in a join statement?
WHERE
is a part of the SELECT
query as a whole, ON
is a part of each individual join.
ON
can only refer to the fields of previously used tables.
When there is no actual match against a record in the left table, LEFT JOIN
returns one record from the right table with all fields set to NULLS
. WHERE
clause then evaluates and filter this.
In your query, only the records from gifts
without match in 'sentgifts' are returned.
Here's the example
gifts
1 Teddy bear
2 Flowers
sentgifts
1 Alice
1 Bob
---
SELECT *
FROM gifts g
LEFT JOIN
sentgifts sg
ON g.giftID = sg.giftID
---
1 Teddy bear 1 Alice
1 Teddy bear 1 Bob
2 Flowers NULL NULL -- no match in sentgifts
---
SELECT *
FROM gifts g
LEFT JOIN
sentgifts sg
ON g.giftID = sg.giftID
WHERE sg.giftID IS NULL
---
2 Flowers NULL NULL -- no match in sentgifts
As you can see, no actual match can leave a NULL
in sentgifts.id
, so only the gifts that had not ever been sent are returned.
Difference between Where and Join on Id
This is very good practice -- in fact, you cannot (easily) get this logic in a WHERE
clause.
A LEFT JOIN
returns all rows in the first table -- even when there are no matches in the second.
So, this returns all rows in the preceding tables -- and any rows from Client
where ClientId = 1
. If there is no match on that ClientId
, then the columns will be NULL
, but the rows are not filtered.
Difference between ON and WHERE clauses in SQL table joins
The main difference is when you are using different joins.
Typically you should see the same result if you were to use inner joins, but once you start using LEFT joins the results will change.
Have a look at the following example
SQL Fiddle DEMO
And have a look at the following article (very explanatory)
EDIT for @ShannonSeverance
Schema and Test data
CREATE TABLE Table1 (
ID INT,
Val VARCHAR(20)
);
INSERT INTO Table1 VALUES (1,'a');
INSERT INTO Table1 VALUES (2,'a');
CREATE TABLE Table2 (
ID INT,
Val VARCHAR(20)
);
INSERT INTO Table2 VALUES (1,'a');
and Tests
SELECT t1.ID,
t1.Val,
t2.ID ID2,
t2.Val Val2
FROM Table1 t1 INNER JOIN
Table2 t2 ON t1.ID = t2.ID AND t1.Val = t2.Val;
SELECT t1.ID,
t1.Val,
t2.ID ID2,
t2.Val Val2
FROM Table1 t1,Table2 t2
WHERE t1.ID = t2.ID
AND t1.Val = t2.Val;
SELECT t1.ID,
t1.Val,
t2.ID ID2,
t2.Val Val2
FROM Table1 t1 LEFT JOIN
Table2 t2 ON t1.ID = t2.ID AND t1.Val = t2.Val;
SELECT t1.ID,
t1.Val,
t2.ID ID2,
t2.Val Val2
FROM Table1 t1 LEFT JOIN
Table2 t2 ON t1.ID = t2.ID
WHERE t1.Val = t2.Val;
SQL LEFT JOIN: difference between WHERE and condition inside AND
with a left join
there is a difference
with condition on left join
rows with column > 10
will be there filled with nulls
with where
condition rows will be filtered out
with a inner join
there is no difference
example:
declare @t table (id int, dummy varchar(20))
declare @a table (id int, age int, col int)
insert into @t
select * from (
values
(1, 'pippo' ),
(2, 'pluto' ),
(3, 'paperino' ),
(4, 'ciccio' ),
(5, 'caio' ),
(5, 'sempronio')
) x (c1,c2)
insert into @a
select * from (
values
(1, 38, 2 ),
(2, 26, 5 ),
(3, 41, 12),
(4, 15, 11),
(5, 39, 7 )
) x (c1,c2,c3)
select t.*, a.age
from @t t
left join @a a on t.ID = a.ID and a.col > 10
Outputs:
id dummy age
1 pippo NULL
2 pluto NULL
3 paperino 41
4 ciccio 15
5 caio NULL
5 sempronio NULL
While
select t.*, a.age
from @t t
left join @a a on t.ID = a.ID
where a.col > 10
Outputs:
id dummy age
3 paperino 41
4 ciccio 15
So with LEFT JOIN
you will get ALWAYS all the rows from 1st table
If the join condition is true, you will get columns from joined table filled with their values, if the condition is false their columns will be NULL
With WHERE
condition you will get only the rows that match the condition.
Difference between where and and clause in join sql query
Base on the following two test result
select a.id, a.name,a.country from table a left join table b
on a.id = b.id
where a.name is not null
is faster (237 Vs 460). As far as I know, it is a standard.
Related Topics
Does Assigning Stored Procedure Input Parameters to Local Variables Help Optimize the Query
Rowset Does Not Support Scrolling Backward
How to Export Data from Excel Spreadsheet to SQL Server 2008 Table
How to Transfer or Export SQL Server 2005 Data to Excel
How to Expand Comma Separated Values into Separate Rows Using SQL Server 2005
Left Join Query Not Returning All Rows in First Table
Check Bound Datatable for Null Value Vb.Net
SQL Join on Multiple Columns in Same Tables
When to Use SQL Sub-Queries Versus a Standard Join
Difference Between Timestamps in Milliseconds in Oracle
SQL Server 2005 - Export Table Programmatically (Run a .SQL File to Rebuild It)
Rails Find Record with Zero Has_Many Records Associated
What Is Your Naming Convention for Stored Procedures
T-Sql: Checking for Email Format