ON employees.id = jan_bonuses.employee_id Īs expected, the result includes all employees. Our result should include all employees, no matter if they received a bonus or not. We want to join these tables so we can see who received bonuses in January. We have a table with employees info and want to match it with the table that lists all the bonuses paid in January ( jan_bonuses). SQL LEFT JOINs In Use Example with employees And now let’s look into some more examples of working with LEFT JOINs. Simple, isn’t it? You can practice LEFT JOINs in this interactive SQL JOINs course. If there is no match, it still keeps the row from the left table and puts NULL in the corresponding columns of the right table (customer #2 in our example). If there are several matches (like in our case with customer #3), it duplicates the row in the left table to include all records from the right table. If there is a match, it adds data from the right table to the corresponding row of the left table. Take a look at the animation: How does LEFT JOIN work?įirst of all, the database looks into each row of the left table and searches for a match in the right table based on the related columns. In this query, we also use ORDER BY to order the output by customer’s ID, but this is optional.Īfter running this query, you’ll get the following table. (Here, it’s id from customers and customer_id from orders).
0 Comments
Leave a Reply. |