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:
x
==========================================
| 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
);
Hope this solves your problem. Thanks.