Unlocking the World of Business Intelligence with SQLBI

In SQL, JOINs are used to combine rows from two or more tables based on a related column between them. There are different types of joins in SQL that allow you to fetch data in various ways depending on how you want to combine the tables. These include:
The INNER JOIN
returns only the rows where there is a match in both tables. If there is no match between the columns being joined, those rows will not appear in the result.
Consider two tables:
employees
: Contains employee details.departments
: Contains department details.To find the employees and their corresponding department names:
INNER JOIN
ensures that only employees who belong to a department are returned, excluding employees with no department assigned.The query will return all employees who are assigned to a department.
The LEFT JOIN
returns all rows from the left table and the matched rows from the right table. If no match is found, NULL
values are returned for columns from the right table.
To find all employees and their departments (including those without a department):
LEFT JOIN
includes all employees, even those who do not belong to any department. For those employees, the department_name
will be NULL
.The query will return all employees, with NULL
in the department_name
column for employees who do not have a department.
The RIGHT JOIN
is similar to the LEFT JOIN
, but it returns all rows from the right table and the matched rows from the left table. If no match is found, NULL
values are returned for columns from the left table.
To find all departments and their employees (including departments with no employees):
RIGHT JOIN
ensures that all departments are returned, even if there are no employees assigned to them. For departments without employees, the employee_id
and employee_name
will be NULL
.The query will return all departments, with NULL
in the employee details for departments with no employees.
The FULL JOIN
(or FULL OUTER JOIN
) returns all rows from both tables. If there is a match, the rows are combined. If there is no match, NULL
values are returned for columns from the table without a match.
To find all employees and all departments, including those without a matching department or employee:
FULL JOIN
returns all rows from both the employees
and departments
tables. Employees without a department and departments without employees will have NULL
values in the columns of the other table.The query will return all employees and all departments, filling NULL
where no match exists.
The CROSS JOIN
returns the Cartesian product of the two tables, i.e., it returns every combination of rows between the two tables. This join does not require a condition to match rows.
To get every combination of employee and department:
CROSS JOIN
will return every possible combination of employee and department, even if an employee is not assigned to a department.If there are 10 employees and 5 departments, the query will return 50 rows (10 × 5), representing all combinations of employees and departments.
A self join is a join where a table is joined with itself. It is useful when you need to compare rows within the same table.
Consider a employees
table where each employee has a manager_id
pointing to their manager's employee_id
. To find each employee and their manager's name:
employees
table is aliased as e1
for employees and e2
for managers. The LEFT JOIN
ensures that even employees without managers are included.The query will return a list of employees along with their manager's name (or NULL
if they don’t have a manager).
Join Type |
Description |
Includes Rows |
INNER JOIN |
Returns rows
where there is a match in both tables. |
Only matching
rows from both tables. |
LEFT JOIN |
Returns all
rows from the left table and matched rows from the right table. |
All rows from
the left table and matched rows from the right. |
RIGHT JOIN |
Returns all
rows from the right table and matched rows from the left table. |
All rows from
the right table and matched rows from the left. |
FULL JOIN |
Returns all
rows when there is a match in either left or right table. |
All rows from
both tables, with NULL where no match exists. |
CROSS JOIN |
Returns the
Cartesian product of both tables (every combination of rows). |
Every
combination of rows from both tables. |
SELF JOIN |
Joins a table
with itself. |
A table
joined with itself, typically used for hierarchical data. |
Comments
Post a Comment