Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

In a relational database, data is often distributed across multiple tables.

The concept of JOINs


involves combining rows from two or more tables based on a related column. This related
column is typically a key, such as a primary key in one table and a foreign key in another. The
purpose of JOINs is to link data from different tables that share a common relationship, enabling
more complex and comprehensive analysis of the data.
SELECT columns
FROM table1
JOIN table2 ON table1.column = table2.column;
The key is to understand which table is the primary focus of your query (the one from which you
want to retrieve data) and which one is supplementary. The JOIN keyword is used to connect the
tables based on specified conditions.
Books List:

Book ID Title Author Category


1 Harry Potter J.K. Rowling Fantasy
2 To Kill a Mockingbird Harper Lee Fiction
3 The Hobbit J.R.R. Tolkien Fantasy
Members List:

Member ID Name Book ID Return Date


101 Alice 1 2023-08-15
102 Bob 2 2023-08-12
103 Charlie 3 2023-08-20
Now, let's say you want to know which member borrowed which book and when they need to return it.
You can use a "join" to bring these lists together based on the Book ID.
In simple terms, a "join" is like putting the two lists side by side, matching the Book IDs. Here's how you
do it in SQL:
SELECT Members.Name, Books.Title, Members.ReturnDate
FROM Members
JOIN Books ON Members.BookID = Books.BookID;

In this example, the JOIN combines the Members table and the Books table using the BookID column,
which is common to both tables. The result will be like this:

Name Title Return Date


Alice Harry Potter 2023-08-15
Bob To Kill a Mockingbird 2023-08-12
Charlie The Hobbit 2023-08-20
So, using a "join," you were able to see who borrowed which book and when they need to return it, all in
one place!
In essence, "joins" are a way to combine information from different tables by looking at a shared piece of
information, like the Book ID in this case. It's like putting puzzle pieces together to see the bigger picture.

Join Conditions:
 The join condition specifies how the rows from different tables are matched. This is
usually done using the ON keyword, which indicates the columns that establish the
relationship.

Is it possible to join tables without a common column in two tables?

Yes, it is possible to join tables without a common column, but the result might not be very meaningful
or useful. In a typical SQL join operation, you combine rows from two tables based on a common column
value. This common column acts as a connection point between the tables, allowing you to link related
information.

If you try to join tables without a common column, you might end up with a cartesian product or a cross
join. In a cartesian product, each row from the first table is combined with every row from the second
table. This can lead to a massive number of rows in the result, and it's often not what you actually want.

For example, let's say you have two tables: Students and Courses, but they don't have a common
column. If you attempt to join them without a common column:
SELECT * FROM Students CROSS JOIN Courses;

This would create a combination of all students with all courses, which might not be meaningful in most
scenarios.

Common Types of JOINs:


 INNER JOIN: Retrieves rows when there is a match in both tables.
An INNER JOIN retrieves rows from two or more tables where there is a match based
on a specified condition.

-- Creating sample tables


CREATE TABLE employees (
employee_id INT PRIMARY KEY,
employee_name VARCHAR(255),
department_id INT
);
CREATE TABLE departments (
department_id INT PRIMARY KEY,
department_name VARCHAR(255)
);

-- Inserting sample data


INSERT INTO employees (employee_id, employee_name, department_id)
VALUES
(1, 'Alice', 101),
(2, 'Bob', 102),
(3, 'Carol', 101);

INSERT INTO departments (department_id, department_name)


VALUES
(101, 'HR'),
(102, 'IT');

-- Performing an INNER JOIN


SELECT employees.employee_id, employees.employee_name,
departments.department_name
FROM employees
INNER JOIN departments ON employees.department_id = departments.department_id;

 LEFT JOIN (or LEFT OUTER JOIN): Retrieves all rows from the left table and
matching rows from the right table.
-- Creating sample tables
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
employee_name VARCHAR(255),
project_id INT
);

CREATE TABLE projects (


project_id INT PRIMARY KEY,
project_name VARCHAR(255)
);

-- Inserting sample data


INSERT INTO employees (employee_id, employee_name, project_id)
VALUES
(1, 'Alice', 101),
(2, 'Bob', NULL),
(3, 'Carol', 102);

INSERT INTO projects (project_id, project_name)


VALUES
(101, 'Project A'),
(102, 'Project B');

-- Performing a LEFT JOIN


SELECT employees.employee_id, employees.employee_name, projects.project_name
FROM employees
LEFT JOIN projects ON employees.project_id = projects.project_id;

 RIGHT JOIN (or RIGHT OUTER JOIN): Retrieves all rows from the right table and
matching rows from the left table.
 FULL JOIN (or FULL OUTER JOIN): Retrieves all rows when there is a match in
either table.
An OUTER JOIN is a generic term that includes both LEFT JOIN and RIGHT JOIN. It retrieves
all rows from one table and matching rows from the other table. If there is no match in one of the
tables, NULL values are returned for columns from that table.
-- Creating sample tables
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
employee_name VARCHAR(255),
project_id INT
);

CREATE TABLE projects (


project_id INT PRIMARY KEY,
project_name VARCHAR(255)
);

-- Inserting sample data


INSERT INTO employees (employee_id, employee_name, project_id)
VALUES
(1, 'Alice', 101),
(2, 'Bob', NULL),
(3, 'Carol', 102);

INSERT INTO projects (project_id, project_name)


VALUES
(101, 'Project A'),
(102, 'Project B');
-- Performing an OUTER JOIN
SELECT employees.employee_id, employees.employee_name, projects.project_name
FROM employees
FULL JOIN projects ON employees.project_id = projects.project_id;

 SELF JOIN: Joins a table with itself, often used to represent hierarchical relationships.
A self join involves joining a table with itself. This can be useful when you have hierarchical
data or need to establish relationships within the same table.
-- Creating a sample table
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
employee_name VARCHAR(255),
manager_id INT
);

-- Inserting sample data


INSERT INTO employees (employee_id, employee_name, manager_id)
VALUES
(1, 'Alice', 3),
(2, 'Bob', 3),
(3, 'Carol', NULL),
(4, 'Dave', 2);

-- Performing a SELF JOIN


SELECT e.employee_name AS employee, m.employee_name AS manager
FROM employees e
INNER JOIN employees m ON e.manager_id = m.employee_id;

SELECT StudentCourse.COURSE_ID, Student.NAME, Student.AGE FROM Student


INNER JOIN StudentCourse
ON Student.ROLL_NO = StudentCourse.ROLL_NO;
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
LEFT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
RIGHT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
FULL JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;

You might also like