SQL Joins
SQL Joins
SQL Join operation combines data or rows from two or more tables based on a common field
between them.
In this article, we will learn about Joins in SQL, covering JOIN types, syntax, and examples.
SQL JOIN
SQL JOIN clause is used to query and access data from multiple tables by establishing
logical relationships between them. It can access data from multiple tables simultaneously
using common key values shared across different tables.
We can use SQL JOIN with multiple tables. It can also be paired with other clauses, the most
popular use will be using JOIN with WHERE clause to filter data retrieval.
SQL JOIN Example
Consider the two tables below as follows:
Student:
StudentCourse :
Both these tables are connected by one common key (column) i.e ROLL_NO.
We can perform a JOIN operation using the given SQL query:
SELECT s.roll_no, s.name, s.address, s.phone, s.age, sc.course_id
FROM Student s
JOIN StudentCourse sc ON s.roll_no = sc.roll_no;
Output:
SAPTARH
5 KOLKATA XXXXXXXXXX 19 1
I
Syntax
The syntax of SQL FULL JOIN is:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;
Here,
table1: First table.
table2: Second table
matching_column: Column common to both the tables.
FULL JOIN Example
Let’s look at the example of FULL JOIN clause, and understand it’s working
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
FULL JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
Output:
NAME COURSE_ID
HARSH 1
PRATIK 2
RIYANKA 2
DEEP 3
SAPTARH
1
I
DHANRAJ NULL
ROHIT NULL
NIRAJ NULL
NULL 4
NULL 5
NULL 4
NATURAL JOIN
A NATURAL JOIN is a type of join that automatically joins tables based on all columns with
the same name and compatible data types. It eliminates the need to specify the join condition
explicitly.
Example:
sql
Copy code
SELECT * FROM table1
NATURAL JOIN table2;
This join automatically matches columns with the same names from both tables.
The result set includes rows where the values in these columns are equal.
Columns that are used for the join condition appear only once in the result set.
Example Tables
Table1
id name
1 Alice
2 Bob
Table2
id city
1 New York
3 Boston
A SELF JOIN is a join operation where a table is joined with itself. This is useful for querying
hierarchical data or comparing rows within the same table.
Syntax
The basic syntax of a SELF JOIN involves joining a table with itself using an alias
A SELF JOIN is a join operation where a table is joined with itself. This is useful for querying
hierarchical data, comparing rows within the same table, or identifying relationships between
rows in the same table.
Syntax
The basic syntax of a SELF JOIN involves joining a table with itself using an alias:
sql
Copy code
SELECT A.column1, B.column2
FROM table_name A
JOIN table_name B
ON A.common_column = B.common_column;
Example
Consider a table employees with the following data:
1 Alice NULL
2 Bob 1
3 Charlie 1
4 David 2
5 Eve 2
To find each employee along with their manager's name, you can use a SELF JOIN:
sql
Copy code
SELECT e1.name AS employee_name, e2.name AS manager_name
FROM employees e1
LEFT JOIN employees e2 ON e1.manager_id = e2.employee_id;
Result
employee_name manager_name
Alice NULL
Bob Alice
Charlie Alice
David Bob
Eve Bob
CROSS JOIN
A CROSS JOIN returns the Cartesian product of the two tables. This means it will return all
possible combinations of rows from the two tables.
Example:
sql
Copy code
SELECT * FROM table1
CROSS JOIN table2;
If table1 has n rows and table2 has m rows, the result set will have n * m rows.
There is no ON clause with CROSS JOIN because it combines every row from the first table
with every row from the second table.
Example Tables
Table1
id name
1 Alice
2 Bob
Table2
id city
1 New York
3 Boston
1 Alice 3 Boston
2 Bob 3 Boston