Tharindu Madushanka
Tharindu Madushanka

Reputation: 3351

SQLite Select DISTINCT Values of a Column without Ordering

I would like to know whether its possible to get a list of distinct values in a SQLite Column without ordering them.

I tried following query. But it automatically orders the unique text into ascending order.

SELECT DISTINCT column FROM table;

Ex.

Column

Mathew
Mathew
John
John
John
Numbers
Numbers

Result

John
Mathew
Numbers

But I would like it not to be ordered. Would like it in Mathew, John, Numbers

Thanks.

Upvotes: 23

Views: 39813

Answers (3)

Rahul Ravindran
Rahul Ravindran

Reputation: 304

hey one easy method will be adding a serial number(Make it primary Key) as you insert a record and make the order descending .

Upvotes: 0

Etienne Laurin
Etienne Laurin

Reputation: 7184

What order do you want?

If you want to get the values in the order of first appearance you can do:

select distinct column
  from table
 order by min(rowid);

Upvotes: 41

Larry Lustig
Larry Lustig

Reputation: 50970

What do you mean "without ordering"? There's no natural order implied in a SQL table, so the default ascending order by column from left to right is as good as any other order. If you prefer another order, SELECT DISTINCT does accept an ORDER BY clause.

Upvotes: 2

Related Questions