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 7726735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:17:26+00:00 2026-06-01T05:17:26+00:00

I have the following table id score start_time end_time 1 60 25 30 2

  • 0

I have the following table

id  score   start_time  end_time
1   60      25          30
2   85      5           10
3   90      10          15
4   100     0           20

I would like to perform the query

SELECT * FROM table

WHERE the range between start_time and end_time doesn't overlap with anything in the result set ordered by score DESC

So in this case the result set would be:

id  score   start_time  end_time
4   100     0           20
1   60      25          30

since the range between start_time and end_time for table.id =2 and table.id =3 overlapped with the range between start_time and end_time for table.id =4 and the score of table.id =4 was greater than the score of table.id =2 and table.id =3

Is it possible to do this strictly through mysql?

  • 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-01T05:17:27+00:00Added an answer on June 1, 2026 at 5:17 am

    EDIT: Sorry, had a minor mistake in there. Now it really works.

    Set up test data:

    create table test(
    id int,
    score int,
    start_time int,
    end_time int
    );
    
    insert into test values
    (5, 95, 0, 15), /*extra test case from me*/
    (1,   60,      25,          30),
    (2,   85,      5,           10),
    (3,   90,      10,          15),
    (4,   100,     0,           20)
    ;
    

    The function which is needed:

    DELIMITER $$
    DROP FUNCTION IF EXISTS checkOverlap$$
    CREATE FUNCTION checkOverlap(p_id INT, p_score INT, p_stime INT, p_etime INT)
    RETURNS BOOL
    READS SQL DATA 
    BEGIN
    DECLARE done INT DEFAULT FALSE;
    DECLARE result INT DEFAULT TRUE;
    
    DECLARE stime, etime INT;
    
    DECLARE cur1 CURSOR FOR SELECT start_time, end_time FROM test WHERE id != p_id AND score > p_score;
    
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
    
    OPEN cur1;
    
    read_loop: LOOP
    FETCH cur1 INTO stime, etime;
    IF done THEN
    LEAVE read_loop;
    END IF;
    
    IF ((p_stime >= stime AND p_stime <= etime) OR (p_etime >= stime AND p_etime <= etime)) THEN
    SET result = FALSE;
    END IF;
    END LOOP;
    
    CLOSE cur1;
    
    RETURN result;
    
    END$$
    
    DELIMITER ;
    

    How to use the function:

    select *
    from
    test
    where
    checkOverlap(id, score, start_time, end_time) = TRUE
    order by score desc
    

    P.S: Really nice question. Was fun to solve

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

Sidebar

Related Questions

I have the following table: id INT (PRIMARY) score INT I'd like to know,
I have the following query: SELECT location, step, COUNT(*), AVG(foo), YEAR(start), MONTH(start), DAY(start) FROM
I have the following used to create a dummy high score table if if
Suppose I have the following table: Table: Score field: ID: uniqueidentifier field: Departmentid: int
I have the following table: CREATE TABLE `score` ( `score_id` int(10) unsigned NOT NULL
I have the following table called stores id store zip 1 Market 1 01569
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
I have following table Id | Status | date ------------------- 1 | Onsite |2007
I have the following table in a database. I want to be able to
I have the following table: CREATE TABLE notes (noteId INTEGER PRIMARY KEY ASC, note,

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.