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

SQL Joins

d

Uploaded by

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

SQL Joins

d

Uploaded by

rishij043876
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

SQL JOINS

Customers Table:

CustomerID Name

1 Alice

2 Bob

3 Charlie

4 David

Orders Table:

OrderID CustomerID Product

101 1 Laptop

102 2 Phone

103 1 Tablet

104 3 Headphones

1. INNER JOIN

Definition:

An INNER JOIN returns only the rows that have matching values in both tables.

Example:

SELECT Customers.Name, Orders.Product

FROM Customers

INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

Output:

Name Product

Alice Laptop

Alice Tablet

Bob Phone

Charlie Headphones

 Explanation:
o Shows customers who have made orders (Alice, Bob, and Charlie).

o David is not shown because he hasn’t made any orders.

2. LEFT JOIN (LEFT OUTER JOIN)

Definition:

A LEFT JOIN returns all rows from the left table, and the matching rows from the right table. If there’s
no match, NULL is returned for columns from the right table.

Example:

SELECT Customers.Name, Orders.Product

FROM Customers

LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

Output:

Name Product

Alice Laptop

Alice Tablet

Bob Phone

Charlie Headphones

David NULL

 Explanation:

o All customers are shown, even if they didn’t place an order.

o David is shown with NULL in the Product column because he has no orders.

3. RIGHT JOIN (RIGHT OUTER JOIN)

Definition:

A RIGHT JOIN returns all rows from the right table, and the matching rows from the left table. If
there’s no match, NULL is returned for columns from the left table.

Example:

SELECT Customers.Name, Orders.Product

FROM Customers

RIGHT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

Output:
Name Product

Alice Laptop

Alice Tablet

Bob Phone

Charlie Headphones

 Explanation:

o All orders are shown, and the customer names are filled in if they exist.

o There are no NULL rows in the Product column since every order has a customer.

4. FULL JOIN (FULL OUTER JOIN)

Definition:

A FULL JOIN returns all rows from both tables, filling in NULL where there’s no match.

Example:

SELECT Customers.Name, Orders.Product

FROM Customers

FULL JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

Output:

Name Product

Alice Laptop

Alice Tablet

Bob Phone

Charlie Headphones

David NULL

 Explanation:

o All customers and all orders are shown.

o David is shown with NULL in the Product column since he didn’t make any orders.

5. CROSS JOIN

Definition:
A CROSS JOIN returns the Cartesian product of both tables, meaning it combines every row from the
first table with every row from the second table.

Example:

SELECT Customers.Name, Orders.Product

FROM Customers

CROSS JOIN Orders;

Output:

Name Product

Alice Laptop

Alice Phone

Alice Tablet

Alice Headphones

Bob Laptop

Bob Phone

Bob Tablet

Bob Headphones

Charlie Laptop

Charlie Phone

Charlie Tablet

Charlie Headphones

David Laptop

David Phone

David Tablet

David Headphones

 Explanation:

o Combines every customer with every order, even though there is no matching
condition.

o This creates a large number of combinations (Cartesian product).


6. SELF JOIN

Definition:

A SELF JOIN is a join where a table is joined with itself. It’s used to compare rows within the same
table.

Example:

SELECT A.Name AS Customer, B.Name AS Customer2

FROM Customers A

JOIN Customers B ON A.CustomerID != B.CustomerID;

Output:

Customer Customer2

Alice Bob

Alice Charlie

Alice David

Bob Alice

Bob Charlie

Bob David

Charlie Alice

Charlie Bob

Charlie David

David Alice

David Bob

David Charlie

 Explanation:

o This query compares every customer with every other customer (excluding
themselves).

o It shows all possible combinations of customers.

7. NATURAL JOIN

Definition:
A NATURAL JOIN automatically joins tables based on columns that have the same name and data
type. You don’t need to specify the join condition.

Example:

SELECT Customers.Name, Orders.Product

FROM Customers

NATURAL JOIN Orders;

Output:

Name Product

Alice Laptop

Alice Tablet

Bob Phone

Charlie Headphones

 Explanation:

o The NATURAL JOIN automatically uses the CustomerID to join the tables.

o It works like an INNER JOIN, but you don’t need to specify the join condition
explicitly.

You might also like