Solved

Where to put the Where Clause

Posted on 2008-10-01
2
201 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 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22615514
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
ID: 22615530
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

756 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