I have a MySQL table called names
with a column names name
.
This column has the following values:
Beacher
Ackley
Addison
Caldwell
Cameron
Alcott
Carling
Beardsley
Beldon
I want to get, alphabetically sorted, the value of every first letter.
Alphabetically sorted the list above is like this:
Ackley
Addison
Alcott
Beacher
Beardsley
Beldon
Caldwell
Cameron
Carling
The first value starting with A
is Ackley
, the first value starting with B
is Beacher
…
So I want the following output:
Ackley
Beacher
Caldwell
Is this even possible with a single SQL query? How?
3
Answers
You can do it using
row_number
:You can test on this db<>fiddle
Steps:
column
= your actual column nameFirst we are creating a
CTE
that does two things. It creates a rank using the windows functionrow_number()
. This rank is build using the following logic: Pick the column’s first letter i.e the use ofLEFT()
function(1 specifies the placement i.e the first letter in our case). This will eventually tell SQL to pick every word’s first letter. The second functionality is theORDER BY
within the window function.The
ORDER BY
orders all the words based on the A-Z i.e Ascending order by default. If you useDESC
it will return the last word for the same starting character.Connecting both logics, what we get is the rank based on the ascending order of each word and the first character. [All the first words starting with each alphabet in a given data set]
In the final select we just filter the
rank_ = 1
in the where clauseIf you want the last word you can just use
order by column DESC
IF windows function is not supported you can use the following solution, the logic remains almost the same:
The answers that use the
row_number()
window function require that you have MySQL 8.0.Here’s a solution that should work on MySQL 5.x.