Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 10th, 2007, 01:21 AM
Registered User
 
Join Date: Nov 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default redundant SQL logic

I'm a newbie to SQL so this might be a simple question...I was given the SQL syntax below but I think it could change to the second SQL statement further below

SELECT age,
       highage
FROM agetable
WHERE (highage IS NULL
        AND age BETWEEN '20' AND '30')
        OR ((highage IS NOT NULL)
             OR (age BETWEEN '20' AND '30'
                  OR highage BETWEEN '20' AND '30'))

I think it just could be reduced down to:

SELECT age,
       highage
FROM agetable
WHERE (age BETWEEN '20' AND '30'
         OR highage BETWEEN '20' AND '30')

Don't the 'highage IS NULL' and the 'highage IS NOT NULL' statements just cancel each other out b/c of the OR?

Thanks.

Reply With Quote
  #2 (permalink)  
Old November 10th, 2007, 03:12 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there.. a quick view says no... look at what are you doing in the first case.. you are selecting rows that futfill this cases:
A) age between 20 and 30 and no highage
b) has a highage OR age or highage between 20 and 30..

there are to diferents conditions.. at least at first sigh...

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
Reply With Quote
  #3 (permalink)  
Old November 11th, 2007, 12:25 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

As Gonzalo said, the answer is no.

The reason is because IS NULL and IS NOT NULL appear on different sides of the OR operator hence do not cancel each other out.

If you are only trying to get rows where the highage is 20 to 30, however, you should remove the (highage IS NOT NULL) expression as this will always evaluate to true if the left side of your or condition has failed and thus will allow any highage value that isnt null.

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor :.
Wrox Books 24 x 7
================================================== =========
Reply With Quote
  #4 (permalink)  
Old November 12th, 2007, 01:28 PM
Registered User
 
Join Date: Nov 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the input!
F

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Entirely redundant function? RoryBecker BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 0 May 30th, 2008 05:16 AM
Creating a report and need assistance w/ SQL Logic avididy BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 0 January 4th, 2008 02:45 PM
Eliminating Redundant Records spraveens MySQL 1 May 12th, 2004 04:21 AM
Mixing Data access logic and business logic polrtex BOOK: Professional Jakarta Struts 0 December 15th, 2003 07:19 PM



All times are GMT -4. The time now is 06:34 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.