How and why a SQL inner left-right full
We have 2 tables – TableA and TableB. Both the tables have just one column each. TableA has 2 rows and TableB has 3 rows.
To join both these tables, we are using ColumnA in TableA and ColumnB in TableB. The following is the SQL Server interview question.
No matter how you join these 2 tables, the query produces the same result i.e 6 rows – How and Why?
- Inner Join
- Left Outer Join
- Right Outer Join
- Full Outer Join OR
- even Cross Join
SQL Script to create and populate the tables with test data
Create Table TableA
(
ColumnA int
)
Go
Create Table TableB
(
ColumnB int
)
Go
Insert into TableA Values (1)
Insert into TableA Values (1)
Go
Insert into TableB Values (1)
Insert into TableB Values (1)
Insert into TableB Values (1)
Go
Select ColumnA, ColumnB
from TableA
inner join TableB
on TableA.ColumnA = TableB.ColumnB
Select ColumnA, ColumnB
from TableA
left outer join TableB
on TableA.ColumnA = TableB.ColumnB
Select ColumnA, ColumnB
from TableA
right outer join TableB
on TableA.ColumnA = TableB.ColumnB
Select ColumnA, ColumnB
from TableA
full outer join TableB
on TableA.ColumnA = TableB.ColumnB
Select ColumnA, ColumnB
from TableA
cross join TableB
All the above queries return the same row count – 6 rows. How and why all the different types of joins return the same count of rows.
Every row in TableA matches with every row in TableB, so what we get back is a cartesian product i.e the number of rows in TableA multiplied by the number of rows in TableB. So, in essence it’s like a cross join.
TableA has 2 rows and TableB 3 rows. Every row in TableA matches with every row in TableB. So irrespective of the type of join we get the cartesian product 6, i.e 2 rows in TableA multiplied by 3 rows in TableB.
What do you think is the result going to be if we add one more row with a value of 1 to TableB.
Well, the same logic, Cartesian product. 2 rows in TableA multiplied by 4 rows in TableB. So, the answer is 8.
Insert into TableA Values (1)
Go
Execute all the 5 select queries again and you will get 8 rows as the result.
Are you still confused? Let’s look at another example.
Drop both the tables
Drop table TableA
Drop table TableB
Recreate the tables. We now have a second column called SomeValue in both the tables.
Create Table TableA
(
ColumnA int,
SomeValue nvarchar(2)
)
Go
Create Table TableB
(
ColumnB int,
SomeValue nvarchar(2)
)
Go
–Insert test data.
Insert into TableA Values (1, ‘A1’)
Insert into TableA Values (1, ‘A2’)
Go
Insert into TableB Values (1, ‘B1’)
Insert into TableB Values (1, ‘B2’)
Insert into TableB Values (1, ‘B3’)
Go
Now, the select queries. In addition to ColumnA and ColumnB we also want to select SomeValue From TableA. Let’s give it an alias TableASomeValue. Similarly SomeValue column from TableB as well. Let’s call it TableBSomeValue.
Let’s include the same select list on the rest of the 4 queries – that is left join, right join, full join, and cross join.
Select ColumnA, ColumnB, TableA.SomeValue as [TableASomeValue],
TableB.SomeValue as [TableBSomeValue]
from TableA inner join
TableB on TableA.ColumnA = TableB.ColumnB
Select ColumnA, ColumnB, TableA.SomeValue as [TableASomeValue],
TableB.SomeValue as [TableBSomeValue]
from TableA left outer join
TableB on TableA.ColumnA = TableB.ColumnB
Select ColumnA, ColumnB, TableA.SomeValue as [TableASomeValue],
TableB.SomeValue as [TableBSomeValue]
from TableA right outer join
TableB on TableA.ColumnA = TableB.ColumnB
Select ColumnA, ColumnB, TableA.SomeValue as [TableASomeValue],
TableB.SomeValue as [TableBSomeValue]
from TableA full outer join
TableB on TableA.ColumnA = TableB.ColumnB
Select ColumnA, ColumnB, TableA.SomeValue as [TableASomeValue],
TableB.SomeValue as [TableBSomeValue]
from TableA cross join TableBhttps://googleads.g.doubleclick.net/pagead/ads?guci=2.2.0.0.2.2.0.0&client=ca-pub-6328538092426311&output=html&h=280&slotname=1864580310&adk=1584143243&adf=3331659945&pi=t.ma~as.1864580310&w=578&fwrn=4&fwrnh=100&lmt=1613942187&rafmt=1&psa=1&format=578×280&url=https%3A%2F%2Fcsharp-video-tutorials.blogspot.com%2F&flash=0&fwr=0&fwrattr=true&rpe=1&resp_fmts=3&wgl=1&adsid=NT&dt=1613984336835&bpp=2&bdt=270&idt=370&shv=r20210211&cbv=r20190131&ptt=9&saldr=aa&abxe=1&prev_fmts=0x0%2C924x462%2C578x280%2C578x280%2C1349x626%2C578x280&prev_slotnames=9165837384%2C4735637782%2C4735637782&nras=2&correlator=7317280946015&frm=20&pv=1&ga_vid=438601007.1613984317&ga_sid=1613984317&ga_hid=666265902&ga_fc=1&u_tz=360&u_his=3&u_java=0&u_h=768&u_w=1366&u_ah=728&u_aw=1366&u_cd=24&u_nplug=0&u_nmime=0&adx=231&ady=5052&biw=1349&bih=626&scr_x=0&scr_y=2550&eid=21068769%2C21068893&oid=3&psts=AGkb-H8N1jYo8b7WSy_PxawhKVdA8YPoiJ2CV1VBDWvhEiyPzrFfRbEMhsCwyN6FvgQhsC7A5Hy_XPN8kuCIHw%2CAGkb-H9egM2j9UTGC-fpNDnaucIZLgJJJKbcE8PJYhmj-jKn9XEOCctcC4s%2CAGkb-H_mclUTPGdv8NxmdSAbfgHeo0wckSfWMlqY7zDdETBFajwrH6bE-n4%2CAGkb-H-cTM14ihucgA3JWo91WxVlPexLk-4FTvwMcjXXBwmMnQtvOFPR6xvn8eWm2FgUf1YkKMj52YiywZkK4Q%2CAGkb-H97UrNJZb3rFd6CoWmswaW_W3zrL36LWNPP4eKcqVZC0slGlD9XywDGmtGTW0zFleljFKUh1qGpn-gFOkM1gXOIXW7fBi7N6idW%2CAGkb-H-a7pAq-s0xDbtwDUoyGlZQWx2lpEx0Ulgy8d0ALCLymKv5ZSnZ_xqq4-sSKKG0D6eqWOt0OTKT54A&pvsid=3777348724914625&pem=446&rx=0&eae=0&fc=1920&brdim=-8%2C-8%2C-8%2C-8%2C1366%2C0%2C1382%2C744%2C1366%2C626&vis=1&rsz=o%7Co%7CpeEbr%7C&abl=NS&pfx=0&fu=8320&bc=31&ifi=7&uci=a!7&btvi=7&fsb=1&xpc=HBRV8knRqS&p=https%3A//csharp-video-tutorials.blogspot.com&dtd=36864
Execute all the queries. Notice the output.
It takes that first row in TableA, that is the the row which has the value A1 and returns every row in TableB, so we have A1B1, A1B2, A1B3. The same happends even with the second row in TableA. So we have A2B1, A2B2, A2B3.
Response
Thanks for your comment