SQL LEFT JOIN Keyword
\\n\\nLEFT JOIN is a join keyword in SQL used to retrieve data from multiple tables.
\\n\\nThe difference between LEFT JOIN and INNER JOIN is that LEFT JOIN returns all records from the left table, even if there are no matching records in the right table.
\\n\\nThe LEFT JOIN keyword returns all rows from the left table (table1), even if there is no match in the right table (table2). If there is no match in the right table, the result is NULL.
\\n\\nSQL LEFT JOIN Syntax
\\n\\nSELECT column_name(s) FROM table1 LEFT JOIN table2 ON table1.column_name=table2.column_name;\\n\\nor:\\n\\nSELECT column_name(s) FROM table1 LEFT OUTER JOIN table2 ON table1.column_name=table2.column_name;\\n\\nNote: In some databases, LEFT JOIN is referred to as LEFT OUTER JOIN.\\n\\n- \\n
- table1: Left table (primary table), all records from this table will be retained by
LEFT JOIN. \\n - table2: Right table (secondary table), if no matching data exists, corresponding columns are filled with
NULL. \\n - ON table1.column_name=table2.column_name: Specifies the join condition, usually a common field between the two tables. \\n
Characteristics:
\\n- \\n
- Returns all records from the left table, even if there is no matching data in the right table. \\n
- If there is no matching record in the right table, the corresponding fields in the result will be
NULL. \\n
Suppose we have two tables: Customers and Orders.
\\n\\nCustomers Table:
\\n\\n| CustomerID | \\nName | \\n
|---|---|
| 1 | \\nAlice | \\n
| 2 | \\nBob | \\n
| 3 | \\nCharlie | \\n
| 4 | \\nDavid | \\n
Orders Table:
\\n\\n| OrderID | \\nCustomerID | \\nProduct | \\n
|---|---|---|
| 101 | \\n1 | \\nLaptop | \\n
| 102 | \\n2 | \\nSmartphone | \\n
Using LEFT JOIN Query:
\\n\\nSELECT Customers.Name, Orders.Product FROM Customers LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;\\n\\n\\nQuery Output Result:
\\n\\n| Name | \\nProduct | \\n
|---|---|
| Alice | \\nLaptop | \\n
| Bob | \\nSmartphone | \\n
| Charlie | \\nNULL | \\n
| David | \\nNULL | \\n
Explanation:
\\nLEFT JOIN returns all records from the Customers table. For Charlie and David, since there is no matching CustomerID in the Orders table, their corresponding Product columns are NULL.
\\n\\n
Demo Database
\\n\\nIn this tutorial, we will use the sample database.
\\n\\nHere is some data selected from the "Websites" table:
\\n\\n+----+--------------+---------------------------+-------+---------+\\n| id | name | url | alexa | country |\\n+----+--------------+---------------------------+-------+---------+\\n| 1 | Google | https://www.google.cm/ | 1 | USA |\\n| 2 | Taobao | https://www.taobao.com/ | 13 | CN |\\n| 3 | | | 4689 | CN |\\n| 4 | Weibo | http://weibo.com/ | 20 | CN |\\n| 5 | Facebook | https://www.facebook.com/ | 3 | USA |\\n| 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND |\\n+----+---------------+---------------------------+-------+---------+\\n\\n\\nHere is the data from the "access_log" website access log table:
\\n\\nmysql> SELECT * FROM access_log;\\n+-----+---------+-------+------------+\\n| aid | site_id | count | date |\\n+-----+---------+-------+------------+\\n| 1 | 1 | 45 | 2016-05-10 |\\n| 2 | 3 | 100 | 2016-05-13 |\\n| 3 | 1 | 230 | 2016-05-14 |\\n| 4 | 2 | 10 | 2016-05-14 |\\n| 5 | 5 | 205 | 2016-05-14 |\\n| 6 | 4 | 13 | 2016-05-15 |\\n| 7 | 3 | 220 | 2016-05-15 |\\n| 8 | 5 | 545 | 2016-05-16 |\\n| 9 | 3 | 201 | 2016-05-17 |\\n+-----+---------+-------+------------+\\n9 rows in set (0.00 sec)\\n\\n\\n\\n\\n
SQL LEFT JOIN Example
\\n\\nThe following SQL statement will return all websites and their visit counts (if any).
\\n\\nIn this example, we use Websites as the left table and access_log as the right table:
\\n\\nExample
\\n\\nSELECT Websites.name, access_log.count, access_log.date\\n FROM Websites\\n LEFT JOIN access_log\\n ON Websites.id=access_log.site_id\\n ORDER BY access_log.count DESC;\\n\\n\\nExecuting the above SQL produces the following output:
\\n\\nNote: The LEFT JOIN keyword returns all rows from the left table (Websites), even if there is no match in the right table (access_log).
YouTip