bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/SQL/SQL Tutorial
SQL•SQL Tutorial

SQL Joins

The SQL JOIN Clause

The JOIN clause is used to combine rows from two or more tables, based on a related column between them.

Here are the different types of JOINs in SQL:

  • (INNER) JOIN : Returns only rows that have matching values in both tables
  • LEFT (OUTER) JOIN : Returns all rows from the left table , and only the matched rows from the right table
  • RIGHT (OUTER) JOIN : Returns all rows from the right table , and only the matched rows from the left table
  • FULL (OUTER) JOIN : Returns all rows when there is a match in either the left or right table

Look at an order in "Orders" table:

OrderIDCustomerIDOrderDate
1030821996-09-18

Then, look at a customer in the "Customers" table:

CustomerIDCustomerNameContactNameCountry
2Ana Trujillo Emparedados y heladosAna TrujilloMexico

Here we see that the "CustomerID" column in the "Orders" table refers to the "CustomerID" in the "Customers" table. The relationship between the two tables above is the "CustomerID" column.

Then, we can create the following SQL statement (that contains an INNER JOIN ), that selects records that have matching values in both tables:

Example

 SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;

and it will produce something like this:

OrderIDCustomerNameOrderDate
10308Ana Trujillo Emparedados y helados1996-09-18
10365Antonio Moreno Taquería1996-11-27
10383Around the Horn1996-11-15
10355Around the Horn1996-12-16
10278Berglunds snabbköp1996-12-16

Previous

SQL Aliases

Next

SQL INNER JOIN

This chapter

Overview
46

Lessons

115m

Read time

1. SQL Tutorial2. Introduction to SQL3. SQL Syntax4. SQL SELECT Statement5. SQL SELECT DISTINCT Statement6. SQL WHERE Clause7. SQL ORDER BY Keyword8. SQL AND Operator9. SQL OR Operator10. SQL NOT Operator11. SQL INSERT INTO Statement12. SQL NULL Values13. SQL UPDATE Statement14. SQL DELETE Statement15. SQL SELECT TOP, LIMIT and FETCH FIRST16. SQL Aggregate Functions17. SQL MIN() F unction18. SQL MAX() Function19. SQL COUNT() Function20. SQL SUM() Function21. SQL AVG() Function22. SQL LIKE Operator23. SQL Wildcards24. SQL IN Operator25. SQL BETWEEN Operator26. SQL Aliases27. SQL Joins28. SQL INNER JOIN29. SQL LEFT JOIN30. SQL RIGHT JOIN31. SQL FULL JOIN32. SQL Self Join33. SQL UNION Operator34. SQL UNION ALL Operator35. SQL GROUP BY Statement36. SQL HAVING Clause37. SQL EXISTS Operator38. SQL ANY Operator39. SQL ALL Operator40. SQL SELECT INTO Statement41. SQL INSERT INTO SELECT Statement42. SQL CASE Expression43. SQL NULL Functions44. SQL Stored Procedures45. SQL Comments46. SQL Operators