0

i am new in learning sql. how to create query to get the timestamp of a minimum value and the minimum value itself? previously i managed to get the minimum value but not with its timestamp. with this query

SELECT min(score) as lowest 
FROM rank 
WHERE time >= CAST(CURDATE() AS DATE)

here is the table that i've created:

https://i.ibb.co/9sMV8b4/aa.png

(cannot attach image because of the reputation rule)

sorry for the bad english.

2
  • 1
    Note that rank is now a reserved word, making it a poor choice as a table/column identifier, and please don't attach pictures where raw text would do just as well.
    – Strawberry
    Commented Feb 4, 2019 at 9:27
  • thank you for the advice, i'll replace the reserved word.
    – rosy ciks
    Commented Feb 4, 2019 at 10:25

2 Answers 2

1

If you either expect that there would be only a single record with the lowest score, or if there be ties, you don't care which record gets returned, then using LIMIT might be the easiest way to go here:

SELECT timestamp, score
FROM rank
WHERE time >= CAST(CURDATE() AS DATE)
ORDER BY score
LIMIT 1;

If you care about ties, and want to see all of them, then we can use a subquery:

SELECT timestamp, score
FROM rank
WHERE time >= CAST(CURDATE() AS DATE) AND
      score = (SELECT MIN(score) FROM rank WHERE time >= CAST(CURDATE() AS DATE));
1
  • it works! Thank you Tim, i really appreciate your answer
    – rosy ciks
    Commented Feb 4, 2019 at 10:26
1

It's possible by following way.

Note: It only works if you want to get a single record at once

select score, time
FROM rank 
WHERE time >= CAST(CURDATE() AS DATE)
ORDER BY score ASC LIMIT 1
4
  • 2
    have you executed your query? I don't understand how this answer can get 3 votes? and your answer is just copied answer of @Tim Biegeleisen
    – Fahmi
    Commented Feb 4, 2019 at 9:41
  • Yes, I've executed my query. Sorry, but it's not copied from other. Commented Feb 4, 2019 at 10:02
  • ha ha ha bro, nobody will believe you as @Tim's answer was posted 9 mins before your answer - and you know what your answer also give wrong output
    – Fahmi
    Commented Feb 4, 2019 at 10:05
  • the differences between Tim and Pinal answer is tim select the lowest score and pinal select the highest score. I appreciate both of your answer. Thank you
    – rosy ciks
    Commented Feb 4, 2019 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.