Database Systems
BCSE302P
SQL Joins
Dr. Amrit Pal
Assistant Professor
SCOPE, VIT Chennai
SQL JOINS
➢ SQL JOINS are used to retrieve data from multiple tables. A SQL JOIN is
performed whenever two or more tables are joined in a SQL statement.
➢ There are different types of SQL joins:
➢ SQL INNER JOIN (or sometimes called simple join)
➢ SQL LEFT OUTER JOIN (or sometimes called LEFT JOIN)
➢ SQL RIGHT OUTER JOIN (or sometimes called RIGHT JOIN)
➢ SQL FULL OUTER JOIN (or sometimes called FULL JOIN)
➢ SQL CROSS JOIN
➢ SQL NATURAL JOIN
➢ SQL SELF JOIN
6 February 2025 VITCC-CSE3091 2
SQL JOINS: Sample tables (Example)
student
course
6 February 2025 VITCC-CSE3091 3
SQL INNER JOIN (SIMPLE JOIN)
➢ SQL INNER JOINS return all rows from multiple tables where the join condition is
met.
➢ The syntax for the SQL INNER JOIN is:
➢ SELECT columns FROM table1 INNER JOIN table2 ON table1.column =
table2.column;
6 February 2025 VITCC-CSE3091 4
SQL INNER JOIN (SIMPLE JOIN): Example query
➢ select * from student inner join course on student.StudentID = course.StudentID;
6 February 2025 VITCC-CSE3091 5
SQL LEFT OUTER JOIN
➢ Another type of join is called a LEFT OUTER JOIN. This type of join returns all
rows from the LEFT-hand table specified in the ON condition and only those rows
from the other table where the joined fields are equal (join condition is met).
➢ The syntax for the SQL LEFT OUTER JOIN is:
➢ SELECT columns FROM table1 LEFT [OUTER] JOIN table2 ON table1.column =
table2.column;
➢ In some databases, the LEFT OUTER JOIN keywords are replaced with LEFT JOIN.
6 February 2025 VITCC-CSE3091 6
SQL LEFT OUTER JOIN: Example query
➢ select * from student left outer join course on student.StudentID =
course.StudentID;
➢ Or, select * from student left join course on student.StudentID = course.StudentID;
6 February 2025 VITCC-CSE3091 7
SQL LEFT OUTER JOIN: Example query
➢ select * from course left outer join student on course.StudentID =
student.StudentID;
➢ Or, select * from course left join student on course.StudentID = student.StudentID;
6 February 2025 VITCC-CSE3091 8
SQL RIGHT OUTER JOIN
➢ Another type of join is called a SQL RIGHT OUTER JOIN. This type of join
returns all rows from the RIGHT-hand table specified in the ON condition and only
those rows from the other table where the joined fields are equal (join condition is
met).
➢ The syntax for the SQL RIGHT OUTER JOIN is:
➢ SELECT columns FROM table1 RIGHT OUTER JOIN table2 ON table1.column =
table2.column;
➢ In some databases, the RIGHT OUTER JOIN keywords are replaced with RIGHT
JOIN.
6 February 2025 VITCC-CSE3091 9
SQL RIGHT OUTER JOIN: Example query
➢ select * from student right outer join course on student.StudentID =
course.StudentID;
➢ Or, select * from student right join course on student.StudentID =
course.StudentID;
6 February 2025 VITCC-CSE3091 10
SQL RIGHT OUTER JOIN: Example query
➢ select * from course right outer join student on course.StudentID =
student.StudentID;
➢ Or, select * from course right join student on course.StudentID =
student.StudentID;
6 February 2025 VITCC-CSE3091 11
SQL FULL OUTER JOIN
➢ Another type of join is called a SQL FULL OUTER JOIN. This type of join returns
all rows from the LEFT-hand table and RIGHT-hand table with nulls in place where
the join condition is not met.
➢ The syntax for the SQL FULL OUTER JOIN is:
➢ SELECT columns FROM table1 FULL OUTER JOIN table2 ON table1.column =
table2.column;
➢ In some databases, the FULL OUTER JOIN keywords are replaced with FULL
JOIN.
6 February 2025 VITCC-CSE3091 12
SQL FULL OUTER JOIN
➢ To clearly understand full outer join, add two more tuples in course table. Let’s
reconsider both the tables.
student
course
6 February 2025 VITCC-CSE3091 13
SQL FULL OUTER JOIN: Example query
➢ select * from student full join course on student.StudentID = course.StudentID;
6 February 2025 VITCC-CSE3091 14
SQL NATURAL JOIN
➢ A NATURAL JOIN is a JOIN operation that creates an implicit join clause for you
based on the common columns in the two tables being joined. Common columns
are columns that have the same name in both tables.
➢ Syntax
➢ Select * FROM table1 NATURAL JOIN table2;
6 February 2025 VITCC-CSE3091 15
SQL NATURAL JOIN: Example query
➢ select * from student natural join course;
6 February 2025 VITCC-CSE3091 16
SQL CROSS JOIN
➢ A CROSS JOIN is a JOIN operation that produces the Cartesian product of two
tables. Unlike other JOIN operators, it does not let you specify a join clause. You
may, however, specify a WHERE clause in the SELECT statement.
➢ Syntax
➢ Select * FROM table1 CROSS JOIN table2;
➢ Or, Select * FROM table1, table2;
6 February 2025 VITCC-CSE3091 17
SQL CROSS JOIN: Example query
➢ Select * FROM student CROSS JOIN course;
➢ Or, Select * FROM student, course;
6 February 2025 VITCC-CSE3091 18
SQL CROSS JOIN: Example query (continued…)
➢ Select * FROM student CROSS JOIN course;
➢ Or, Select * FROM student, course;
6 February 2025 VITCC-CSE3091 19
SQL SELF JOIN
➢ A SELF JOIN is a JOIN operation that join with itself.
➢ Syntax
➢ Select a.StudentID, b.firstName, a.country FROM student a, student b where
a.StudentID < b.StudentID;
➢ SELECT a.ID, b.NAME, a.SALARY FROM CUSTOMER a, CUSTOMER b WHERE
a.SALARY < b.SALARY;
6 February 2025 VITCC-CSE3091 20
Thank You!
6 February 2025 VITCC-CSE3091 21