Professional Documents
Culture Documents
Session11 JoinsVeryImportantForInterviews
Session11 JoinsVeryImportantForInterviews
Session11 JoinsVeryImportantForInterviews
JOINS
-----
** INNER JOIN : only the matching records will be considered and non-matching
records will be discarded
---------------
LEFT OUTER JOIN
---------------
*** All the matching records from left and right table are considered +
All the non matching records in the left table which does not have the match in
the right padded with NULL
** For LEFT OUTER JOIN to happen, we must have some extra records in the left
table(T1)
Error: ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key
constraint fails (`trendytech`.`students`, CONSTRAINT `students_ibfk_1` FOREIGN KEY
(`selected_course`) REFER
ENCES `courses` (`course_id`))
* The above DELETE query won't work because it is attached with the students table
also in the form of FOREIGN KEY
* If we try to delete the course having course_id = 2, then the student who has
enrolled for course_id = 2 will become obsolete
-- Now, we will create a new students table and new couses table with same data but
FOREIGN key constraint won't be there
-- What will happen if we perform INNER JOIN on the above created table
----------------
RIGHT OUTER JOIN
----------------
*** All the matching records from left and right table are considered +
All the non matching records in the right table which does not have the match in
the left padded with NULL
** For RIGHT OUTER JOIN to happen, we must have some extra records in the right
table(T2)
---------------
FULL OUTER JOIN
---------------
* FULL OUTER JOIN = LEFT OUTER JOIN UNION RIGHT OUTER JOIN
* All the matching records + Non-matching records from left + Non-matching records
from right
* In SQL, we do not have keyword FULL for FULL OUTER JOIN, so we will do UNION of
LEFT OUTER JOIN and RIGHT OUTER JOIN
----------
CROSS JOIN
----------
* CROSS JOIN is costly as each record of first table is getting combined with each
record of second table.
If Left Table (T1) has 4 records
If Right Table (T2) has 5 records