Read my latest article: Planet Argon Blog (posted Wed, 17 Feb 2010 15:11:00 GMT)

Using BETWEEN for SQL comparisons

Posted by Robby Russell Sat, 14 Nov 2009 20:55:00 GMT

10 comments Latest by ed hardy sunglass Wed, 10 Mar 2010 08:12:30 GMT

Recently, Carlos, suggested that I should start sharing some basic SQL tips that help with performance and/or general usage. I recently came across some code that I didn’t like to read and/or write. For example, let’s take the following…


SELECT * FROM brochures WHERE published_at <= now() AND archived_at >= now()

Essentially, this is pulling back some data WHERE the the brochures are considered published. (We have a project that allows people to manage their brochure launch dates ahead of time.) In fact, in this project, we have no less than 6-8 dates in the database that we’re comparing data on and it’s easy to get lost in the logic when trying to understand it.

Now, there isn’t anything inheriently wrong with how this condition is constuctued. As a matter of personal taste, I find it annoying to mentally parse. Also, I find having to write now() more than once in a WHERE clause to feel like I’m repeating myself.

Read it outloud…

“WHERE the brochures published at date is less than and/or equal to right now AND the archived date is greater than and/or equal to now.”

Who talks like that?

Luckily, there is a better and in my opinion, a more readable way to express this is with the BETWEEN construct in SQL. (postgresql docs, mysql docs)


SELECT * FROM brochures WHERE now() BETWEEN published_at AND archived_at

Let’s read this outloud…

“WHERE the current date is between the published at and archived at dates.”

This sounds more natural to me.

Additionally, you can also do the inverse with NOT.


SELECT ... WHERE now() NOT BETWEEN brochures.published_at AND brochures.archive_at

Remember kids, “code is for humans first and computers second.”—Martin Fowler

Subscribe to my RSS feed Enjoying the content? Be sure to subscribe to my RSS feed.
Comments

Leave a response

  1. Avatar
    Duncan Beevers Sun, 15 Nov 2009 02:03:14 GMT

    Indeed, much nicer. When you provide a Range to ActiveRecord’s :conditions it generates a BETWEEN.

    From http://api.rubyonrails.org/classes/ActiveRecord/Base.html

    A range may be used in the hash to use the SQL BETWEEN operator: Student.find(:all, :conditions => { :grade => 9..12 })
  2. Avatar
    ипотпал Mon, 16 Nov 2009 00:01:16 GMT

    Is there a rails statement, function for between?

  3. Avatar
    Paul Charles Leddy Mon, 16 Nov 2009 16:14:43 GMT

    I just learned some sub-selects using GROUP BY. Still amazed a week later. Where have I been all these years?

  4. Avatar
    Credit Cards Offers Wed, 17 Feb 2010 06:44:48 GMT

    This was an excellent read. I am glad you shred it with us.

  5. Avatar
    michael jordan shoes Mon, 22 Feb 2010 03:05:37 GMT
  6. Avatar
    jordan shoes Sat, 27 Feb 2010 06:35:11 GMT

    Thank you for your sharing. Looking For discount jordan shoes? The store online sells the air jordan shoes. Welcome to visit and buy michael jordan shoes.

  7. Avatar
    chi flat iron Tue, 02 Mar 2010 09:21:13 GMT

    Thank you this article I hope you have an enjoyable day. gucci shoes gucci handbags gucci handbags gucci belts

  8. Avatar
    abercrombie clothes Wed, 10 Mar 2010 07:55:48 GMT

    good article and enjoy it

  9. Avatar
    replica pandora jewelry Wed, 10 Mar 2010 08:01:31 GMT

    good

  10. Avatar
    ed hardy sunglass Wed, 10 Mar 2010 08:12:30 GMT

    Thank you for your post! Thant is great!

Share your thoughts... (really...I want to hear them)

Comments