473,513 Members | 2,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Difference between IN and OR

madhoriya22
252 Contributor
HI,
I am using this query to get the count from table
Expand|Select|Wrap|Line Numbers
  1.  
  2. select count(*) from defect_detail where status = 'RESOLVED' OR 'CLOSED' OR 'VERIFIED'
  3.  
It is retruning count 15. And when I am using this query
Expand|Select|Wrap|Line Numbers
  1.  
  2. select count(*) from defect_detail where status in ( 'RESOLVED' , 'CLOSED' , 'VERIFIED')
  3.  
Then it is returnin count as 19. Which one is the correct?
Aug 29 '07 #1
3 1129
pbmods
5,821 Recognized Expert Expert
Heya, madhoriya.

This is more correct than either of the two:
Expand|Select|Wrap|Line Numbers
  1. select count(*) from defect_detail where status in ('CLOSED', 'RESOLVED', 'VERIFIED');
  2.  
Note that the items in the IN subclause are sorted. This way, MySQL can use a binary search on the options, which is much faster than going through the list sequentially.

True, MySQL will automatically sort the items for you, but why make it do all that extra work when the values are constants?

Admittedly, when you only have three options, the time savings isn't great, but it's a good habit to get into.

The problem with this statement, which I will go ahead and 'explode' so you can get a better picture of what's going on:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         COUNT(*)
  3.     FROM
  4.         `defect_detail`
  5.     WHERE
  6.     (
  7.             `status` = 'RESOLVED'
  8.         OR
  9.             'CLOSED'
  10.         OR
  11.             'VERIFIED'
  12.     )
  13.  
Note that MySQL will only check to see if `status` = 'RESOLVED'. When you use "OR 'CLOSED'", MySQL evaluates 'CLOSED' as true, so in essence your query is doing this:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         COUNT(*)
  3.     FROM
  4.         `defect_detail`
  5.     WHERE
  6.     (
  7.             `status` = 'RESOLVED'
  8.         OR
  9.             true
  10.         OR
  11.             true
  12.     )
  13.  
which, of course actually counts ALL the records in the table.

Now, you could do this:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         COUNT(*)
  3.     FROM
  4.         `defect_detail`
  5.     WHERE
  6.     (
  7.             `status` = 'RESOLVED'
  8.         OR
  9.             `status` = 'CLOSED'
  10.         OR
  11.             `status` = 'VERIFIED'
  12.     )
  13.  
but it's MUCH faster, especially as your table grows in size, to use IN instead:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         COUNT(*)
  3.     FROM
  4.         `defect_detail`
  5.     WHERE
  6.         `status`
  7.             IN
  8.             (
  9.                 'CLOSED',
  10.                 'RESOLVED',
  11.                 'VERIFIED'
  12.             )
  13.  
Aug 29 '07 #2
madhoriya22
252 Contributor
Heya, madhoriya.

This is more correct than either of the two:
Expand|Select|Wrap|Line Numbers
  1. select count(*) from defect_detail where status in ('CLOSED', 'RESOLVED', 'VERIFIED');
  2.  
Note that the items in the IN subclause are sorted. This way, MySQL can use a binary search on the options, which is much faster than going through the list sequentially.

True, MySQL will automatically sort the items for you, but why make it do all that extra work when the values are constants?

Admittedly, when you only have three options, the time savings isn't great, but it's a good habit to get into.

The problem with this statement, which I will go ahead and 'explode' so you can get a better picture of what's going on:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2. COUNT(*)
  3. FROM
  4. `defect_detail`
  5. WHERE
  6. (
  7. `status` = 'RESOLVED'
  8. OR
  9. 'CLOSED'
  10. OR
  11. 'VERIFIED'
  12. )
  13.  
Note that MySQL will only check to see if `status` = 'RESOLVED'. When you use "OR 'CLOSED'", MySQL evaluates 'CLOSED' as true, so in essence your query is doing this:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2. COUNT(*)
  3. FROM
  4. `defect_detail`
  5. WHERE
  6. (
  7. `status` = 'RESOLVED'
  8. OR
  9. true
  10. OR
  11. true
  12. )
  13.  
which, of course actually counts ALL the records in the table.

Now, you could do this:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2. COUNT(*)
  3. FROM
  4. `defect_detail`
  5. WHERE
  6. (
  7. `status` = 'RESOLVED'
  8. OR
  9. `status` = 'CLOSED'
  10. OR
  11. `status` = 'VERIFIED'
  12. )
  13.  
but it's MUCH faster, especially as your table grows in size, to use IN instead:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2. COUNT(*)
  3. FROM
  4. `defect_detail`
  5. WHERE
  6. `status`
  7. IN
  8. (
  9. 'CLOSED',
  10. 'RESOLVED',
  11. 'VERIFIED'
  12. )
  13.  
Hi,
Thanks buddy. Nice description ! :)
Aug 29 '07 #3
pbmods
5,821 Recognized Expert Expert
Heya, Madhoriya.

We aim to please.

Good luck with your project, and if you ever need anything, post back anytime :)
Aug 29 '07 #4

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

Similar topics

34
7027
by: yensao | last post by:
Hi, I have a hard time to understand difference and similarities between Relational database model and the Object-Oriented model. Can somebody help me with this? Thank you in advance. ...
21
2967
by: b83503104 | last post by:
Hi, Can someone tell me the difference between single quote and double quote? Thanks
26
4370
by: Frank | last post by:
For my website i would like to display the age of my son in years, months, days and hours. For now i manage to get a result for totals. Like the total number of days. This is the beginning: ...
21
2812
by: Rich | last post by:
I was considering C# for developing a scientific application, but I have noticed a ~30% difference between VC++ .NET and C# on the same machine, under identical conditions: double a = 0,b = 0, c...
4
15669
by: jamesyreid | last post by:
Hi, I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere. Does anyone have a snippet of JavaScript code I could borrow...
3
4142
by: bbawa1 | last post by:
Hi, I have a table which has a field ItemsReceived of type datetime. I have a grid view which has two columns. In first column i have to show the data from field ItemsReceived and in second...
12
2698
by: Petronius | last post by:
Hallo, does anyone have an idea how to implement difference lists in Javascript? Thanks allot in advance
5
3474
by: Julius | last post by:
Hej dudes, I need to calc the difference between two timestamps / dates ... For example what i need to calculate: Date 1: 2007.11.06 - 20:13:04 Date 2: 2007.11.07 - 21:13:04 Difference:...
9
2661
by: viki1967 | last post by:
Hi all! This new forum its great! :) Congratulations !!! My answer: why this my code not working? Nothing error but not work the difference.... : <html>
11
12088
by: cmb3587 | last post by:
I have two arrays and I'm trying to create a 3rd array that is the difference between the two arrays Ex: arrayA: 3 5 8 9 arrayB: 3 4 6 9 difference of A-B: 5 8 however, my...
0
7158
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
7380
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
7535
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
7523
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5683
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5085
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.