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

Joins_SQL_Assignment_3

Uploaded by

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

Joins_SQL_Assignment_3

Uploaded by

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

Joins (SQL)

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.

Types of joins in SQL: -

• 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.

There are three main types of outer joins:

a) LEFT OUTER JOIN (LEFT JOIN):

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: -

SELECT table1.column1, table2.column2, ...


FROM table1
LEFT OUTER JOIN table2
ON table1.common_column = table2.common_column;

b) RIGHT OUTER JOIN (RIGHT JOIN): -

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: -

SELECT table1.column1, table2.column2, ...


FROM table1
RIGHT OUTER JOIN table2
ON table1.common_column = table2.common_column;

c) FULL OUTER JOIN (FULL JOIN): -

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: -

SELECT table1.column1, table2.column2, ...


FROM table1
FULL OUTER JOIN table2
ON table1.common_column = table2.common_column;

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: -

SELECT A.column1, B.column2


FROM table_name A
INNER JOIN table_name B
ON A.common_column = B.common_column;

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: -

SELECT table1.column1, table2.column2


FROM table1
CROSS JOIN table2;
Assignment No. – 3

Q1. Create the following table “Employees”

Column Name Datatype Size

Emp_id (Primary Key) Number 10


Emp_name Varchar2 20
Dept_id Number 20

Q2. Insert the following data in the Employees table

Emp_id Emp_name Dept_id

1 Alice 10
2 Bob 20
3 Charlie 30
4 David NULL
5 Emma 10
6 Frank 20
7 Grace 30
8 Hannah 30

Q3. Create the following table “Departments”

Column Name Datatype Size

Dept_id (Primary Key) Number 10


Dept_name Varchar2 10
Q4. Insert the following data in the Departments table

Dept_id Dept_name

10 HR
20 IT
40 Marketing
30 Finance
50 Operations
60 Research
70 Legal
80 Sales

Q5. Based on above two tables answer the following Questionaries: -

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.)

You might also like