LEFT JOIN is a keyword that selects all records from the left table, but returns only records from the right table that match criteria
Syntax
SELECT * FROM Procuts
LEFT JOIN Categories ON Products.CategoryId = Categories.CategoryId Example
In the example below, when using LEFT JOIN keyword, a database will return all products and only categories that match the condition
Products Table
| ProductID | ProductName | CategoryID | Price |
|---|---|---|---|
| 1 | Chais | 1 | 18 |
| 2 | Chang | 1 | 19 |
| 3 | Aniseed Syrup | 10 | 10 |
| Category Table |
| CategoryID | CategoryName | Description |
|---|---|---|
| 1 | Beverages | Soft drinks, coffees, teas, beers, and ales |
| 2 | Condiments | Sweet and savory sauces, relishes, spreads, and seasonings |
| 3 | Confections | Desserts, candies, and sweet breads |
| Joined Table |
| ProductId | ProductName | CategoryId | Price | Category Name | Description |
|---|---|---|---|---|---|
| 1 | Chais | 1 | 18 | Beverges | Soft drinks, coffees, teas, beers, and ales |
| 2 | Chang | 1 | 19 | Bevereges | Soft drinks, coffees, teas, beers, and ales |
| 3 | Ainseed Syrup | 10 | 10 | NULL | NULL |