SQL Join BCIS1013 / IS251 – Database System Ms Nurfarahhanis Abdul Rasid Copyright @2024 by UNIMY University of Computer Science & Engineering Information Classification-Restricted Introduction • 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. (INNER) JOIN • (INNER) JOIN: Returns records that have matching values in both tables. • Syntax: SELECT column_name(s) FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name; (INNER) JOIN • Example : Show book titles along with their authors Books id title 1 2 Time to Grow Up! Your Trip 3 4 5 Lovely Love Dream Your Life Oranges 6 Your Happy Life 7 Applied AI 8 My Last Book Authors type author _id original 11 editor_ transla id tor_id 21 transla ted original original transla ted transla ted transla ted original 15 22 14 11 12 24 24 25 31 15 22 33 13 23 34 11 28 32 id first_name last_name 11 Ellen Writer 12 Olga Savelieva 13 Jack Smart 14 Donald Brain 15 Yao Dou (INNER) JOIN • Example : Show book titles along with their authors SELECT b.id, b.title, a.first_name, a.last_name FROM books b INNER JOIN authors a id title first_name last_name ON b.author_id = a.id 1 Time to Grow Up! Ellen Writer ORDER BY b.id; 2 Your Trip Yao Dou 3 Lovely Love Donald Brain 4 Dream Your Life Ellen Writer 5 Oranges Olga Savelieva 6 Your Happy Life Yao Dou 7 Applied AI Jack Smart 8 My Last Book Ellen Writer LEFT (OUTER) JOIN • LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the right table. • Syntax: SELECT column_name(s) FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name; LEFT (OUTER) JOIN • Example: Display all books with their editors, if any. Books id title 1 2 Time to Grow Up! Your Trip 3 4 5 Lovely Love Dream Your Life Oranges 6 Your Happy Life 7 Applied AI 8 My Last Book type author _id original 11 editor_ transla id tor_id 21 transla ted original original transla ted transla ted transla ted original 15 22 32 14 11 12 24 24 25 31 15 22 33 13 23 34 11 28 Editors id 21 22 23 24 25 26 27 first_name Daniel Mark Maria Cathrine Sebastian Barbara Matthew last_name Brown Johnson Evans Roberts Wright Jones Smith LEFT (OUTER) JOIN • Example: Display all books with their editors, if any. SELECT b.id, b.title, e.last_name AS editor FROM books b LEFT JOIN editors e id title ON b.editor_id = e.id 1 Time to Grow Up! ORDER BY b.id; editor Brown 2 Your Trip Johnson 3 Lovely Love Roberts 4 Dream Your Life Roberts 5 Oranges Wright 6 Your Happy Life Johnson 7 Applied AI Evans 8 My Last Book NULL RIGHT (OUTER) JOIN • RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the left table. • Syntax: SELECT column_name(s) FROM table1 RIGHT JOIN table2 ON table1.column_name = table2.column_name WHERE condition; RIGHT (OUTER) JOIN • Example: : Display all editors with their books. Books id title 1 2 Time to Grow Up! Your Trip 3 4 5 Lovely Love Dream Your Life Oranges 6 Your Happy Life 7 Applied AI 8 My Last Book type author _id original 11 editor_ transla id tor_id 21 transla ted original original transla ted transla ted transla ted original 15 22 32 14 11 12 24 24 25 31 15 22 33 13 23 34 11 28 Editors id 21 22 23 24 25 26 27 first_name Daniel Mark Maria Cathrine Sebastian Barbara Matthew last_name Brown Johnson Evans Roberts Wright Jones Smith RIGHT (OUTER) JOIN • Example: : Display all editors with their books. SELECT b.id, b.title, e.last_name AS editor FROM books b id title RIGHT JOIN editors e ON b.editor_id = e.id 1 Time to Grow Up! ORDER BY b.id; 2 Your Trip editor Brown Johnson 3 Lovely Love Roberts 4 Dream Your Life Roberts 5 Oranges Wright 6 Your Happy Life Johnson 7 Applied AI Evans NULL NULL Jones NULL NULL Smith FULL (OUTER) JOIN • FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table. • Syntax: SELECT column_name(s) FROM table1 FULL JOIN table2 ON table1.column_name table2.column_name WHERE condition; = FULL (OUTER) JOIN • Example : Display all books and all editors. Books id title 1 2 Time to Grow Up! Your Trip 3 4 5 Lovely Love Dream Your Life Oranges 6 Your Happy Life 7 Applied AI 8 My Last Book type author _id original 11 editor_ transla id tor_id 21 transla ted original original transla ted transla ted transla ted original 15 22 32 14 11 12 24 24 25 31 15 22 33 13 23 34 11 28 Editors id 21 22 23 24 25 26 27 first_name Daniel Mark Maria Cathrine Sebastian Barbara Matthew last_name Brown Johnson Evans Roberts Wright Jones Smith FULL (OUTER) JOIN • Example : Display all books and all editors. SELECT b.id, b.title, e.last_name AS editor FROM books b id title FULL JOIN editors e ON b.editor_id = e.id 1 Time to Grow Up! ORDER BY b.id; 2 Your Trip editor Brown Johnson 3 Lovely Love Roberts 4 Dream Your Life Roberts 5 Oranges Wright 6 Your Happy Life Johnson 7 Applied AI Evans 8 My Last Book NULL NULL NULL Jones NULL NULL Smith