0% found this document useful (0 votes)
14 views

SQL Join Queries

SQL joins are used to combine rows from two or more tables based on a common field. The most common type of join is an inner join, which returns all rows from multiple tables where the join condition is met. Different types of joins include inner joins, left joins, right joins, and full joins. Left joins return all rows from the left table with matching rows from the right table, while right joins return all rows from the right table with any matches from the left table.

Uploaded by

Sofia Arquero
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

SQL Join Queries

SQL joins are used to combine rows from two or more tables based on a common field. The most common type of join is an inner join, which returns all rows from multiple tables where the join condition is met. Different types of joins include inner joins, left joins, right joins, and full joins. Left joins return all rows from the left table with matching rows from the right table, while right joins return all rows from the right table with any matches from the left table.

Uploaded by

Sofia Arquero
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

SQL joins are used to combine rows from two or more tables.

SQL JOIN
An SQL JOIN clause is used to combine rows from two or more tables, based on a
common field between them.

The most common type of join is: SQL INNER JOIN (simple join). An SQL INNER
JOIN return all rows from multiple tables where the join condition is met.

Let's look at a selection from the "Orders" table:

et's look at a selection from the "Orders" table:

OrderID(PK) CustomerID OrderDate


1 01 1996-09-18

2 02 1996-09-19

2 03 1996-09-20

Then, have a look at a selection from the "Customers" table:

CustomerID(PK) CustomerName ContactName Country


1 Alfreds Futterkiste Maria Anders Germany

2 Ana Trujillo Emparedados y helados Ana Trujillo Mexico

3 Antonio Moreno Taquería Antonio Moreno Mexico

Notice that the "CustomerID" column in the "Orders" table refers to the customer in the
"Customers" table. The relationship between the two tables above is the "CustomerID"
column.

Then, if we run the following SQL statement (that contains an INNER JOIN):

Example
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;
it will produce something like this:

OrderID CustomerName OrderDate

10308 Ana Trujillo Emparedados y helados 9/18/1996

10365 Antonio Moreno Taquería 11/27/1996

10383 Around the Horn 12/16/1996

10355 Around the Horn 11/15/1996

10278 Berglunds snabbköp 8/12/1996

Different SQL JOINs


Before we continue with examples, we will list the types the different SQL JOINs you
can use:

 INNER JOIN: Returns all rows when there is at least one match in BOTH tables
 LEFT JOIN: Return all rows from the left table, and the matched rows from the
right table
 RIGHT JOIN: Return all rows from the right table, and the matched rows from
the left table
 FULL JOIN: Return all rows when there is a match in ONE of the tables

SQL LEFT JOIN Keyword


The LEFT JOIN keyword returns all rows from the left table (table1), with the matching
rows in the right table (table2). The result is NULL in the right side when there is no
match.

SQL LEFT JOIN Syntax


SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
or:
SELECT column_name(s)
FROM table1
LEFT OUTER JOIN table2
ON table1.column_name=table2.column_name;
PS! In some databases LEFT JOIN is called LEFT OUTER JOIN.

LEFT JOIN

SELECT Customers.CustomerName, Orders.OrderID


FROM Customers
LEFT JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;

SQL RIGHT JOIN Keyword


The RIGHT JOIN keyword returns all rows from the right table (table2), with the
matching rows in the left table (table1). The result is NULL in the left side when there is
no match.

SQL RIGHT JOIN Syntax


SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;

or:

SELECT column_name(s)
FROM table1
RIGHT OUTER JOIN table2
ON table1.column_name=table2.column_name;

PS! In some databases RIGHT JOIN is called RIGHT OUTER JOIN.

You might also like