Skip to content
Advertisement

How I fetch values from one column based on distinct values from other column?

I have two columns in a table and I want a query to fetch First column values based on distinct values from Second column. Since, There are multiple combinations of First and second column I would like to have the first match from each combination.

DATASET:

First Column Second Column
A abc
B abc
C abc
D abc
F abc
G abc
H qwe
I qwe
J jkl
K jkl
L jkl
M uio
N uio

Excepted Output:

First Column Second Column
A abc
H qwe
J jkl
M uio

Advertisement

Answer

Just use MIN() and GROUP BY.

SELECT min(`First Column`) AS `First Column`, `Second Column`
FROM yourTable
GROUP BY `Second Column`

DEMO

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