Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8942761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:35:59+00:00 2026-06-15T11:35:59+00:00

I need to select from a table that has a starts_on field and an

  • 0

I need to select from a table that has a starts_on field and an ends_on field.
I need to pass start date and end date for filtering and retrieving those objects.

At the moment it’s working and I use the following:

SELECT * FROM ***
WHERE ((starts_on >= START_DATE AND starts_on <= END_DATE) OR
       (ends_on >= START_DATE AND ends_on <= END_DATE) OR
       (starts_on <= END_DATE AND ends_on >= END_DATE))
ORDER BY starts_on, id

It looks a bit messy, but can’t see an easy way to simplify it. Any idea?
I’m using postgres 9.1 as dbms.

Edit:

 starts_on   | timestamp without time zone | 
 ends_on     | timestamp without time zone | 

Ex: if one entry has starts_on = ‘2012/02/02’ and ends_on ‘2012/02/05’ I want the following behavior:

  • if I filter by start date 2012/01/01 and end date 2012/03/01 I want the item to be returned
  • if I filter by start date 2012/02/04 and end date 2012/03/01 I want the item to be returned
  • if I filter by start date 2012/02/05 and end date 2012/03/01 I want the item to be returned
  • if I filter by start date 2012/02/04 and end date 2012/02/04 I want the item to be returned
  • if I filter by start date 2012/02/06 and end date 2012/03/01 I want the item to NOT be returned
  • if I filter by start date 2012/01/01 and end date 2012/02/01 I want the item to NOT be returned
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:36:00+00:00Added an answer on June 15, 2026 at 11:36 am

    Query

    If you want all rows where the time period between starts_on and ends_on overlaps with the passed time period of START_DATE and END_DATE, and "end" is always later than "start", and all involved columns are of type timestamp (as opposed to time or date), this simpler query does the job:

    SELECT *
    FROM   tbl
    WHERE  starts_on <= END_DATE
    AND    ends_on   >= START_DATE
    ORDER  BY starts_on, id;
    

    Fits the question as later clarified.

    Index

    The best index for this query would be a multi-column index like:

    CREATE INDEX tbl_range_idx ON tbl (starts_on, ends_on DESC)
    

    Would work with DESC / ASC almost as well, because an index can be searched in both directions almost equally well.

    How do I figure?

    The index is searched on the first condition starts_on <= END_DATE, qualifying rows are at the beginning.
    From there, Postgres can take all rows that end late enough according to ends_on >= START_DATE. Qualifying rows come first. Optimal index.

    But don’t just take my word – test performance with EXPLAIN ANALYZE. Run a couple of times to exclude caching effects.

    There is also the OVERLAPS operator for the same purpose. Simplifies the logic, but isn’t superior otherwise.

    And there are the new range types in PostgreSQL 9.2, with their own operators. Not for 9.1 though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to do the following: Select Usernames from table delivery where date>2012-06-01 00:00:00
I need to select a field called ProgramID from a table and if the
I need to select data from one table and insert it into another table.
I need to select only 20 records from the table and than add pagination
I need to select rows randomly from an Oracle DB. Ex: Assume a table
I need to select multiple records I use SELECT * FROM `table` WHERE `ID`
I have a report that I need to create from a UserHistory table in
I need to select all from table1 where active=1 and replace t1cid,t2cid,L1cid,L2cid---L10cid with corresponding
I need to select data from 2 tables in my DB - comments and
I need to select all columns from two tables, but need to be able

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.