Solved

Where to put the Where Clause

Posted on 2008-10-01
2
202 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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