Difference between revisions of "Forgotten rows"

From SQLZOO
Jump to: navigation, search
 
(3 intermediate revisions by one user not shown)
Line 1: Line 1:
 
<p>In this example you are shown how to include the rows your <code>JOIN</code> function automatically leaves out as it does not perform the join when one of the values is 0.</p>
 
<p>In this example you are shown how to include the rows your <code>JOIN</code> function automatically leaves out as it does not perform the join when one of the values is 0.</p>
 
<p>This is done by adding a either a <code>LEFT JOIN</code> or a <code>UNION</code> making the join also to reveal the rows with a count of 0.</p>
 
<p>This is done by adding a either a <code>LEFT JOIN</code> or a <code>UNION</code> making the join also to reveal the rows with a count of 0.</p>
 +
<p>Table 1 shows the results without a <code>LEFT JOIN</code> and table 2 shows the results we obtain with the <code>LEFT JOIN</code></p>
 +
<div class="ref_section">
 +
<table class= "db_ref">
 +
<caption>Table 1</caption>
 +
<tr><th align='center'>'''name'''</th><th>'''COUNT(custid)'''</th></tr>
 +
<tr><td align='left'>Betty</td><td align='left'>2</td></tr>
 +
<tr><td align='left'>Janette</td><td align='left'>1</td></tr>
 +
</table>
 +
<table class="db_ref">
 +
<caption>Table 2</caption>
 +
<tr><th align='center'>'''name'''</th><th>'''COUNT(custid)'''</th></tr>
 +
<tr><td align='left'>Betty</td><td align='left'>2</td></tr>
 +
<tr><td align='left'>Janette</td><td align='left'>1</td></tr>
 +
<tr><td align='left'>Robert</td><td align='left'>0</td></tr>
 +
 +
</table>
 +
</div>
 
<div class='ht'>
 
<div class='ht'>
 
<div class=params>schema:scott</div>
 
<div class=params>schema:scott</div>
Line 24: Line 41:
 
not include rows with a count of 0.</p>
 
not include rows with a count of 0.</p>
 
  SELECT name, COUNT(custid)
 
  SELECT name, COUNT(custid)
FROM customer JOIN invoice ON (id=custid)
+
  FROM customer JOIN invoice ON (id=custid)
GROUP BY name
+
  GROUP BY name
 
<p>In order to obtain the rows where the count from the query is 0 a
 
<p>In order to obtain the rows where the count from the query is 0 a
 
<code>LEFT JOIN</code> or a <code>UNION</code> can be used.</p>
 
<code>LEFT JOIN</code> or a <code>UNION</code> can be used.</p>
 
</div>
 
</div>
<source lang='sql' class='def'> SELECT name, COUNT(*)
+
<source lang='sql' class='def'>  
FROM customer LEFT JOIN invoice ON (id=custid)
+
SELECT name, COUNT(custid)
GROUP BY name</source>
+
  FROM customer LEFT JOIN invoice ON (id=custid)
 +
  GROUP BY name</source>
 
<div class="ecomm e-mysql" style="display: none"></div>
 
<div class="ecomm e-mysql" style="display: none"></div>
 
<div class="ecomm e-oracle" style="display: none"></div>
 
<div class="ecomm e-oracle" style="display: none"></div>
 
</div>
 
</div>
 
{{Hacks Ref}}
 
{{Hacks Ref}}

Latest revision as of 16:03, 2 August 2012

In this example you are shown how to include the rows your JOIN function automatically leaves out as it does not perform the join when one of the values is 0.

This is done by adding a either a LEFT JOIN or a UNION making the join also to reveal the rows with a count of 0.

Table 1 shows the results without a LEFT JOIN and table 2 shows the results we obtain with the LEFT JOIN

Table 1
nameCOUNT(custid)
Betty2
Janette1
Table 2
nameCOUNT(custid)
Betty2
Janette1
Robert0
schema:scott
DROP TABLE customer;
DROP TABLE invoice;
 CREATE TABLE customer(
  id INTEGER,
  name VARCHAR(20));
INSERT INTO customer VALUES (1,'Betty');
INSERT INTO customer VALUES (2,'Robert');
INSERT INTO customer VALUES (3,'Janette');
CREATE TABLE invoice(
  invoiceno INTEGER,
  whn DATE,
  custid INTEGER,
  cost INTEGER );
INSERT INTO invoice VALUES (1,'2006-11-01',1,100);
INSERT INTO invoice VALUES (2,'2006-11-05',1,500);
INSERT INTO invoice VALUES (3,'2006-11-11',3,200);

The following query will only give two rows as the JOIN function automatically does not include rows with a count of 0.

SELECT name, COUNT(custid)
  FROM customer JOIN invoice ON (id=custid)
  GROUP BY name

In order to obtain the rows where the count from the query is 0 a LEFT JOIN or a UNION can be used.

 
SELECT name, COUNT(custid)
  FROM customer LEFT JOIN invoice ON (id=custid)
  GROUP BY name
Personal tools
Namespaces

Variants
Actions
Reference
Toolbox
Google AdSense