Question: My MySQL table country\_phone\_codes looks something like this

```
id     country_code     area_code     name
------------------------------------------------------------
1   |   93           |  93         |  AFGHANISTAN
2   |   93           |  9370       |  AFGHANISTAN - MOBILE
3   |   93           |  9375       |  AFGHANISTAN - MOBILE
4   |   355          |  355        |  ALBANIA
5   |   355          |  35568      |  ALBANIA - MOBILE - AMC
6   |   213          |  213        |  ALGERIA
7   |   213          |  2131       |  ALGERIA - CAT
------------------------------------------------------------

```

These are just few records of more than 28000 records. I am trying to formulate a query that will provide me with the result like this-

```
country_code    name
-----------------------------
93            |  AFGHANISTAN
355           |  ALBANIA
213           |  ALGERIA
-----------------------------

```

By using `SELECT DISTINCT(country_code) FROM country_phone_codes ORDER BY country_code LIMIT 0,260`, I am able to get distinct country codes. But how do I get corresponding country name?

Answer: 
The answer is trivial, using `GROUP BY`:

```
SELECT country_code,MIN(name)
FROM country_phone_codes
GROUP BY country_code;

```

The `DISTINCT` function and `ORDER BY` aren’t necessary with `GROUP BY`. As the original question specifically pertained to MySQL, the `MIN()` aggregate function isn’t necessary and you might see better performance without it if all of the following are true:

* The server is MySQL
* The storage engine is InnoDB
* The first column of the example data is the primary key and the entries follow the same ordering suggested by the small sample, namely, the country name appears before all other names in the group.

This works because the InnoDB storage engine will scan in the order of the primary key and, for nonaggregated columns, it will use the first value it finds.
You need to group your country\_code also then & then only you will get correct matching results.

```
SELECT country_code,name
FROM country_phone_codes 
group by country_code
ORDER BY country_code

```

And we should need to avoid use of `Distinct` in select clause as it gives performance issues.
Instead of that we can use `group by`