How to select records without duplicate on just one field in SQL? How to select records without duplicate on just one field in SQL? sql sql

How to select records without duplicate on just one field in SQL?


Try this:

SELECT MIN(id) AS id, titleFROM tbl_countriesGROUP BY title


DISTINCT is the keyword
For me your query is correct

Just try to do this first

SELECT DISTINCT title,id FROM tbl_countries

Later on you can try with order by.


For using DISTINCT keyword, you can use it like this:

SELECT DISTINCT     (SELECT min(ti.Country_id)      FROM tbl_countries ti      WHERE t.country_title = ti.country_title) As Country_id    , country_titleFROM     tbl_countries t

For using ROW_NUMBER(), you can use it like this:

SELECT     Country_id, country_title FROM (    SELECT *, ROW_NUMBER() OVER (PARTITION BY country_title ORDER BY Country_id) As rn    FROM tbl_countries) tWHERE rn = 1

Also with using LEFT JOIN, you can use this:

SELECT t1.Country_id, t1.country_titleFROM tbl_countries t1    LEFT OUTER JOIN    tbl_countries t2 ON t1.country_title = t2.country_title AND t1.Country_id > t2.Country_idWHERE    t2.country_title IS NULL

And with using of EXISTS, you can try:

SELECT t1.Country_id, t1.country_titleFROM tbl_countries t1   WHERE    NOT EXISTS (SELECT 1                 FROM tbl_countries t2                 WHERE t1.country_title = t2.country_title AND t1.Country_id > t2.Country_id)