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

  • Home
  • SEARCH
  • 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 6601209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:43:24+00:00 2026-05-25T18:43:24+00:00

compare SELECT distinct u_id,timestamp as time FROM my_table; and SELECT distinct u_id,max(timestamp) as time

  • 0

compare

SELECT distinct u_id,timestamp as time
FROM my_table;

and

SELECT distinct u_id,max(timestamp) as time
FROM my_table;

When my table has no rows at all (or if I add a where clause that matches no rows):

The former returns an empty results set (which is what I expect)
while the later returns a single row that has null as the value for both its fields.

Can someone please explain to me why does the second one acts as it does?

  • 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-25T18:43:24+00:00Added an answer on May 25, 2026 at 6:43 pm

    MySQL documentation says

    MAX() returns NULL if there were no matching rows.

    And if you have no data then it just returns both values as NULL.

    If you want the second query return the empty resultset too, then you must filter out the NULL values for example with HAVING clause that you can use with aggregate functions:

    SELECT DISTINCT u_id, MAX(timestamp) as time FROM my_table GROUP BY u_id HAVING time IS NOT NULL;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to table. Both table has nric field. I want to select the
Is there a better way to write Select distinct id_no from revenue_table where (exists
I'm trying to compare two columns from different columns. Ex: Select a.Col1, b.Col1, CASE
So I have this query: SELECT DISTINCT(b.friend_id) AS possible_id FROM friend_friends a JOIN friends_friends
Compare the following stored procedures: CREATE PROCEDURE testProc1 AS SELECT * INTO #temp FROM
This query does the job to compare 2 table structures on mysql: select column_name
I have this to select a date from a mysql db, and compare it
select count(distinct id) from svn where name='ant' and type='Bug' and select count(distinct id) from
When you right click on a file and select Compare..., you get a few
I'm looking for the answer on how DISTINCT clause works in SQL (SQL Server

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.