Skip to content
Advertisement

SQL: Select only one row of table with same value

Im a bit 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 te be returned:

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

Thanks in advance!

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement