Solved

Where to put the Where Clause

Posted on 2008-10-01
2
197 Views
Last Modified: 2010-03-20
This should be a simple question...

I am using UNION to join multiple tables with the same columns. At the end of the my UNION I am aliasing the whole thing as a tableX. After that I have a WHERE statement and a GROUP statement. Efficiency is of great importance! I want to know if it makes a difference from an effeciency standpoint if I repeat the WHERE and GROUP clause with each table in the union, or if I just have it at the end after the alais.
0
Comment
Question by:MapleMale
2 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
the WHERE clause should be per each SELECT.
SELECT ...
  FROM ...
 WHERE ...
 GROUP BY ...
UNION ALL
SELECT ...
  FROM ...
 WHERE ... 
 GROUP BY ...

Open in new window

0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 250 total points
Comment Utility
If you put your where clause and group by in each part of the union, then the # of records returned will be lower in each part of the union.  Also, your where and group by can utilize the indexes of the table.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now