본문 바로가기
Issue Note

Exclusion of NULL values in b.customer_id column in SQL query [SQL]

by jhleeatl 2024. 3. 22.

I had a question about the above SQL code. Looking at the code, the query 'where b.customer_id is not null' is searching for non-null values in the customer_id column. However, there are no null values in the b.customer_id column, and null values in b.name and b.age are excluded. I was curious about the reason for this.

 

select a.order_id,
       a.customer_id,
       a.restaurant_name,
       a.price,
       b.name,
       b.age,
       b.gender
from food_orders a left join customers b on a.customer_id=b.customer_id
where b.customer_id is not null

 

 

The condition "b.customer_id is not null" is used to select rows in the "food_orders" table where the "customer_id" column is not NULL. This does not mean rows in the "customers" table where the "customer_id" column is not NULL, but rather it signifies rows in the joined result where the "customer_id" column of the "food_orders" table is not NULL.

 

Therefore, the condition "b.customer_id is not null" selects only rows from the join result of the "food_orders" table and the "customers" table where the "customer_id" is not NULL. Hence, rows with NULL values in the "b.name" and "b.age" columns are excluded from the resulting set.