Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL – join.

Similar presentations


Presentation on theme: "SQL – join."— Presentation transcript:

1 SQL – join

2 Multiple Relation queries
Often, queries need to combine (join) rows from multiple tables. Joins specify which rows get merged with rows from a second table. As there are many possible methods to match rows together, there are many types of joins. You will need to know (memorize) the different joins. Annoying, but very useful knowledge.

3 students id name smithe Eric cancan Can nahumjos Josh samiam Samuel-Hunter thesaint Nicholas mafia Anthony NULL demo-student CREATE TABLE students (id TEXT, name TEXT); INSERT INTO students VALUES('smithe','Eric'); INSERT INTO students VALUES('cancan','Can'); INSERT INTO students VALUES('nahumjos','Josh'); INSERT INTO students VALUES('samiam','Samuel-Hunter'); INSERT INTO students VALUES('thesaint','Nicholas'); INSERT INTO students VALUES('mafia','Anthony'); INSERT INTO students VALUES(NULL, 'demo-student'); associates CREATE TABLE associates (responder_id TEXT, associate_id TEXT); INSERT INTO associates VALUES('cancan','samiam'); INSERT INTO associates VALUES('cancan','smithe'); INSERT INTO associates VALUES('samiam','cancan'); INSERT INTO associates VALUES('samiam','smithe'); INSERT INTO associates VALUES('mafia','thesaint'); responder_id associate_id cancan samiam smithe mafia thesaint

4 CROSS JOIN Combine every row in one table with every row in the other
SELECT * FROM students CROSS JOIN associates; Implicit Cross Join (equivalent) SELECT * FROM students, associates; Also called the Cartesian Product (as it results in every possible combination) Not often used, because you rarely need all possible combinations.

5 How many Rows should be returned by a cross join between a table with M rows and a table with N rows? 2 * (M * N) N * M M + N M ^ N

6 Inner Join INNER JOIN is like the CROSS JOIN, except only rows that match a predicate are allowed. Frequently that predicate includes primary keys to match associated data SELECT * FROM students INNER JOIN associates ON id = responder_id; You can equivalently do an implicit inner join and use WHERE to filter rows: SELECT * FROM students, associates WHERE id = responder_id;

7 INNER Join Retuned rows are those that match both tables (shaded).

8 Full Outer Join Like an INNER JOIN, the FULL OUTER JOIN includes all rows that match a predicate. In addition however, rows that don't have a match are included with NULL values where their corresponding data would be SELECT * FROM students FULL OUTER JOIN associates ON id = respondent_id; SQLite doesn't implement FULL OUTER JOIN, . But I'll show you how to do it anyways in a later lecture.

9 FULL OUTER JOIN

10 LEFT OUTER JOIN LEFT OUTER JOIN includes all the rows to the left of the JOIN keyword, where there is a match, it add the matching column data to rows. If there isn't a match NULL values are used instead. SELECT * FROM students LEFT OUTER JOIN associates ON id = responder_id; Note: order matters!!! SELECT * FROM associates LEFT OUTER JOIN students

11 LEFT OUTER JOIN

12 LEFT OUTER JOIN Without Matches
If we want to find the rows in the left table that don't have matches, we can do a LEFT OUTER JOIN and filter out the rows with matches. SELECT * FROM students LEFT OUTER JOIN associates ON id = responder_id WHERE responder_id IS NULL;

13 LEFT OUTER JOIN Without Matches

14 Which rows should a LEFT INNER Join return?
Rows that match the predicate Row that match plus the unmatched rows on left Rows that are unmatched and the matches on the left ???

15 Self Join You can make a table join on itself. For instance, what it you wanted to find all the mutual associates (where both respondents marked each other as associates). SELECT * FROM associates AS a1 -- We need aliases INNER JOIN associates AS a2 -- Another Alias ON a1.responder_id = a2.associate_id -- first other AND a1.associate_id = a2.responder_id -- Other match first WHERE a1.responder_id < a2.responder_id; -- remove dupes

16 Natural joins Natural Joins are joins where column names that are shared between two tables in a join are assumed to be keys of each other. The rows returned are the ones that match in the shared column names They are bad practice in general as coincidental column name matches will break the query. SELECT * FROM students NATURAL JOIN associates; This is the same as cross join as there are no column names in common.

17 Types of Relations Often tables tend to break into two categories (I made this up, so it won't be in any textbook): Entity Tables – they map a primary key to data about an entity Relation Tables – they map relationships between primary keys in tables together If you can break up your tables into these types, it makes for easier joins and less duplication. students (id TEXT, name TEXT) is an Entity Table associates (responder_id TEXT, associate_id TEXT) is a Relation Table These aren't hard a fast rules, just suggestions.


Download ppt "SQL – join."

Similar presentations


Ads by Google