Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 10th, 2007, 01:21 AM
Registered User
 
Join Date: Nov 2007
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.

 
Old November 10th, 2007, 03:12 PM
Friend of Wrox
 
Join Date: Jun 2003
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
================================================== =========
 
Old November 11th, 2007, 12:25 AM
Wrox Author
 
Join Date: Oct 2005
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
================================================== =========
 
Old November 12th, 2007, 01:28 PM
Registered User
 
Join Date: Nov 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the input!
F






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





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