Difference between revisions of "Combinations"

From SQLZOO
Jump to: navigation, search
 
(2 intermediate revisions by one user not shown)
Line 1: Line 1:
A <code>JOIN</code> with no join conditions results in every row in one table being connected to every row in another table, forming all possible row combinations.  
+
<p>A <code>JOIN</code> with no join conditions results in every row in one table being connected to every row in another table, forming all possible row combinations.</p>
Often this is done by mistake, but it can be useful. In this example we use this as an advantage to show the goals
+
<p>Often this is done by mistake, but it can be useful. In this example we use this as an advantage to show the goals
made for all games.
+
made for all games.</p>
 
<div class='ht'>
 
<div class='ht'>
 
<div class=params>schema:scott</div>
 
<div class=params>schema:scott</div>
Line 23: Line 23:
 
INSERT INTO tscores VALUES ('Wildcats', 'Toads', 0, 0);
 
INSERT INTO tscores VALUES ('Wildcats', 'Toads', 0, 0);
 
INSERT INTO tscores VALUES ('Lions', 'Tigers', 0, 0);
 
INSERT INTO tscores VALUES ('Lions', 'Tigers', 0, 0);
 +
INSERT INTO tscores VALUES ('Tigers', 'Lions', 0, 0);
 +
INSERT INTO tscores VALUES ('Toads', 'Wildcats', 0, 0);
 +
INSERT INTO tscores VALUES ('Toads', 'Lions', 0, 0);
 +
INSERT INTO tscores VALUES ('Tigers', 'Wildcats', 0, 0);
 +
INSERT INTO tscores VALUES ('Tigers', 'Toads', 0, 0);
 +
INSERT INTO tscores VALUES ('Wildcats', 'Lions', 0, 0);
 
</source>
 
</source>
 
<div>
 
<div>
Line 28: Line 34:
 
<p>Conditions as well as a <code>LEFT JOIN</code> are then used to filter the query and make it more accurate.</p>
 
<p>Conditions as well as a <code>LEFT JOIN</code> are then used to filter the query and make it more accurate.</p>
 
</div>
 
</div>
<source lang='sql' class='def'>SELECT home.teamname Home, away.teamname Away,  
+
<source lang='sql' class='def'>
 +
SELECT home.teamname Home, away.teamname Away,  
 
tscores.homescore, tscores.awayscore
 
tscores.homescore, tscores.awayscore
FROM teams home CROSS JOIN teams away LEFT JOIN tscores ON
+
  FROM teams home CROSS JOIN teams away LEFT JOIN tscores ON
 
   (home.teamname = tscores.hometeam
 
   (home.teamname = tscores.hometeam
 
   AND tscores.awayteam = away.teamname)
 
   AND tscores.awayteam = away.teamname)
WHERE home.teamname != away.teamname</source>
+
  WHERE home.teamname != away.teamname</source>
 
<div class="ecomm e-mysql" style="display: none"></div>
 
<div class="ecomm e-mysql" style="display: none"></div>
 
</div>
 
</div>
 
{{Hacks Ref}}
 
{{Hacks Ref}}

Latest revision as of 15:00, 2 August 2012

A JOIN with no join conditions results in every row in one table being connected to every row in another table, forming all possible row combinations.

Often this is done by mistake, but it can be useful. In this example we use this as an advantage to show the goals made for all games.

schema:scott
DROP TABLE teams;
DROP TABLE tscores
 CREATE TABLE teams(
  teamname VARCHAR(20));
INSERT INTO teams VALUES ('Lions');
INSERT INTO teams VALUES ('Tigers');
INSERT INTO teams VALUES ('Wildcats');
INSERT INTO teams VALUES ('Toads');
CREATE TABLE tscores (
  hometeam VARCHAR (20),
  awayteam VARCHAR (20),
  homescore INTEGER,
  awayscore INTEGER );
INSERT INTO tscores VALUES ('Lions', 'Wildcats', 1, 4);
INSERT INTO tscores VALUES ('Toads', 'Tigers', 3, 5);
INSERT INTO tscores VALUES ('Wildcats', 'Tigers', 0, 0);
INSERT INTO tscores VALUES ('Lions', 'Toads', 0, 0);
INSERT INTO tscores VALUES ('Wildcats', 'Toads', 0, 0);
INSERT INTO tscores VALUES ('Lions', 'Tigers', 0, 0);
INSERT INTO tscores VALUES ('Tigers', 'Lions', 0, 0);
INSERT INTO tscores VALUES ('Toads', 'Wildcats', 0, 0);
INSERT INTO tscores VALUES ('Toads', 'Lions', 0, 0);
INSERT INTO tscores VALUES ('Tigers', 'Wildcats', 0, 0);
INSERT INTO tscores VALUES ('Tigers', 'Toads', 0, 0);
INSERT INTO tscores VALUES ('Wildcats', 'Lions', 0, 0);

To get all possible values a CROSS JOIN is used.

Conditions as well as a LEFT JOIN are then used to filter the query and make it more accurate.

SELECT home.teamname Home, away.teamname Away, 
tscores.homescore, tscores.awayscore
  FROM teams home CROSS JOIN teams away LEFT JOIN tscores ON
  (home.teamname = tscores.hometeam
   AND tscores.awayteam = away.teamname)
  WHERE home.teamname != away.teamname
Personal tools
Namespaces

Variants
Actions
Reference
Toolbox
Google AdSense