SQL LEFT JOIN
- LEFT JOIN performs a join starting with the first (left-most) table and then any matching second (right-most) table records.
- LEFT JOIN and LEFT OUTER JOIN are the same.
The SQL LEFT JOIN syntax
The general syntax is:
- SELECT column-names FROM table-name1 LEFT JOIN table-name2 ON column-name1 = column-name2 WHERE condition
SQL LEFT JOIN Examples
1) Example of Left Outer join
mysql> select customers.customername, orders.orderid from customers left join orders on customers.customerid = orders.customerid;
+---------------+---------+
| customername | orderid |
+---------------+---------+
| Mike Wooksahi | 10308 |
| Subham Ball | NULL |
| jon Wooksahi | NULL |
+---------------+---------+
3 rows in set (0.00 sec)
2) Example of Left Outer join with order by Clause
mysql> select customers.customername, orders.orderid from customers left join orders on customers.customerid = orders.customerid order by customers.customername;
+---------------+---------+
| customername | orderid |
+---------------+---------+
| jon Wooksahi | NULL |
| Mike Wooksahi | 10308 |
| Subham Ball | NULL |
+---------------+---------+
3 rows in set (0.00 sec)
mysql> select customers.customername, orders.orderid from customers left join orders on customers.customerid = orders.customerid;
+---------------+---------+
| customername | orderid |
+---------------+---------+
| Mike Wooksahi | 10308 |
| Subham Ball | NULL |
| jon Wooksahi | NULL |
+---------------+---------+
3 rows in set (0.00 sec)
2) Example of Left Outer join with order by Clause
mysql> select customers.customername, orders.orderid from customers left join orders on customers.customerid = orders.customerid order by customers.customername;
+---------------+---------+
| customername | orderid |
+---------------+---------+
| jon Wooksahi | NULL |
| Mike Wooksahi | 10308 |
| Subham Ball | NULL |
+---------------+---------+
3 rows in set (0.00 sec)
Comments
Post a Comment