0% found this document useful (0 votes)
27 views16 pages

DSMP 2022-23 SQL Joins

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views16 pages

DSMP 2022-23 SQL Joins

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

What are SQL joins

20 February 2023 15:28

In SQL (Structured Query Language), a join is a way to combine data


from two or more database tables based on a related column between
them.

Joins are used when we want to query information that is distributed


across multiple tables in a database, and the information we need is
not contained in a single table. By joining tables together, we can
create a virtual table that contains all of the information we need for
our query.

SQL Joins Page 1


But why have data in multiple tables?
20 February 2023 15:28

SQL Joins Page 2


Types of Joins
20 February 2023 15:30

SQL Joins Page 3


Cross Joins -> Cartesian Products
20 February 2023 15:29

In SQL, a cross join (also known as a Cartesian product) is a type of join that
returns the Cartesian product of the two tables being joined. In other words, it
returns all possible combinations of rows from the two tables.

Cross joins are not commonly used in practice, but they can be useful in certain
scenarios, such as generating test data or exploring all possible combinations of
items in a product catalogue. However, it's important to be cautious when using
cross joins with large tables, as they can generate a very large result set, which can
be resource-intensive and slow to process.

SQL Joins Page 4


Inner Joins
20 February 2023 15:30

In SQL, an inner join is a type of join operation that combines data from two or
more tables based on a specified condition. The inner join returns only the rows
from both tables that satisfy the specified condition, i.e., the matching rows.

When you perform an inner join on two tables, the result set will only contain rows
where there is a match between the joining columns in both tables. If there is no
match, then the row will not be included in the result set.

SQL Joins Page 5


Left Join
20 February 2023 15:30

A left join, also known as a left outer join, is a type of SQL join operation that
returns all the rows from the left table (also known as the "first" table) and
matching rows from the right table (also known as the "second" table). If there are
no matching rows in the right table, the result will contain NULL values in the
columns that come from the right table.

In other words, a left join combines the rows from both tables based on a common
column, but it also includes all the rows from the left table, even if there are no
matches in the right table. This is useful when you want to include all the records
from the first table, but only some records from the second table.

SQL Joins Page 6


Right Join
20 February 2023 15:31

A right join, also known as a right outer join, is a type of join operation in SQL that
returns all the rows from the right table and matching rows from the left table. If
there are no matches in the left table, the result will still contain all the rows from
the right table, with NULL values for the columns from the left table.

SQL Joins Page 7


Full Outer Join
20 February 2023 15:31

A full outer join, sometimes called a full join, is a type of join operation in SQL that
returns all matching rows from both the left and right tables, as well as any non-
matching rows from either table. In other words, a full outer join returns all the
rows from both tables and matches rows with common values in the specified
columns, and fills in NULL values for columns where there is no match.

SQL Joins Page 8


SQL Set Operations
20 February 2023 15:35

1. UNION: The UNION operator is used to combine the results of two or more SELECT
statements into a single result set. The UNION operator removes duplicate rows
between the various SELECT statements.
2. UNION ALL: The UNION ALL operator is similar to the UNION operator, but it does
not remove duplicate rows from the result set.
3. INTERSECT: The INTERSECT operator returns only the rows that appear in both
result sets of two SELECT statements.
4. EXCEPT: The EXCEPT or MINUS operator returns only the distinct rows that appear
in the first result set but not in the second result set of two SELECT statements.

SQL Joins Page 9


SQL Joins Page 10
Self Joins
20 February 2023 15:34

A self join is a type of join in which a table is joined with itself. This
means that the table is treated as two separate tables, with each row in
the table being compared to every other row in the same table.

Self joins are used when you want to compare the values of two
different rows within the same table. For example, you might use a self
join to compare the salaries of two employees who work in the same
department, or to find all pairs of customers who have the same billing
address.

SQL Joins Page 11


Joining on more than one cols
20 February 2023 16:58

SQL Joins Page 12


Joining more than 2 tables
20 February 2023 15:33

1. Find order name and corresponding category name

SQL Joins Page 13


Filtering Columns
20 February 2023 15:31

1. Find order_id, name and city by joining users and orders.


2. Find order_id, product category by joining order_details and
category

SQL Joins Page 14


Filtering Rows
20 February 2023 15:32

1. Find all the orders placed in pune


2. Find all orders under Chairs category

SQL Joins Page 15


Practice Questions
20 February 2023 15:35

1. Find all profitable orders


2. Find the customer who has placed max number of orders
3. Which is the most profitable category
4. Which is the most profitable state
5. Find all categories with profit higher than 5000

SQL Joins Page 16

You might also like