order()

Order the query result by column.

You can call this method multiple times to order by multiple columns.

You can order foreign tables, but it doesn't affect the ordering of the current table.

1const { data, error } = await supabase
2  .from('cities')
3  .select('name', 'country_id')
4  .order('id', { ascending: false })

Parameters#

  • columnrequired
    ColumnName

    The column to order by

  • optionsoptional
    object

    Named parameters

      Properties
    • ascendingoptional
      boolean

      If true, the result will be in ascending order

    • foreignTableoptional
      undefined

      Set this to order a foreign table by foreign columns

    • nullsFirstoptional
      boolean

      If true, nulls appear first. If false, nulls appear last.

Examples#

With select()#

1create table
2  countries (id int8 primary key, name text);
3
4insert into
5  countries (id, name)
6values
7  (1, 'Afghanistan'),
8  (2, 'Albania'),
9  (3, 'Algeria');

On a foreign table#

Ordering on foreign tables doesn't affect the ordering of the parent table.

1create table
2  countries (id int8 primary key, name text);
3create table
4  cities (
5    id int8 primary key,
6    country_id int8 not null references countries,
7    name text
8  );
9
10insert into
11  countries (id, name)
12values
13  (1, 'United States'),
14  (2, 'Vanuatu');
15insert into
16  cities (id, country_id, name)
17values
18  (1, 1, 'Atlanta'),
19  (2, 1, 'New York City');