Joins_SQL_Assignment_3
Joins_SQL_Assignment_3
Definition: - In SQL, a JOIN operation is used to combine rows from two or more tables based
on a related column between them.
An SQL Join clause is used to combine multiple related tables in a database, based on common
fields/columns.
• Inner Join
• Outer Join
a) Left Join
b) Right Join
c) Full Join
• Other joins
a) Self Join
b) Cross Join
1. Inner Join: -
An INNER JOIN in SQL returns only the rows where there is a match in both tables based on a
specified condition. This means it combines rows from two or more tables by comparing values in
specified columns, and only includes rows where the joined columns have matching values in both
tables.
In other words, an INNER JOIN filters out any rows from either table that do not meet the join
condition, so only the "intersection" of both tables is included in the result.
Syntax: -
SELECT table1.column1, table2.column2, ...
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column;
2. Outer Join: -
An OUTER JOIN in SQL is a type of join that returns all records from one table and the
matching records from the other table. If there is no match, the result will contain NULL for
columns from the table that lacks a matching row.
Returns all records from the left table and the matching records from the right table. If
there is no match in the right table, the result will contain NULL for columns from the
right table.
Syntax: -
Returns all records from the right table and the matching records from the left table. If
there is no match in the left table, the result will contain NULL for columns from the left
table.
Syntax: -
Returns all records when there is a match in either the left or right table. Rows without a
match in one of the tables will contain NULL for columns from the table that lacks a
match.
Syntax: -
3. Self join: -
A SELF JOIN is a join operation where a table is joined with itself. This is useful when you
want to compare rows within the same table or find relationships among rows in the same table.
Since you are joining the table to itself, you must use aliases to distinguish between the two
instances of the table.
Syntax: -
4. Cross Join: -
A CROSS JOIN produces the Cartesian product of two tables, which means it combines each
row from the first table with each row from the second table. A cross join does not require a join
condition, and it will return a result set with a total number of rows equal to the product of the
rows in both tables.
Syntax: -
1 Alice 10
2 Bob 20
3 Charlie 30
4 David NULL
5 Emma 10
6 Frank 20
7 Grace 30
8 Hannah 30
Dept_id Dept_name
10 HR
20 IT
40 Marketing
30 Finance
50 Operations
60 Research
70 Legal
80 Sales
i. Use an INNER JOIN to find the names of employees who belong to a department.
ii. Use a LEFT OUTER JOIN to list all employees, showing their department if they
have one. If an employee does not belong to a department, show NULL for the
department name.
iii. Use a RIGHT OUTER JOIN to list all departments, showing the names of
employees in each department. If a department has no employees, show NULL for
the employee’s name.
iv. Use a FULL OUTER JOIN to list all employees and all departments, showing
NULL for departments with no employees and employees with no department.
v. Use an INNER JOIN to find the names of employees who are in both IT and HR
departments.
vi. Write a query to find employees without a department using a LEFT JOIN.
vii. Find the total number of employees in each department using INNER JOIN and
GROUP BY.
viii. Find all employees who do not belong to either the "HR" or "IT" department using
an OUTER JOIN.
ix. List the names of employees and departments where employees work, but exclude
records where department names start with the letter "M."
x. Use a LEFT JOIN to list all employees and count how many employees are in each
department, even if some departments have no employees.
xi. Find the department name and employee name for employees who have no
department assigned by using an OUTER JOIN.
xii. Write a query to find employees who are in the same department as another
employee.
(This query uses a Self Join to find pairs of employees in the same department.)