473,396 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Order of execution in queries

33
I have a table with one column that contains blank(''), null, numeric & varchar data. So when I wanted to find values which are numeric & between a range I wrote something like
Expand|Select|Wrap|Line Numbers
  1. select Value from Table1 
  2. where isnumeric(Value)=1 and Convert(numeric(10,2), Value) between 110.00 and 115.00
which works fine. But when I change the order of the 'where' it fails, i.e. the following SQL fails giving a conversion error...
Expand|Select|Wrap|Line Numbers
  1. select Value from Table1 
  2. where Convert(numeric(10,2), Value) between 110.00 and 115.00 and isnumeric(Value)=1
Why? Does the order matter in where? I read somewhere in case of performance improve it doesn't matter at all.

Also just for testing when I write the same query as...
Expand|Select|Wrap|Line Numbers
  1. Select AA.Value from 
  2. (select Convert(numeric(10,2), Value) as Value from Table1 where isnumeric(Value)=1) AA
  3. where Value between 110.00 and 115.00
It again fails giving conversion error. I was in the impression that the subqueries are performed first then the outside sql executes. So why in this case it's giving error? Why the 'outside where' is not performed just on the dataset fetched by the inner query?
Jul 11 '12 #1

✓ answered by ck9663

Why? Does the order matter in where?
For the most part, the sequence of condition matters in WHERE clause and is evaluated from left to right. That's why your first query did not fail because if the value of your column is not numeric, there is no need to test the other conditions. Because your conditions are INCLUSIVE (AND) where all conditions must be satisfied in order to be TRUE, the first condition already failed, no need to test the rest. This is the same reason (in reverse) why your second query failed.

However, SQL Server optimizer may, sometime, change the sequence of the condition being tested based on the way it thinks your query will run faster.

So why in this case it's giving error? Why the 'outside where' is not performed just on the dataset fetched by the inner query?
Because your inner subquery will have to run first. Since the convert() function already failed, the entire query fails.

Hope that clear things up a bit.

Happy Coding!!!


~~ CK

4 2350
ck9663
2,878 Expert 2GB
Why? Does the order matter in where?
For the most part, the sequence of condition matters in WHERE clause and is evaluated from left to right. That's why your first query did not fail because if the value of your column is not numeric, there is no need to test the other conditions. Because your conditions are INCLUSIVE (AND) where all conditions must be satisfied in order to be TRUE, the first condition already failed, no need to test the rest. This is the same reason (in reverse) why your second query failed.

However, SQL Server optimizer may, sometime, change the sequence of the condition being tested based on the way it thinks your query will run faster.

So why in this case it's giving error? Why the 'outside where' is not performed just on the dataset fetched by the inner query?
Because your inner subquery will have to run first. Since the convert() function already failed, the entire query fails.

Hope that clear things up a bit.

Happy Coding!!!


~~ CK
Jul 11 '12 #2
sanndeb
33
@ck9663
Thanks for the clarrification mate.... The first two are clear now.

But for the last one I need a little bit more clearence
Expand|Select|Wrap|Line Numbers
  1. Select AA.Value from 
  2. (select Convert(numeric(10,2), Value) as Value from Table1 where isnumeric(Value)=1) AA
  3. where Value between 110.00 and 115.00
  4.  
The bold subquery runs fine individually, i.e. 'AA' has all the rows where there's no varchar value at all, then I'm putting a where around that numeric column, still it fails. This one is not clear till now :(

Thanks for you help.
Jul 12 '12 #3
ck9663
2,878 Expert 2GB
Although SQL Server mostly access the table based on certain factor, ie. if there's index it will mostly use it, sometimes, the optimizer will not use the index and just access it sequentially. So if there's a record that will fail when it was access one way and not the other, it will.

Hope that helps...

Good Luck!!!


~~ CK
Jul 12 '12 #4
sanndeb
33
Thanks for the clarrification mate....
Jul 12 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: gary b | last post by:
Hello When I use a PreparedStatement (in jdbc) with the following query: SELECT store_groups_id FROM store_groups WHERE store_groups_id IS NOT NULL AND type = ? ORDER BY group_name
3
by: Will Atkinson | last post by:
Hi All, I'm a relative newbie to SQL Server, so please forgive me if this is a daft question... When I set "Show Execution Plan" on in Query Analyzer, and execute a (fairly complex) sproc, I...
4
by: Tryfon Gavriel | last post by:
Hi all I recently noticed when trying to optimise a major query of a chess website I am the webmaster of, that adding an order by for "gamenumber" which is a clustered index field as in for...
5
by: javier garcia - CEBAS | last post by:
Hi, I'm not an expert, not by far. I've just installed postgres7.4 and have realized tat the order of rows in queries in different to that in 7.3.4. It seems that previously it was more logic,...
5
by: Johannes Lebek | last post by:
Hi there, lately, I experienced a strange thing on my DB2 V8.1 on Windows: Some queries took a very long time. A snapshot discovered the following: Number of executions = 47...
7
by: rajdb2 | last post by:
Hi, the below list shows the top running queries in oracle on a database on a single day. Can we do something similar in DB2 v8 where we get a list of top 10 queries in execution time , or CPU...
104
by: Beowulf | last post by:
I have the view below and if I use vwRouteReference as the rowsource for a combo box in an MS Access form or run "SELECT * FROM vwRouteReference" in SQL Query Analyzer, the rows don't come through...
24
by: junky_fellow | last post by:
Hi, I read in some of the articles that there are two types of "reordering" of instructions. One at the compiler level and other at the processor/CPU level. What is the difference between...
1
by: erizy | last post by:
There are situations that exception could be throw within constructor and we usually employ auto_ptr to protect resources. But if we do not have auto_ptr can we use the following technique to handle...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.