пятница, 5 февраля 2016 г.

Type of Joins. Inner vs Outer join

We have 2 tables. Lets practice!
Table 1                                   
ID 
Name
1
Dan
2
Bob
3
Elly

Table 2
ID 
Job position
2
QA
3
Dev
4
Trainee


Example: SELECT * FROM table1 INNER JOIN table2 on table1.ID=table2.ID
Result:
ID 
Name
ID 
Job position
2
Bob
2
QA
3
Elly
3
Dev



Example: SELECT * FROM table1 LEFT JOIN table2 on table1.ID=table2.ID
Result:
ID 
Name
ID 
Job position
1
Dan
Null
Null
2
Bob
2
QA
3
Elly
3
Dev

Example: SELECT * FROM table1 RIGHT JOIN table2 on table1.ID=table2.ID
Result:
ID 
Name
ID 
Job position
2
Bob
2
QA
3
Elly
3
Dev
Null
Null
4
Trainee


Example: SELECT * FROM table1, table2  
Result will be always Count(*) from table1 multiply by count(*) from table 2,
so in our example it is 3*3=9


ID 
Name
ID 
Job position
1
Dan
2
QA
1
Dan
3
Dev
1
Dan
4
Trainee
2
Bob
2
QA
2
Bob
3
Dev
2
Bob
4
Trainee
3
Elly
2
QA
3
Elly
3
Dev
3
Elly
4
Trainee



Комментариев нет:

Отправить комментарий