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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:38:36+00:00 2026-05-11T03:38:36+00:00

In MySQL, how can you select data where every row meets a certain condition?

  • 0

In MySQL, how can you select data where every row meets a certain condition? For example lets say I have a table showing when employees arrived at work, it has three fields:

CREATE TABLE ArrivalTimes (UserID INT ,Day DATE  ,ArrivalTime TIME ); 

I want to select all UserIDs of employees who have never been late (arrived 9am or earlier), what’s the best way to do this?

  • 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. 2026-05-11T03:38:37+00:00Added an answer on May 11, 2026 at 3:38 am

    The answers from @jjclarkson and @davethegr8 are close, but you can’t put aggregate functions in the WHERE clause. The WHERE clause is evaluated for each row.

    You need to evaluate the MAX() expression for each group, so you need to use a HAVING clause.

    Try this:

    SELECT UserID  FROM ArrivalTimes GROUP BY UserID HAVING MAX(ArrivalTime) <= '09:00:00'; 

    @MBCook comments that HAVING can be slow. You’re right, it might not be the absolute quickest way to produce the desired result. But the HAVING solution is the most clear. There are situations where performance has lower priority than clarity and maintainability.

    I looked at the EXPLAIN output (on MySQL 5.1.30) for the HAVING solution: no indexes were used, and the extra notes said ‘Using temporary; Using filesort,’ which usually means performance will be poor.

    Consider the following query:

    SELECT DISTINCT a1.UserID FROM ArrivalTimes a1   LEFT OUTER JOIN ArrivalTimes a2    ON (a1.UserID = a2.UserID AND a2.ArrivalTime > '09:00:00') WHERE a2.UserID IS NULL; 

    This generates an optimization plan that uses an index on UserID and says:

    • a1: ‘Using index; Using temporary‘
    • a2: ‘Using where; Distinct‘

    Finally, the following query generates an optimization plan that appears to use indexes most effectively, and no temp tables or filesort.

    SELECT DISTINCT a1.UserID FROM ArrivalTimes a1 WHERE NOT EXISTS (SELECT * FROM ArrivalTimes a2                    WHERE a1.UserID = a2.UserID                      AND a2.ArrivalTime > '09:00:00');  
    • a1: ‘Using where; Using index‘
    • a2: ‘Using where‘

    This appears most likely to have the best performance. Admittedly, I only have four rows in my test table, so this isn’t a representative test.

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

Sidebar

Related Questions

How can I SELECT the last row in a MySQL table? I'm INSERTING data
How can I select a row from mysql query and save it to variable
Is there a mysql syntax that can preform a select values from a table,
How can you write the following in MYSQL? SELECT AVG(col1) FROM table WHERE DISTINCT
I have an application which collects data into a mysql table. The table has
I have a while() function that loops a row of data within my table.
I wonder if is it possible to run mysql command which can select an
How can create a string in JSON after a mysql select: $ritorno = '{Prodotto:'.$riga['Prodotto'].',Prezzo:'.$riga['Prezzo'].'}'
How can i display variable only once in the table. <?php $sql = mysql_query(SELECT
i have one table where one column name is city. for every user i

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.