-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path45. AK- ALL JOINS with nulls.sql
52 lines (43 loc) · 1.41 KB
/
45. AK- ALL JOINS with nulls.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#Most Asked SQL JOIN based Interview Question | # of Records after 4 types of JOINs
#CREATE TABLE t7(id int );
#CREATE TABLE t8(id int );
#insert into t7 values (1);
#insert into t7 values (1);
#insert into t7 values (2);
#insert into t7 values (2);
#insert into t7 values (4);
#insert into t7 values (null);
#insert into t8 values (1);
#insert into t8 values (1);
#insert into t8 values (1);
#insert into t8 values (3);
#insert into t8 values (2);
#insert into t8 values (2);
#insert into t8 values (null);
#delete from t2;
select * from t7;
select * from t8;
#null cant be compare with other null,so null also not joined with another null
select * from t7
inner join t8 on t7.id=t8.id;
#result on inner join+not matching records from left,6+ 2*1=(2) 2 records of 2
#all are matching in left table so sresult will be same as inner join
# 2*3 + 4+ 1 not matching+1 not matching =12
select * from t7
right join t8 on t7.id=t8.id;
#right= non matching records from left
#inner join+not matching records from right
#2*3 +4+ 1 not m+1 not m
select * from t7
left join t8 on t7.id=t8.id;
#matching records from oth table & non matching records from both table
#ssms only
# 3*2 +2 +1 + 2*2 + 4 is not matching so 1 record for this + 3 not matching = 12 records
select * from t7
full outer join t8 on t7.id=t8.id;
#mysql full outer join
SELECT * FROM t3
LEFT JOIN t4 ON t3.id = t4.id
UNION
SELECT * FROM t3
right JOIN t4 ON t3.id = t4.id