When doing an INNER JOIN, are columns matched on NULL values?

Question

In the context of this exercise, when doing an INNER JOIN, are columns matched on NULL values?

Answer

No, when you have NULL values in a column, these are never matched to other NULL values. This is because NULL signifies the absence of any value, and cannot be compared as they will never equal anything. Doing say NULL = NULL results in False.

Let’s take for example the animation given in the exercise, which shows how INNER JOIN works. Let’s say that an additional row was added to each table, with NULL in the C2 column, such that they become

Left table:

C1, C2
A, B
Q, W
X, Y
T, NULL

Right table:

C2, C3
B, C
E, R
Y, Z
NULL, V

If we inner joined these tables the same way, we would end up with the same result, because NULL values are not matched.

C1, C2, C3
A, B, C
X, Y, Z
9 Likes

There is no difference between the JOIN and INNER JOIN commands, is there?

10 Likes

Hey, correct. There is no different.

5 Likes

Is it necessary to use ON command whenever we are joining two tables?
And why is the subscription_id column showing twice in the resulsts? Can we do something to make it appear only once, while also joining the two tables ON subscription_id ?

4 Likes

The reason why subscription_id is showing twice is that a SELECT * was done, and as subscription_id column exists in both table, one of the columns is for the first table (orders) and the other column is for the 2nd table (subscriptions).
To prevent from having both columns show up, one would have to specify which columns to show. For example: SELECT orders.subscription_id, subscriptions.description FROM orders JOIN ON etc This will only return two columns.

12 Likes

Basically INNER JOIN works like this
suppose you have two tables TABLE1 and TABLE2.
It select TABLE1 first row and match with TABLE2 rows one by one . If our ON condition is matched it creates new entry .
Then it goes for TABLE2 second row and match with TABLE2 all rows and so on.
if ON condition matches it create new entry otherwise ignore the result .
so we only have matched condtion result .
There all more join statements also like LEFT JOIN , RIGHT JOIN etc.
That’s work on same concept but minor diffrence they don’t ignore the result instead they add NULL in result if ON condition not matched.

5 Likes

if your ON condition match both tables. then instead of ON you can use USING keyword.

exaple:

SELECT column_list
FROM table_1
JOIN table_2
ON join_condition;

SELECT column_list
FROM table_1
JOIN table_2
USING (column_name); // column name must be same in both tables

19 Likes

Thanks for this! It helped me in learning new command!!

4 Likes

Very useful:
just tested it and helped simplify the code quite a bit.

I went from this:

SELECT 
  orders.subscription_id,
  subscriptions.description
FROM orders
JOIN subscriptions
  ON orders.subscription_id
  = subscriptions.subscription_id
WHERE subscriptions.description = 'Fashion Magazine';

To this:

SELECT
  subscription_id,
  description
FROM orders
JOIN subscriptions
  USING (subscription_id)
WHERE description = 'Fashion Magazine';

Thanks!

9 Likes

Thanks for simplifying. In addition to it, if you type like this:

SELECT * FROM orders

JOIN subscriptions

USING(subscription_id)

WHERE description = 'Fashion Magazine';

It will only print the subscription_id column one time.

3 Likes

I really don’t get this. I mean i cannot understand what exactly the Inner Join does :thinking:. Anyone could explain me please. I would really appreciate that.

1 Like

Hey man!
It’s been a while since I’ve done some SQL, but I saved this GIF because I believed it really helps illustrate how Inner Join works:

inner-join

It really just combines two tables on a matching value and returns the combined result.

They use it a lot to combine different tables on transactional sites, such as a client’s information and their orders being separated into two tables, then performing an Inner Join on both the see the client’s info and their order history by joining on their, let’s say, client ID. This allows to keep data into different files and then joining them when necessary to perform analysis. Let me know if that helps!

11 Likes

Thanks man! I appreciate it. It was very helpful :grinning:

1 Like

No problem!
Here, we don’t have much support, so I’m trying to help out others when I can.

Don’t hesitate if anything else comes up.

6 Likes

INNER JOIN is a defaul when you just type only JOIN.
there are LEFT JOIN , RIGHT JOIN, FULL JOIN (some database manage system don’t have full join, like mySQL)

1 Like

VERY USEFUL. thank you so much.

Indeed, The USING clause can be simpler to write when the join condition involves only one column with the same name in both tables.

On the other hand, it would be important to mention that not all DBMS support the USING clause. For instance, SQL Server does not support USING, so you would have to use the ON clause instead.

thank you. very helpful visuals, much appreciated.

Visual JOIN - I found this is a very useful website with illustrations to help me understand INNER JOIN/LEFT JOIN/RIGHT JOIN/OUTER JOIN. I hope it helps you too.