 <p>Try putting the individual <code>SELECT</code>s in parentheses: </p> <pre><code>(SELECT ... ) UNION (SELECT ... ) </code></pre> <p>The way you have it now, the second <code>WHERE</code> and <code>GROUP BY</code> clauses are ambiguous - should that apply to the SELECT, or to the UNION? I don't have any way to tell, and neither has your DB server.</p>