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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:06:13+00:00 2026-05-14T06:06:13+00:00

Customers Holidays id | name customer_id | start | end —+—— ————+——–+—— 1 |

  • 0
Customers            Holidays

id | name            customer_id | start  | end
---+------           ------------+--------+------
 1 | Peter           1           |   5    | 10
 2 | Simon           1           |  15    | 20
 3 | Mary            2           |   5    | 20

What’s a working SQL query that gives me all customers without holidays on a specific date? E.g. date=12

  • Peter
  • Mary

Is this even manageable with a simple SQL join, or do I need to use sub-queries?

  • 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-05-14T06:06:14+00:00Added an answer on May 14, 2026 at 6:06 am

    First create a query that finds the opposite of what you want: the customers who do have a holiday on that specific date:

    SELECT DISTINCT name
    FROM Customers
    JOIN Holidays
    ON id = customer_id
    WHERE start <= 12 AND end >= 12
    

    Result:

    Simon
    

    Then join this result back to the customer table and select the results where the join fails:

    SELECT name
    FROM Customers
    LEFT JOIN (
        SELECT DISTINCT id
        FROM Customers
        JOIN Holidays
        ON id = customer_id
        WHERE start <= 12 AND end >= 12
    ) AS T1
    ON Customers.id = T1.id
    WHERE T1.id IS NULL
    

    Result:

    Peter
    Mary
    

    Note that a JOIN isn’t the only alternative here. You could also use NOT EXISTS, NOT IN or EXCEPT. Since you didn’t specify which database, I chose JOIN because it is a portable and efficient way to do it in all the major relational databases.

    The test data I used (taken from the question):

    CREATE TABLE Holidays (customer_id INT NOT NULL, start INT NOT NULL, end INT NOT NULL);
    INSERT INTO Holidays (customer_id, start, end) VALUES
    (1, 5, 10),
    (1, 15, 20),
    (2, 5, 20);
    
    CREATE TABLE Customers (id INT NOT NULL, name NVARCHAR(100) NOT NULL);
    INSERT INTO Customers (id, name) VALUES
    (1, 'Peter'),
    (2, 'Simon'),
    (3, 'Mary');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.