DSMP 2022-23 SQL Joins
DSMP 2022-23 SQL Joins
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.
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.
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.
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.
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.
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.
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.