Joins

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Notes on slide 1

    CIS-182 Joins

    Favorites, Groups & Events

    Joins - Presentation Transcript

    1. Joins CIS-182
    2. Multi-Table Queries
      • Multi-table queries allow data from different tables to be combined and compared
      • Typically done by comparing a common value
        • Comparison done between like data types
        • May be primary key/foreign key fields but doesn’t have to be
    3. Types of Multi-table Queries
      • Joins : combine columns from multiple tables
        • Combine publishers and titles to show title and publisher name
      • Unions : combine rows from different lists
        • Create a list of editors and authors who live in California (two different tables)
      • Subqueries : use the results of one query to determine result of a second query
        • Find all books that have a higher than average price
    4. Joins
      • Inner Join: create a row for each match of values from two tables
      • Outer Join: include all rows from one table and any related data found in a second table
      • Cartesian Join: match each row in a table with every row in a second table
        • Also referred to as a cross join
    5. Inner Joins
      • Most common kind of join
      • Get related data from two tables where values match
      • May be done using FROM clause with JOIN or in WHERE clause
        • Microsoft does not fully support WHERE clause in SQL Server 2008
        • Older database management systems relied on WHERE and don’t support JOIN
    6. Inner Join With FROM Clause
      • Use the keyword JOIN with or without INNER preceeding it
      • Specify the fields to compare using ON
      • Field names that are not unique must be qualified by the table they’re in
    7. FROM Clause Examples
      • SELECT * FROM Publishers JOIN Titles ON Publishers.pub_id = Titles.pub_id
      • SELECT * FROM Publishers INNER JOIN Titles ON Publishers.pub_id = Titles.pub_id
      Optional Inner Join Keyword On Keyword
    8. Join With WHERE
      • List tables in FROM clause, separated by comma
      • WHERE clause defines the fields to compare
    9. WHERE Clause Example
        • SELECT *
        • FROM Publishers, Titles
        • WHERE Publishers.pub_id = Titles.pub_id
    10. Using An Alias
      • To shorten references to tables, can use an Alias
        • Keyword AS is optional
      • SELECT *
      • FROM Publishers AS P JOIN Titles AS T ON P.pub_id = T.pub_id
    11. Predicting Results
      • With no criteria specified, the table on the many side will determine the number of rows returned
        • An entry on the one-side can show up over and over – once for each related entry in the many table
        • If return publisher name and title, the publisher name will repeat once for each title associated with that publisher
    12. More than Two Tables
      • When have more than two tables in query, tables combined in order found unless use parentheses
        • Parentheses control order of execution if found
      • SELECT *
      • FROM publishers p JOIN titles t ON p.pub_id = t.pub_id JOIN titleauthors ta ON t.title_id = ta.title_id
      1 st Combine Publishers and Titles 2 nd Combine Publishers/Titles With TitleAuthors
    13. Outer Joins
      • Used when rows from one table should be part of the result even if there are no related rows in a second table
        • Create a list of all publishers, including any titles the publisher has published (include a publisher where there ISN’T an entry in titles)
      • Direction must be specified
        • Left/Right specify which table has the rows which should always be included
        • Full specifies that rows from both tables should be included even if no match between rows
    14. Outer Join Example
      • Include all publishers, even if they haven’t published a book
        • LEFT refers to the table on the left side of the JOIN keyword and equal sign
      • SELECT *
      • FROM Publishers LEFT OUTER JOIN
      • Titles ON Publishers.pub_id =
      • Titles.pub_id
    15. Cartesian Join
      • Typically used to generate lots of data quickly
      • Match each row from table 1 with every row from table 2
        • Result is (table 1 row count)*(table 2 row count)
    16. Cartesian Join Examples
      • Using table list:
      • SELECT *
      • FROM Publishers, Titles
      • Using CROSS JOIN keywords:
      • SELECT *
      • FROM Publishers CROSS JOIN Titles
    SlideShare Zeitgeist 2009

    + Rand RinessRand Riness Nominate

    custom

    114 views, 0 favs, 2 embeds more stats

    Introduction to Joins in SQL

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 114
      • 58 on SlideShare
      • 56 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 5
    Most viewed embeds
    • 53 views on http://angel.waol.org
    • 3 views on http://angel.spscc.edu

    more

    All embeds
    • 53 views on http://angel.waol.org
    • 3 views on http://angel.spscc.edu

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories