Search code examples
mysqlsqlsubquerygreatest-n-per-groupminimum

Finding the rows with same values having same data in sql using java?


From the table as I want to get the rows contains minimum value of works as here is 3 so that I can get the rows of id 2 and 5 as both the rows has same minimum value.

ID   |   emailID                |   works
------------------------------------------
1    |   [email protected]      |   5
2    |   [email protected]       |   3
3    |   [email protected]     |   5
4    |   [email protected]  |   4
5    |   [email protected]       |   3

Solution

  • "I want to get the rows that contain the minimum value of works" - You can get that by doing the following:

    SELECT emailId 
    FROM yourTable
    WHERE works = (SELECT MIN(works) 
                   FROM yourTable) 
    

    Here is a demonstration of this query in action: SQL Fiddle

    If this is not what you are looking for, please elaborate on your question.