Your SlideShare is downloading. ×
  • Like
Joins
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply
Published

Introduction to Joins in SQL

Introduction to Joins in SQL

Published in Education , Business , Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,352
On SlideShare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
75
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

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 • 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 – Also referred to as an equi-join • May be done using FROM clause or WHERE clause – Microsoft does not fully support WHERE clause in SQL Server 2008
  • 6. Join With FROM • 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
  • 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. 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 • 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
  • 11. Outer Join Example • Include all publishers, even if they haven’t published a book: SELECT * FROM Publishers LEFT OUTER JOIN Titles ON Publishers.pub_id = Titles.pub_id
  • 12. Cartesian Join • Match each row from table 1 with every row from table 2 – Result is (table 1 row count)*(table 2 row count) • May be done using CROSS JOIN SELECT * FROM Publishers, Titles SELECT * FROM Publishers CROSS JOIN Titles