0% found this document useful (0 votes)
21 views6 pages

Joins

SQL joins are essential for combining data from multiple tables based on shared key values, facilitating efficient data retrieval for complex queries. The document explains various types of joins including INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN, along with their syntax and examples using Student and StudentCourse tables. Each join type serves different purposes in data retrieval, allowing for comprehensive data analysis.

Uploaded by

matphychem6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views6 pages

Joins

SQL joins are essential for combining data from multiple tables based on shared key values, facilitating efficient data retrieval for complex queries. The document explains various types of joins including INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN, along with their syntax and examples using Student and StudentCourse tables. Each join type serves different purposes in data retrieval, allowing for comprehensive data analysis.

Uploaded by

matphychem6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

SQL Joins:

 SQL joins are the foundation of database management systems, enabling the
combination of data from multiple tables based on relationships between columns.
 Joins allow efficient data retrieval, which is essential for generating meaningful
observations and solving complex business queries.
 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.

Example of SQL JOINS


 Consider the two tables, Student and StudentCourse, which share a common
column ROLL_NO. Using SQL JOINS, we can combine data from these tables based
on their relationship, allowing us to retrieve meaningful information like student
details along with their enrolled courses.

Student:
StudentCourse Table

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

ROLL_NO NAME ADDRESS PHONE AGE COURSE_ID

1 HARSH DELHI XXXXXXXXXX 18 1

2 PRATIK BIHAR XXXXXXXXXX 19 2

3 RIYANKA SILGURI XXXXXXXXXX 20 2

4 DEEP RAMNAGAR XXXXXXXXXX 18 3

5 SAPTARHI KOLKATA XXXXXXXXXX 19 1


SQL INNER JOIN
The INNER JOIN keyword selects all rows from both the tables as long as the condition is
satisfied. This keyword will create the result-set by combining all rows from both the tables
where the condition satisfies i.e value of the common field will be the same.

Syntax:

SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;

Ex:

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


INNER JOIN StudentCourse
ON Student.ROLL_NO = StudentCourse.ROLL_NO;

SQL LEFT JOIN


LEFT JOIN returns all the rows of the table on the left side of the join and matches rows for
the table on the right side of the join. For the rows for which there is no matching row on the
right side, the result-set will contain null. LEFT JOIN is also known as LEFT OUTER JOIN.
Syntax
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;

Ex:
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
LEFT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;

SQL RIGHT JOIN


RIGHT JOIN returns all the rows of the table on the right side of the join and matching rows
for the table on the left side of the join. It is very similar to LEFT JOIN for the rows for which
there is no matching row on the left side, the result-set will contain null. RIGHT JOIN is also
known as RIGHT OUTER JOIN.
Syntax
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;

Ex:

SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
RIGHT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
SQL FULL JOIN
FULL JOIN creates the result-set by combining results of both LEFT JOIN and RIGHT
JOIN. The result-set will contain all the rows from both tables. For the rows for which there is
no matching, the result-set will contain NULL values.
Syntax
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;

Ex:

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

NAME COURSE_ID

HARSH 1

PRATIK 2

RIYANKA 2
NAME COURSE_ID

DEEP 3

SAPTARHI 1

DHANRAJ NULL

ROHIT NULL

NIRAJ NULL

NULL 4

NULL 5

NULL 4

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy