FAQ/Questions/Different-Join-Types-and-Filters.md
... ...
@@ -1,6 +1,10 @@
1
-###SQL Samples of Various Join Types
1
+#SQL Samples of Various Join Types
2 2
3
-**Inner (Direct) Join:**  Selects rows from two tables such that the value in one column of the first table also appears in a column of the second table.  
3
+[[_TOC_]
4
+
5
+##Inner (Direct) Join
6
+
7
+Selects rows from two tables such that the value in one column of the first table also appears in a column of the second table.  
4 8
5 9
```sql
6 10
SELECT DISTINCT 
... ...
@@ -9,7 +13,9 @@ FROM [dbo].[Orders]
9 13
INNER JOIN [dbo].[Invoices] ON [dbo].[Invoices].[CustomerID]=[dbo].[Orders].[CustomerID];
10 14
```
11 15
12
-**Cross Join:** A cross join will return a result table where each row from the first table is combined with each row from the second table. 
16
+##Cross Join
17
+
18
+A cross join will return a result table where each row from the first table is combined with each row from the second table. 
13 19
14 20
```sql
15 21
SELECT DISTINCT 
... ...
@@ -18,7 +24,9 @@ FROM [dbo].[Orders]
18 24
CROSS JOIN [dbo].[Invoices];
19 25
```
20 26
21
-**Left(First Exists) Join:** The Left Outer Join known also as Left Join returns all rows from the left table in the Left Outer Join clause, no matter if the joined columns match.  A field in a result row will be null if the corresponding input table did not contain a matching row.
27
+##Left(First Exists) Join
28
+
29
+The Left Outer Join known also as Left Join returns all rows from the left table in the Left Outer Join clause, no matter if the joined columns match.  A field in a result row will be null if the corresponding input table did not contain a matching row.
22 30
23 31
```sql
24 32
SELECT DISTINCT 
... ...
@@ -27,7 +35,9 @@ FROM [dbo].[Orders]
27 35
LEFT OUTER JOIN [dbo].[Invoices] ON [dbo].[Invoices].[CustomerID]=[dbo].[Orders].[CustomerID];
28 36
```
29 37
30
-**Right Join:** The Right Outer Join known also as Right Join returns all rows from the right table in the Right Outer Join clause, no matter if the joined columns match.  A field in a result row will be null if the corresponding input table did not contain a matching row.
38
+##Right Join
39
+
40
+ The Right Outer Join known also as Right Join returns all rows from the right table in the Right Outer Join clause, no matter if the joined columns match.  A field in a result row will be null if the corresponding input table did not contain a matching row.
31 41
32 42
```sql
33 43
SELECT DISTINCT 
... ...
@@ -36,7 +46,9 @@ FROM [dbo].[Orders]
36 46
RIGHT OUTER JOIN [dbo].[Invoices] ON [dbo].[Invoices].[CustomerID]=[dbo].[Orders].[CustomerID];
37 47
```
38 48
39
-**Full Join:** The Full Outer Join known also as Full Join returns all rows from Both the Right Outer Join & Left Outer Join.  A field in a result row will be null if the corresponding input table did not contain a matching row.
49
+##Full Join
50
+
51
+The Full Outer Join known also as Full Join returns all rows from Both the Right Outer Join & Left Outer Join.  A field in a result row will be null if the corresponding input table did not contain a matching row.
40 52
41 53
```sql
42 54
SELECT DISTINCT 
... ...
@@ -45,7 +57,7 @@ FROM [dbo].[Orders]
45 57
FULL OUTER JOIN [dbo].[Invoices] ON [dbo].[Invoices].[CustomerID]=[dbo].[Orders].[CustomerID];
46 58
```
47 59
48
-##Filters and Join Types
60
+##Filters in WHERE
49 61
50 62
If you specify a [[filter|http://wiki.izenda.us/Guides/ReportDesign/5.0-Filters-tab]] for the column in first(left) table in JOIN clause, all filters will be applied as usual (i.e. all filters will be in the WHERE clause):
51 63
... ...
@@ -58,6 +70,8 @@ LEFT OUTER JOIN [dbo].[Orders] ON [dbo].[Orders].[OrderID]=[dbo].[Order Details]
58 70
WHERE [dbo].[Order Details].[OrderID] = 10248
59 71
```
60 72
73
+##Filters in Joins
74
+
61 75
Now let's say we want to specify filter for the column in the second(right) table. In this case, we should add this filter to the JOIN clause itself:
62 76
63 77
```sql
... ...
@@ -70,6 +84,8 @@ LEFT OUTER JOIN [dbo].[Orders] ON [dbo].[Orders].[OrderID]=[dbo].[Order Details]
70 84
71 85
The reason why we shouldn't place the filter in the WHERE clause is that it will transform our **LEFT OUTER JOIN** to an **INNER JOIN**. For example, when you use "WHERE [dbo].[Order Details].[OrderID] = 10248", you remove all NULLs from the results (obviously NULL doesn't equal 10248). To avoid this (to retain NULLs in the results) we should follow ANSI-92 SQL syntax and use conditional joins.
72 86
87
+##Filters in Inner Queries
88
+
73 89
We can also use an inner query to apply our outer left join:
74 90
75 91
```sql
... ...
@@ -83,7 +99,11 @@ WHERE [dbo].[Orders].[OrderID] = 10248) AS Ord
83 99
ON Ord.[OrderID]=[dbo].[Order Details].[OrderID]
84 100
```
85 101
86
-When we apply filters within the JOIN clause, it's like we applied these filters separately to the table, and then added them to the JOIN. This can help speedup database performance. Please refer to the articles below for more information:
102
+When we apply filters within the JOIN clause, it's like we applied these filters separately to the table, and then added them to the JOIN. This can help speedup database performance.
103
+
104
+##References
105
+
106
+Please refer to the articles below for more information:
87 107
88 108
* [[ANSI SQL-92 Standard|http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt]]
89 109
* [[MSDN article about OUTER joins|http://msdn.microsoft.com/en-us/library/aa213228]]
... ...
\ No newline at end of file