sqlfilter

Select only one row of table with same value


I'm new to SQL, and for my project I need to do some database sorting and filtering:

Let's assume my database looks like this:

==========================================
|  id       |       email        | name
==========================================
|   1       |  123@test.com      | John
|   2       |  234@test.com      | Peter
|   3       |  234@test.com      | Steward
|   4       |  123@test.com      | Ethan
|   5       |  542@test.com      | Bob
|   6       |  123@test.com      | Patrick
==========================================

What should I do to only have the last column with the same email to be returned:

==========================================
|  id       |       email        | name
==========================================
|   3       |  234@test.com      | Steward
|   5       |  542@test.com      | Bob
|   6       |  123@test.com      | Patrick
==========================================

Solution

  • SQL Query:

        SELECT * FROM test.test1  WHERE id IN (
      SELECT MAX(id) FROM test.test1 GROUP BY email
    );
    

    Your sample dataset Query result

    Hope this solves your problem. Thanks.