|
| 1 | +# SQL Joins |
| 2 | + |
| 3 | +> An SQL JOIN clause is used to combine rows from two or more tables, |
| 4 | +> based on a common field between them. |
| 5 | +
|
| 6 | +There are different types of joins available in SQL: |
| 7 | + |
| 8 | +**INNER JOIN**: returns rows when there is a match in both tables. |
| 9 | + |
| 10 | +**LEFT JOIN**: returns all rows from the left table, even if there are no matches in the right table. |
| 11 | + |
| 12 | +**RIGHT JOIN**: returns all rows from the right table, even if there are no matches in the left table. |
| 13 | + |
| 14 | +**FULL JOIN**: It combines the results of both left and right outer joins. |
| 15 | + |
| 16 | +The joined table will contain all records from both the tables and fill in NULLs for missing matches on either side. |
| 17 | + |
| 18 | +**SELF JOIN**: is used to join a table to itself as if the table were two tables, temporarily renaming at least one table in the SQL statement. |
| 19 | + |
| 20 | +**CARTESIAN JOIN**: returns the Cartesian product of the sets of records from the two or more joined tables. |
| 21 | + |
| 22 | +WE can take each first four joins in Details : |
| 23 | + |
| 24 | +## Our example data |
| 25 | + |
| 26 | +We have two tables with the following values. |
| 27 | + |
| 28 | +`TableA`: |
| 29 | + |
| 30 | + id firstName lastName |
| 31 | + ....................................... |
| 32 | + 1 arun prasanth |
| 33 | + 2 ann antony |
| 34 | + 3 sruthy abc |
| 35 | + 6 new abc |
| 36 | + |
| 37 | +`TableB`: |
| 38 | + |
| 39 | + id2 age Place |
| 40 | + ................ |
| 41 | + 1 24 kerala |
| 42 | + 2 24 usa |
| 43 | + 3 25 ekm |
| 44 | + 5 24 chennai |
| 45 | + |
| 46 | +## Details |
| 47 | + |
| 48 | +### INNER JOIN |
| 49 | + |
| 50 | +**Note** :it gives the intersection of the two tables, i.e. rows they have common in TableA and TableB |
| 51 | + |
| 52 | +Syntax |
| 53 | + |
| 54 | + SELECT table1.column1, table2.column2... |
| 55 | + FROM table1 |
| 56 | + INNER JOIN table2 |
| 57 | + ON table1.common_field = table2.common_field; |
| 58 | + |
| 59 | +Apply it in our sample table : |
| 60 | + |
| 61 | + SELECT TableA.firstName,TableA.lastName,TableB.age,TableB.Place |
| 62 | + FROM TableA |
| 63 | + INNER JOIN TableB |
| 64 | + ON TableA.id = TableB.id2; |
| 65 | + |
| 66 | +Result Will Be |
| 67 | + |
| 68 | + firstName lastName age Place |
| 69 | + .............................................. |
| 70 | + arun prasanth 24 kerala |
| 71 | + ann antony 24 usa |
| 72 | + sruthy abc 25 ekm |
| 73 | + |
| 74 | +### LEFT JOIN |
| 75 | + |
| 76 | +**Note** : will give all selected rows in TableA, plus any common selected rows in TableB. |
| 77 | + |
| 78 | +Syntax |
| 79 | + |
| 80 | + SELECT table1.column1, table2.column2... |
| 81 | + FROM table1 |
| 82 | + LEFT JOIN table2 |
| 83 | + ON table1.common_field = table2.common_field; |
| 84 | + |
| 85 | +Apply it in our sample table : |
| 86 | + |
| 87 | + SELECT TableA.firstName,TableA.lastName,TableB.age,TableB.Place |
| 88 | + FROM TableA |
| 89 | + LEFT JOIN TableB |
| 90 | + ON TableA.id = TableB.id2; |
| 91 | + |
| 92 | +Result |
| 93 | + |
| 94 | + firstName lastName age Place |
| 95 | + ............................................................................... |
| 96 | + arun prasanth 24 kerala |
| 97 | + ann antony 24 usa |
| 98 | + sruthy abc 25 ekm |
| 99 | + new abc NULL NULL |
| 100 | + |
| 101 | +### RIGHT JOIN |
| 102 | + |
| 103 | +**Note** : will give all selected rows in TableB, plus any common selected rows in TableA. |
| 104 | + |
| 105 | +Syntax |
| 106 | + |
| 107 | + SELECT table1.column1, table2.column2... |
| 108 | + FROM table1 |
| 109 | + RIGHT JOIN table2 |
| 110 | + ON table1.common_field = table2.common_field; |
| 111 | + |
| 112 | +Apply it in our sample table : |
| 113 | + |
| 114 | + SELECT TableA.firstName,TableA.lastName,TableB.age,TableB.Place |
| 115 | + FROM TableA |
| 116 | + RIGHT JOIN TableB |
| 117 | + ON TableA.id = TableB.id2; |
| 118 | + |
| 119 | +Result |
| 120 | + |
| 121 | + firstName lastName age Place |
| 122 | + ............................................................................... |
| 123 | + arun prasanth 24 kerala |
| 124 | + ann antony 24 usa |
| 125 | + sruthy abc 25 ekm |
| 126 | + NULL NULL 24 chennai |
| 127 | + |
| 128 | +### FULL JOIN |
| 129 | + |
| 130 | +**Note** :It will return all selected values from both tables. |
| 131 | + |
| 132 | +Syntax |
| 133 | + |
| 134 | + SELECT table1.column1, table2.column2... |
| 135 | + FROM table1 |
| 136 | + FULL JOIN table2 |
| 137 | + ON table1.common_field = table2.common_field; |
| 138 | + |
| 139 | +Apply it in our sample table : |
| 140 | + |
| 141 | + SELECT TableA.firstName,TableA.lastName,TableB.age,TableB.Place |
| 142 | + FROM TableA |
| 143 | + FULL JOIN TableB |
| 144 | + ON TableA.id = TableB.id2; |
| 145 | + |
| 146 | +Result |
| 147 | + |
| 148 | + firstName lastName age Place |
| 149 | + ............................................................................... |
| 150 | + arun prasanth 24 kerala |
| 151 | + ann antony 24 usa |
| 152 | + sruthy abc 25 ekm |
| 153 | + new abc NULL NULL |
| 154 | + NULL NULL 24 chennai |
| 155 | + |
| 156 | +## Interesting Fact |
| 157 | + |
| 158 | +For INNER joins the order doesn't matter |
| 159 | + |
| 160 | +For (LEFT, RIGHT or FULL) OUTER joins,the order matter |
| 161 | + |
| 162 | +Better to go check this **[Link][1]** it will give you interesting details about join order |
| 163 | + |
| 164 | +[1]: https://stackoverflow.com/questions/9614922/does-the-join-order-matter-in-sql |
0 commit comments