Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I.

Similar presentations


Presentation on theme: "Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I."— Presentation transcript:

1 Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I

2 Table Aliases When joining tables with common attribute names, MySQL may get confused: SELECT MovieID FROM Movies, Taglines WHERE MovieID = MovieID; To solve this we can give each table an alias: SELECT M.MovieID FROM Movies M, Taglines T WHERE M.MovieID = T.MovieID;

3 Table Aliases You may also explicitly use qualified table names instead of aliases SELECT Movies.MovieID FROM Movies, Taglines WHERE Movies.MovieID = Taglines.MovieID; You may also use the AS keyword to specify a table alias SELECT M.MovieID FROM Movies AS M, Taglines AS T WHERE M.MovieID = T.MovieID;

4 The Problem Question: Display each movie title along with the name of the production company that produced it. The movie title comes from the Movies table The company name comes from the Companies table Two things we can do: Run multiple queries (not a good idea) Join the tables together (good idea)

5 The Solution Use a join (choose one from several join types): SELECT Title, Name FROM Movies NATURAL JOIN Companies; SELECT Title, Name FROM Movies JOIN Companies USING(CompanyID); SELECT Title, Name FROM Movies M INNER JOIN Companies C ON M.CompanyID = C.CompanyID; SELECT Title, Name FROM Movies M, Companies C WHERE M.CompanyID = C.CompanyID;

6 What is a Join? A join is a subset of the Cartesian Product between two tables A join is a type of mathematical operator, similar to multiplication, but applied to sets A join takes two records from two tables, one from table A and one from table B, and concatenates them horizontally if a condition, known as the join predicate or join condition, is true

7 What is a Cartesian Product? The Cartesian Product of tables A and B is the set of all possible concatenated rows whose first component comes from A and whose second component comes from B If A has a rows and B has b rows, the total number of rows in A x B is a x b Example: A has 6 rows B has 4 rows A x B has 24 rows

8 Cartesian Product Example Given these two tables, what is the Cartesian Product? A = SELECT * FROM Movies; B = SELECT * FROM Companies; Use a CROSS JOIN, which is the simplest type of join in SQL, to get the Cartesian Product A x B = SELECT * FROM Movies CROSS JOIN Companies;

9 Cartesian Product Result Cartesian Product SELECT * FROM Movies CROSS JOIN Companies; 24 total rows, with way too much information Only rows where CompanyIDs match are useful Cartesian Products can be quite large Cartesian Products are rarely useful Therefore, use CROSS JOIN sparingly (rarely)

10 Other (More Useful) Join Types SQL provides several other join types other than the cross join Natural Join, Named Column Join Inner and Outer Joins Equi-Join For each of these other join types, you can specify a boolean condition called a join condition, or join predicate, which is used to filter out the rows of the Cartesian Product that you don’t want

11 Join Conditions Since many records in a Cartesian Product are not meaningful, we can eliminate them using a join condition In general, most of the time, we want to keep only matching records (i.e. only when two values of a common attribute between two tables are equal)‏ Ex. Movies.MovieID = Companies.MovieID How you specify a join condition depends on the type of join you are using

12 Color Demo

13 Natural Joins In a natural join, no join condition is specified Join condition is determined automatically by name Syntax: SELECT attribute_list FROM A NATURAL JOIN B; Example: SELECT * FROM Movies NATURAL JOIN Taglines;

14 Problems with Natural Joins Try the following: SELECT * FROM Movies NATURAL JOIN Companies; Does it produce the expected results? Yes, but it’s not the join condition you wanted Wanted Movies.CompanyID = Companies.CompanyID Natural join uses Movies. CompanyID = Companies. CompanyID Movies.Country = Companies.Country Rarely use natural joins

15 Named Column Joins To solve the problem with natural joins, you may ‘override’ the attribute names that a natural join chooses by using a named column join Also called JOIN USING syntax Syntax: SELECT attribute_list FROM A JOIN B USING(column_name); SELECT attribute_list FROM A JOIN B USING(name1, name2, …);

16 More on Named Column Joins The following two queries are equivalent: SELECT * FROM Movies NATURAL JOIN Taglines; SELECT * FROM Movies JOIN Taglines USING(MovieID) Same thing, except that in a named column join, YOU specify the COMMON attribute for the join condition

17 More on Named Column Joins Remember this join that produced bad results? SELECT * FROM Movies NATURAL JOIN Companies; That’s because it’s equivalent to: SELECT * FROM Movies JOIN Companies USING(CompanyID, Country); To fix, use this instead: SELECT * FROM Movies JOIN Companies USING(CompanyID)

18 Inner Joins In an inner join, you explicitly write a full join condition expression in an ON clause Useful when meaningful, comparable attribute names aren’t named the same way (i.e. People.PersonID and Spouses.HusbandID) Syntax: SELECT attribute_list FROM A INNER JOIN B ON join_condition;

19 Inner Join Example Examples: SELECT * FROM Movies INNER JOIN Taglines T ON M.MovieID = T.MovieID; SELECT * FROM Movies M INNER JOIN Companies C ON M.CompanyID = C.CompanyID; -- A named column join can’t do this one! SELECT * FROM People P INNER JOIN Spouses S ON P.PersonID = S.HusbandID;

20 Equi-Joins Equivalent to an inner join, but with a different syntax Uses a comma separated list of tables in the FROM clause instead of the JOIN clause Join condition is specified in the WHERE clause Syntax: SELECT attribute_list FROM A, B WHERE join_condition;

21 Equi-Join Examples Examples: SELECT * FROM Movies M, Taglines T WHERE M.MovieID = T.MovieID; SELECT * FROM Movies M, Companies C WHERE M.CompanyID = C.CompanyID;

22 What Join Type Should I Use? When join condition attribute names are named the same, named column joins are shorter since you don’t have to type out a whole expression Inner joins and equi-joins always require table aliases or table quantifiers, named column joins do not Recommendations (but it’s up to you): Use named column joins for the most part Use inner joins when named column joins don’t work Rarely use equi-joins

23 Joining More Than Two Tables You may chain tables just like you chain multiplications… -- natural join SELECT * FROM A NATURAL JOIN B NATURAL JOIN C; -- named column join SELECT * FROM A JOIN B USING(a1) JOIN C USING(a2);

24 More Chaining… Examples: -- inner join SELECT * FROM A INNER JOIN B ON A.n1 = B.n2 INNER JOIN C ON B.n3 = C.n4; -- equi-join SELECT * FROM A, B, C WHERE A.n1 = B.n2 AND B.n3 = C.n4;

25 Join Expressions You may also wrap joins within parentheses, just as you can with mathematical expressions such as (1 + 2 – 3) * (8 – 4 + 2) SELECT * FROM (A JOIN B USING(n1)) JOIN (C JOIN D USING(n2)) USING(n3);

26 The Physical Meaning of Joins Every join has some kind of physical meaning A join suggests there is a relationship between two tables, that is described by the join condition A movie ‘is produced by’ a company A movie ‘has a’ tagline A movie ‘is associated with’ keywords People ‘have’ spouses Members ‘rate’ movies

27 How to Solve Join Problems Easy Problem List the first and last names of all actors along with the titles of any movies that they have appeared in. Medium Problem List the first and last names of all movie-related people that are both actors and directors. HARD!!! List all movie titles where any husband and wife pair appeared in the same movie (can be past, present, or future spouses).


Download ppt "Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I."

Similar presentations


Ads by Google