What is a join in SQL

LEFT JOIN works in a similar way to INNER JOIN with the difference that entries in the left table do not have to have a connection to the data in the right table in order to be selected.

short: Select everything from the table on the left, even if there is no matching value in the table on the right.

LEFT JOIN syntax

SELECT * FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name

We want to illustrate the effects of LEFT JOIN with the example of combining JOIN tables.

INNER JOIN

SELECT * FROM users JOIN questions ON users.id = questions.user_id

LEFT JOIN

SELECT * FROM users LEFT JOIN questions ON users.id = questions.user_id

In contrast to the INNER JOIN, for which the linking value must be present in both tables, this is the case with LEFT JOIN not the case. At the LEFT JOIN all values ​​of the table on the left are packed into the result set, even if there is no matching value in the table on the right.

Here is another suitable example. We want to select all users who have not yet asked a question.

SELECT users.username FROM users LEFT JOIN questions ON users.id = questions.user_id WHERE questions.user_id IS NULL

Since the LEFT JOIN Result Set creates a table in which users are also listed who have not asked a question, we can use IS NULL check whether a particular column has the value ZERO Has (if there is no value, NULL is automatically set). In this way, we pull the NULL lines from the LEFT JOIN and thus get the users who have not yet asked a question.