Differences

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

Link to this comparison view

Next revision
Previous revision
swdev:howto:perform_sql_queries [2009/01/28 08:56]
127.0.0.1 external edit
swdev:howto:perform_sql_queries [2015/06/05 10:30] (current)
smayr
Line 1: Line 1:
 +== Performing SQL Queries ==
  
 +== SQL JOINs ==
  
-== Sample Queries ==+{{:swdev:howto:sql_joins.svg.png?700|Visual Representation of SQL Joins}}
  
 +  * INNER JOIN: returns rows when there is a match in both tables. <code sql>SELECT *
 +FROM `t1`
 +INNER JOIN `t2` ON `t1`.`id` = `t2`.`id`;</code>
 +  * LEFT JOIN: returns all rows from the left table, even if there are no matches in the right table. <code sql>SELECT *
 +FROM `t1`
 +LEFT OUTER JOIN `t2` ON `t1`.`id` = `t2`.`id`;</code>
 +  * RIGHT JOIN: returns all rows from the right table, even if there are no matches in the left table. <code sql>SELECT *
 +FROM `t1`
 +RIGHT OUTER JOIN `t2` ON `t1`.`id` = `t2`.`id`;</code>
 +  * FULL JOIN: returns rows when there is a match in one of the tables. <code sql>/* MySQL requires a UNION */
 +(SELECT ... FROM tbl1 LEFT JOIN tbl2 ...) 
 +UNION ALL /* to see duplicates (instead of UNION) */
 +(SELECT ... FROM tbl1 RIGHT JOIN tbl2 ... 
 +WHERE tbl1.col IS NULL /* To display duplicates in MySQL */)</code>
 +  * SELF JOIN: is used to join a table to itself as if the table were two tables, temporarily renaming at least one table in the SQL statement.
 +  * CARTESIAN JOIN: returns the Cartesian product of the sets of records from the two or more joined tables.
 +
 +Source:
 +  * [[http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins|CodeProject: Visual Representations of SQL Joins]]
 +  * [[http://en.wikipedia.org/wiki/Join_(SQL)|Wikipedia: JOIN (SQL)]]
 +  * [[http://stackoverflow.com/questions/5706437/whats-the-difference-between-inner-join-left-join-right-join-and-full-join|StackOverflow: Difference between JOINs]]
 +  * Image: [[http://commons.wikimedia.org/wiki/File%3aSQL_Joins.svg|SQL JOINs]]
 +== Sample Queries ==
  
 === Find Distinct Records (keeping the higher value) === === Find Distinct Records (keeping the higher value) ===