MySQL LEFT JOIN

Download as pdf
Download as pdf
You are on page 1of 9
9116723, 10:08 AM Understanding MySQL. LEFT JOIN Clause By Examples IW mysqutuTorIAL MySQL LEFT JOIN Summary: in this tutorial, you will learn about MySQL LEFT 301N clause and how to apply it to query data from two or more tables. Introduction to MySQL LEFT JOIN clause The LEFT 30IN allows you to query data from two or more tables. Similar to the INNER JOIN (https://tom.nysqltutorial .org/ysql-inner-join.aspx) Clause, the LEFT JOIN is an optional clause of the SELECT (netps://wme.nysqitutorial .org/nysal-select-statenert-query-data.aspx) statement, which appears immediately after the From clause. Suppose that you want to join two tables t1 and 2 The following statement shows how to use the LEFT JOIN clause to join the two tables: SELECT FROM LEFT JOIN t2 ON When you use the Lert 30zN clause, the concepts of the left table and the right table are introduced. In the above syntax, t1 is the left table and *2_is the right table. The LEFT JOIN clause selects data starting from the left table ( t1 ). It matches each row from the left table ( t1 ) with every row from the right table( 2) based on the join_condition If the rows from both tables cause the join condition evaluates to true , the LEFT JOIN combine columns of rows from both tables to a new row and includes this new row in the result rows. ites hwo. mysatatorial orgimysalettjoin aspx 19 9116723, 10:08 AM Understanding MySQL. LEFT JOIN Clause By Examples In case the row from the left table ( t1 ) does not match with any row from the right table( ¢2 ), the LEFT JOIN still combines columns of rows from both tables into a new row and includes the new row in the result rows. However, it uses NULL for all the columns of the row from the right table. In other words, Lert 30zN returns all rows from the left table regardless of whether a row from the left table has a matching row from the right table or not. If there is no match, the columns of the row from the right table will contain NULL The following Venn diagram helps you visualize how the LEFT JOIN clause works: MySQL LEFT JOIN - Venn Diagram MySQL LEFT JOIN clause examples Let's take some examples of using the LeFT Jon clause, 1) Using MySQL LEFT JOIN clause to join two tables See the following tables customers and orders in the sample database (https//Awww mysqltutorial.org/mysal-sample-database aspx) ites hwo. mysatatorial orgimysalettjoin aspx 29 9116723, 10:08 AM Understanding MySQL. LEFT JOIN Clause By Examples Each customer can have zero or more orders while each order must belong to one customer. This query uses the LEFT JOIN clause to find all customers and their orders: SELECT customers Number, customerNam orderNumber status FROM LEFT JOIN orders ON orders. customerNumber = customers. customerNumbi Alternatively, you can save some typing by using table aliases (hetpsi/muw.mysqltutoriaLorg/mysql-alias/) SELECT customerName, orderNumber status FROM custom tps hwo mysqtatoria orgimysal 39 9116723, 10:08 AM Understanding MySQL. LEFT JOIN Clause By Examples LEFT JOIN on In this example: © The customers is the left table and orders is the right table. © The Lert 30rN clause returns all customers including the customers who have no order. If a customer has no order, the values in the column orderNunber and status are NULL . Because both table customers and orders have the same column name (_customerNunber ) in the join condition with the equal operator, you can use the USING syntax as follows SELECT status FROM LEFT JOIN USING The following clauses are equivalent: And ites hwo. mysatatorial orgimysalettjoin aspx 49 9116723, 10:08 AM Understanding MySQL. LEFT JOIN Clause By Examples Ifyou replace the LEFT JOIN clause by the INNER JOIN (httos://twm.nysqltutorial.org/nysai-inner- 4join.asps) clause, you will get the only customers who have at least one order. 2) Using MySQL LEFT JOIN clause to find unmatched rows The LEFT 30IN clause is very useful when you want to find rows in a table that doesn’t have a matching row from another table. The following example uses the LEFT 30IN to find customers who have no order: SELECT FROM LEFT JOIN on WHERE TS NULL 3) Using MySQL LEFT JOIN to join three tables ites hwo. mysatatorial orgimysalettjoin aspx 59 9116723, 10:08 AM Understanding MySQL. LEFT JOIN Clause By Examples See the following three tables employees , customers ,and payments : This example uses two LEFT JOIN clauses to join the three tables: payments . SELECT lastName, fir customerNane, checkNunber, amount FROM employees LEFT JOIN customers ON employeeNumber = salesRepEmployeeNumber LEFT JOIN payments ON payments. customerNumber = customers .customerNunber ORDER BY customerName, checkNumber; This picture shows the partial output: tps Awww. mysatstorialorgimysa employees , customers , and 69 9116723, 10:08 AM Understanding MySQL. LEFT JOIN Clause By Examples How it works. © The first LEFT 201N returns all employees and customers who represented each employee or NULL. if the employee does not in charge of any customer. * The second LeFT J0IN returns payments of each customer represented by an employee or NULL if the customer has no payment. Condition in WHERE clause vs. ON clause See the following example. SELECT FROM LEFT JOIN usING ites hwo. mysatatorial orgimysalettjoin aspx 79 9116723, 10:08 AM Understanding MySQL. LEFT JOIN Clause By Examples WHERE 10123 This example used the LEFT JOTN clause to query data from the tables orders and orderbetails . The query returns the order and its line items of the order number 10123 However, if you move the condition from the WHERE (nttps://sm.rysaitutortal.org/sysql-where/) clause to the on clause SELECT FROM LEFT JOIN on AND 10123 Itwill have a different meaning. In this case, the query returns all orders but only the order 10123 will have line items associated with it asin the following picture ites hwo. mysatatorial orgimysalettjoin aspx 89 9116723, 10:08 AM Understanding MySQL. LEFT JOIN Clause By Examples Notice that for INNER JOIN (hetps://mw.nysqltutorial .org/nysql-inner-join.aspx) Clause, the condition in the oN clause is equivalent to the condition in the WHERE clause, In this tutorial, you have learned how to use the MySQL LEFT JOIN clause to join data from two or more tables. ites hwo. mysatatorial orgimysalettjoin aspx 99

You might also like