supabase-rb-rb
Database

Order the results

Order rows by one or more columns. Chains; multiple calls accumulate.

Add an ORDER BY clause. Lives on the select (and rpc) builder via SelectMixin. Each call appends to the existing order — call it once per column for multi-key sorts.

Signature

builder.order(column, desc: false, nullsfirst: nil, foreign_table: nil)

Parameters

NameTypeRequiredDescription
columnString / SymbolRequiredColumn name to order by. Passed verbatim to the PostgREST order= query string.
descBooleanOptionalWhen true, sort descending. Default false (ascending).
nullsfirstBoolean, nilOptionaltrue → nullsfirst, false → nullslast, nil (default) → omit the directive and let Postgres decide.
foreign_tableStringOptionalOrder an embedded resource. Rewrites the query key to <foreign_table>.order so PostgREST applies the order to the joined relation, not the parent.

Returns

Returns
self (SelectRequestBuilder)

The same builder, so you can keep chaining other modifiers / filters before .execute.

Example — ascending by name

response = supabase
  .from("countries")
  .select("id, name")
  .order("name")
  .execute

Example — descending with nulls last

response = supabase
  .from("users")
  .select("id, last_login")
  .order("last_login", desc: true, nullsfirst: false)
  .execute

Example — multi-column ordering

Call order once per column. The builder concatenates them with a comma, exactly like PostgREST expects.

supabase
  .from("orders")
  .select("id, customer_id, total")
  .order("customer_id")
  .order("total", desc: true)
  .execute

Example — order an embedded resource

foreign_table: targets the joined relation. Use it with a select that embeds the table.

supabase
  .from("authors")
  .select("name, books(title, published_at)")
  .order("published_at", desc: true, foreign_table: "books")
  .execute

On this page