title: Count number of rows returned in a SQL statement
body: <p>Are there any DB engines that allow you to run an <code>EXPLAIN</code> (or other function) where it will give you an approximate count of values that may be returned before an aggregation is run (not rows scanned but that actually would be returned)? For example, in the following query:</p>
  <pre><code>SELECT gender, COUNT(1) FROM sales JOIN (
         SELECT id, person FROM sales2 WHERE country='US'
         GROUP BY person_id
  ) USING (id)
  WHERE sales.age &gt; 20
  GROUP BY gender
  </code></pre>
  <p>Let's say this query returns 3 rows after being aggregated, but would return 170M rows if unaggregated.</p>
  <p>Are there any tools where you can run the query to get this '170M' number or does this have to do with complexity theory (or something similar) where it's almost just as expensive to run the query (without the final aggregation/having/sort/limit/etc) to get the count? In other words, doing a rewrite to:</p>
  <pre><code>SELECT COUNT(1) FROM sales JOIN (
         SELECT id, person FROM sales2 WHERE country='US'
         GROUP BY person_id
  ) USING (id)
  WHERE sales.age &gt; 20
  </code></pre>
  <p>But having to execute the query nonetheless.</p>
  <p>As an example of using the current (mysql) explain to show how 'off' it is to get what I'm looking for:</p>
  <pre><code>explain select * from movies where title&gt;'a';
  # rows=147900
  select count(1) from _tracktitle where title&gt;'a';
  # 144647 --&gt; OK, pretty close
  
  
  explain select * from movies where title&gt;'u';
  # rows=147900
  select * from movies where title&gt;'u';
  # 11816 --&gt; Not close at all
  </code></pre>