
To start our analysis, please, refer to the Venn diagram.

Which are these 6 rows that make the difference? That’s all that needs to be modified in the code.Īfter executing, the query returned 26 rows, as shown in the picture below!Ħ rows more than the 20 rows we obtained in the example about INNER JOINS.
Postgresql replace null with 0 left join code#
This is what the code would look like: SELECTĭepartments_dup d ON m.dept_no = d.dept_no Instead of relating the two tables through the keyword JOIN, or INNER JOIN, we must type LEFT JOIN. However, the JOIN syntax will be subject to a slight change. We can write similar code to the one we would use with the INNER JOIN. The INSERT statement is discussed in the following tutorial.Īfter running the query, everything looks good. In order to add them back, try coding: INSERT INTO dept_manager_dup Technically, after deleting the rows in which we see the respective employee and department numbers, MySQL will delete all initial records and all their duplicates. If you are not sure how the DELETE statement works, the linked tutorial may well shed some light on the subject.ĭuplicate values inflate our output with repeating records and do not contribute to the results we obtain meaningfully. We can achieve that by writing the following code: DELETE FROM dept_manager_dup First, let’s remove the duplicates from the two tables. So, if you’ve read the tutorial on the duplicate records, there are a few things we want to delete. To clarify the concept, let’s look at an application. In SQL terms, this translates to retrieving all matching values of the two tables plus all values from the left table that do not match any values from the right table. That’s why, compared to the INNER JOIN, the result set, coloured in red, includes the rest of the area of the left table.

Its output allows us to see all records from the table on the left side of the JOIN, including all matching rows of the two tables. The Venn diagram you see in the picture below, allows you to visualize how a LEFT JOIN in SQL works. Side note: If you don’t have the ‘ employees’ database, you can download it here. You can learn all this and more in our SQL course. Moreover, we will be handling duplicate records, so make sure you know a thing or two about it.
Postgresql replace null with 0 left join how to#
If you have any idea what JOINS are and you are familiar with the INNER JOIN type, learning how to use the LEFT JOIN in SQL should be a walk in the park. Martin Ganchev 12 min read How does a LEFT JOIN in SQL work?
