Differences

This shows you the differences between two versions of the page.

Link to this comparison view

sql_joins [2008/12/02 16:09] (current)
Joel Dare created
Line 1: Line 1:
 +====== SQL Joins ======
  
 +===== Inner Join =====
 +
 +Return the rows from both tables where the row in the left table and the row in the right table are relevant based on the where clause.
 +Select main.id, main.name, location.city,​ location.state from main, location where main.id = address.id;
 +
 +This example returns only the records in the main table where there is also a record in the location table.
 +
 +===== Left Join =====
 +
 +Return the rows from the left table regardless of the presence of data in matching rows from the right table. When there is no matching data in the right table, Null is returned.
 +Select main.id, main.name, location.city,​ location.state from main left join location on main.id = location.id;​
 +
 +This example returns all the records in the main table and null values where there is no record in the location table.
 +
 +===== Right Join =====
 +
 +Return the rows from the right table regardless of the presence of data in matching rows from the left table. When there is no matching data in the left table, Null is returned.
 +Select main.id, main.name, location.city,​ location.state from main right join location on main.id = location.id;​
 +
 +This example returns all of the records in the location table and null values where there is no record in the main table.
 +
 +===== Cross Join =====
 +
 +Return each row from the left table with all of the rows of the right table. This results in all combinations of the row sets.
 +Select months.name,​ years.number from months, years;
 +
 +This example returns all of the records in the months table with all of the records in the years table, showing all combinations.
 

comments powered by Disqus
sql_joins.txt ยท Last modified: 2008/12/02 16:09 by Joel Dare