Curriculum
Joins and Relationships are among the most important concepts in relational databases and SQL. In real-world applications, data is rarely stored in a single table. Instead, information is divided into multiple related tables to reduce duplication, improve data integrity, and make database management more efficient. SQL Joins allow developers to combine data from multiple tables, while relationships define how those tables are connected.
Whether you are building a banking application, e-commerce platform, hospital management system, ERP software, or Spring Boot backend application, understanding Joins and Relationships is essential. Most enterprise SQL queries involve multiple tables connected through relationships.
For Java Backend Engineers, mastering Joins and Relationships is critical because technologies such as JDBC, Hibernate, JPA, and Spring Data JPA rely heavily on these concepts.
A database relationship defines how two or more tables are connected.
Example:
| Student_ID | Name |
|---|---|
| 101 | Rahul |
| 102 | Priya |
| Course_ID | Course_Name |
|---|---|
| 1 | Java |
| 2 | Python |
| Enrollment_ID | Student_ID | Course_ID |
|---|---|---|
| 1 | 101 | 1 |
| 2 | 102 | 2 |
These tables are connected through relationships.
Relationships help organize data efficiently.
Relationships provide several benefits.
Avoids storing duplicate information.
Maintains consistency.
Creates scalable systems.
Updates become simpler.
Data can be retrieved across multiple tables.
These advantages make relationships essential.
A Primary Key uniquely identifies a record.
Example:
Student_ID INT PRIMARY KEY
Student table:
| Student_ID | Name |
|---|---|
| 101 | Rahul |
| 102 | Priya |
Properties:
Primary keys are the foundation of relationships.
A Foreign Key connects one table to another.
Example:
Student_ID INT,
FOREIGN KEY(Student_ID)
REFERENCES Student(Student_ID)
The foreign key references the primary key of another table.
This creates a relationship between tables.
Students:
| Student_ID | Name |
|---|---|
| 101 | Rahul |
Enrollments:
| Enrollment_ID | Student_ID |
|---|---|
| 1 | 101 |
The Student_ID column connects both tables.
This relationship improves consistency.
Relational databases primarily support three types of relationships.
One record in Table A relates to one record in Table B.
Example:
| Employee_ID | Name |
|---|---|
| 1 | Rahul |
| Employee_ID | Passport_No |
|---|---|
| 1 | AB12345 |
One employee has one passport record.
Most common relationship type.
Example:
| Customer_ID | Name |
|---|---|
| 1 | Rahul |
| Order_ID | Customer_ID |
|---|---|
| 101 | 1 |
| 102 | 1 |
One customer can place many orders.
This relationship is widely used.
Many records relate to many records.
Example:
| Student_ID | Name |
|---|---|
| 101 | Rahul |
| Course_ID | Course_Name |
|---|---|
| 1 | Java |
A student can enroll in multiple courses.
A course can have multiple students.
An intermediate table is required.
Example:
| Student_ID | Course_ID |
|---|---|
| 101 | 1 |
This table connects both entities.
A Join combines data from multiple tables.
Example:
Instead of storing:
Student Name
Course Name
in the same table repeatedly, joins retrieve information dynamically.
This improves efficiency.
Joins provide:
Combine multiple tables.
Generate meaningful reports.
Avoid duplicate data.
Simplify complex queries.
Joins are used extensively in enterprise applications.
Student Table:
| Student_ID | Name |
|---|---|
| 101 | Rahul |
| 102 | Priya |
| 103 | Amit |
Enrollment Table:
| Student_ID | Course |
|---|---|
| 101 | Java |
| 102 | Python |
| 104 | Data Science |
These tables will be used throughout the lesson.
INNER JOIN returns matching records from both tables.
Syntax:
SELECT *
FROM Table1
INNER JOIN Table2
ON Table1.ID = Table2.ID;
SELECT
Student.Student_ID,
Student.Name,
Enrollment.Course
FROM Student
INNER JOIN Enrollment
ON Student.Student_ID =
Enrollment.Student_ID;
Output:
| Student_ID | Name | Course |
|---|---|---|
| 101 | Rahul | Java |
| 102 | Priya | Python |
Only matching records are displayed.
Notice:
Student_ID 103
has no enrollment.
And:
Student_ID 104
has no student record.
Therefore both are excluded.
This is how INNER JOIN works.
LEFT JOIN returns all records from the left table and matching records from the right table.
Syntax:
SELECT *
FROM Table1
LEFT JOIN Table2
ON condition;
SELECT
Student.Student_ID,
Student.Name,
Enrollment.Course
FROM Student
LEFT JOIN Enrollment
ON Student.Student_ID =
Enrollment.Student_ID;
Output:
| Student_ID | Name | Course |
|---|---|---|
| 101 | Rahul | Java |
| 102 | Priya | Python |
| 103 | Amit | NULL |
All student records appear.
Unmatched values become NULL.
RIGHT JOIN returns all records from the right table and matching records from the left table.
Example:
SELECT
Student.Student_ID,
Student.Name,
Enrollment.Course
FROM Student
RIGHT JOIN Enrollment
ON Student.Student_ID =
Enrollment.Student_ID;
Output:
| Student_ID | Name | Course |
|---|---|---|
| 101 | Rahul | Java |
| 102 | Priya | Python |
| NULL | NULL | Data Science |
All enrollment records appear.
Returns all records from both tables.
Example concept:
FULL OUTER JOIN
Output includes:
Not directly supported in MySQL but supported in several other databases.
Produces every possible combination.
Example:
Students:
| Name |
|---|
| Rahul |
| Priya |
Courses:
| Course |
|---|
| Java |
| Python |
Query:
SELECT *
FROM Student
CROSS JOIN Course;
Output:
| Name | Course |
|---|---|
| Rahul | Java |
| Rahul | Python |
| Priya | Java |
| Priya | Python |
Every combination is generated.
A table joins with itself.
Example:
Employee table:
| Employee_ID | Name | Manager_ID |
|---|---|---|
| 1 | Rahul | NULL |
| 2 | Priya | 1 |
Query:
SELECT
E.Name,
M.Name
FROM Employee E
JOIN Employee M
ON E.Manager_ID =
M.Employee_ID;
Output:
| Employee | Manager |
|---|---|
| Priya | Rahul |
Useful for hierarchical data.
Customer_ID
Customer_Name
Order_ID
Customer_ID
Relationship:
One Customer
Many Orders
This is a One-to-Many relationship.
Tables:
Customers
Accounts
Transactions
Loans
Join queries retrieve:
Banking applications rely heavily on joins.
Tables:
Customers
Products
Orders
Payments
Joins retrieve:
These systems perform thousands of joins daily.
Tables:
Patients
Doctors
Appointments
Prescriptions
Joins generate complete patient records.
Healthcare systems depend heavily on relational queries.
Data remains normalized.
Less redundant information.
Consistent records.
Updates become simpler.
Supports advanced analytics.
These advantages make joins indispensable.
Incorrect:
SELECT *
FROM Student,
Enrollment;
May produce unintended results.
Always define:
ON
conditions.
Choose joins carefully based on requirements.
Poor relationship design leads to inefficient databases.
Use relationships instead of repeated values.
Understanding these mistakes improves database quality.
These practices improve scalability and maintainability.
Technologies such as:
use relationships extensively.
Examples:
@OneToOne
@OneToMany
@ManyToOne
@ManyToMany
These annotations are built upon database relationships.
Joins and Relationships are used in:
Customer Accounts
Orders and Products
Employees and Departments
Patients and Doctors
Students and Courses
Modern applications depend heavily on relational database design.
Joins and Relationships enable relational databases to connect tables efficiently and retrieve meaningful information. Relationships reduce redundancy and improve consistency, while joins combine data from multiple tables to support business operations.
Key concepts covered include:
Mastering Joins and Relationships is essential for SQL development, JDBC programming, Hibernate, JPA, Spring Boot, and enterprise Java backend engineering.
A Join combines data from multiple tables based on a related column.
A Foreign Key creates a relationship between two tables.
One-to-Many is the most commonly used relationship.
INNER JOIN returns only matching records from both tables.
Relationships reduce data duplication, improve consistency, and support scalable database design.
Want to explore additional programming and software development topics? Click here for more free courses
WhatsApp us