Questions tagged [sql-order-by]

An ORDER BY clause in SQL specifies that a SQL SELECT statement returns a result set with the rows being sorted by the values of one or more columns.

An ORDER BY clause in specifies that a SQL SELECT statement returns a result set with the rows being sorted by the values of one or more columns.

Tag Usage

Do not use this tag for every question that involves a SQL query that contains an ORDER BY clause. Use this tag for questions about how to use, write, debug, or understand an ORDER BY clause or for questions about ordering the results of a SQL query.

Reference

7618 questions
1796
votes
7 answers

Multiple "order by" in LINQ

I have two tables, movies and categories, and I want to get an ordered list by categoryID first and then by Name. The movie table has three columns ID, Name and CategoryID. The category table has two columns ID and Name. I tried something like the…
Sasha
  • 20,424
  • 9
  • 40
  • 57
895
votes
9 answers

SQL multiple column ordering

How can I sort multiple columns in SQL and in different directions? For instance, 'column1' would be sorted descendingly and 'column2' ascendingly.
Señor Reginold Francis
  • 16,318
  • 16
  • 57
  • 73
507
votes
4 answers

LINQ Orderby Descending Query

I have a LINQ query that I want to order by the most recently created date. I tried: var itemList = from t in ctn.Items where !t.Items && t.DeliverySelection orderby t.Delivery.SubmissionDate descending …
109221793
  • 16,477
  • 38
  • 108
  • 160
371
votes
14 answers

SQL how to make null values come last when sorting ascending

I have a SQL table with a datetime field. The field in question can be null. I have a query and I want the results sorted ascendingly by the datetime field, however I want rows where the datetime field is null at the end of the list, not at the…
David Božjak
  • 16,887
  • 18
  • 67
  • 98
335
votes
14 answers

MySQL Orderby a number, Nulls last

Currently I am doing a very basic OrderBy in my statement. SELECT * FROM tablename WHERE visible=1 ORDER BY position ASC, id DESC The problem with this is that NULL entries for 'position' are treated as 0. Therefore all entries with position as…
JonB
  • 4,422
  • 2
  • 27
  • 25
329
votes
7 answers

PostgreSQL DISTINCT ON with different ORDER BY

I want to run this query: SELECT DISTINCT ON (address_id) purchases.address_id, purchases.* FROM purchases WHERE purchases.product_id = 1 ORDER BY purchases.purchased_at DESC But I get this error: PG::Error: ERROR: SELECT DISTINCT ON expressions…
sl_bug
  • 5,066
  • 5
  • 21
  • 22
299
votes
4 answers

Order a MySQL table by two columns

How do I sort a MySQL table by two columns? What I want are articles sorted by highest ratings first, then most recent date. As an example, this would be a sample output (left # is the rating, then the article title, then the article…
magenta
295
votes
13 answers

MySQL order by before group by

There are plenty of similar questions to be found on here but I don't think that any answer the question adequately. I'll continue from the current most popular question and use their example if that's alright. The task in this instance is to get…
Rob Forrest
  • 7,329
  • 7
  • 52
  • 69
289
votes
8 answers

How does MySQL process ORDER BY and LIMIT in a query?

I have a query that looks like this: SELECT article FROM table1 ORDER BY publish_date LIMIT 20 How does ORDER BY work? Will it order all records, then get the first 20, or will it get 20 records and order them by the publish_date field? If it's the…
Alex
  • 66,732
  • 177
  • 439
  • 641
289
votes
13 answers

Laravel Eloquent: Ordering results of all()

I'm stuck on a simple task. I just need to order results coming from this call $results = Project::all(); Where Project is a model. I've tried this $results = Project::all()->orderBy("name"); But it didn't work. Which is the better way to obtain…
MatterGoal
  • 16,038
  • 19
  • 109
  • 186
282
votes
12 answers

How to order by with union in SQL?

Is it possible to order when the data is come from many select and union it together? Such as Select id,name,age From Student Where age < 15 Union Select id,name,age From Student Where Name like "%a%" How can I order this query by name? I tried…
Guilgamos
  • 3,621
  • 4
  • 20
  • 11
239
votes
17 answers

ORDER BY the IN value list

I have a simple SQL query in PostgreSQL 8.3 that grabs a bunch of comments. I provide a sorted list of values to the IN construct in the WHERE clause: SELECT * FROM comments WHERE (comments.id IN (1,3,2,4)); This returns comments in an arbitrary…
nutcracker
  • 2,944
  • 2
  • 17
  • 16
228
votes
8 answers

how to customize `show processlist` in mysql?

I want to order by Time,but seems no way to do that ? mysql> show…
omg
  • 136,412
  • 142
  • 288
  • 348
227
votes
5 answers

GROUP_CONCAT ORDER BY

I've a table like: +-----------+-------+------------+ | client_id | views | percentage | +-----------+-------+------------+ | 1 | 6 | 20 | | 1 | 4 | 55 | | 1 | 9 | 56 | | 1 | 2…
ronquiq
  • 2,579
  • 3
  • 23
  • 25
225
votes
6 answers

Why does MYSQL higher LIMIT offset slow the query down?

Scenario in short: A table with more than 16 million records [2GB in size]. The higher LIMIT offset with SELECT, the slower the query becomes, when using ORDER BY *primary_key* So SELECT * FROM large ORDER BY `id` LIMIT 0, 30 takes far less…
Rahman
  • 2,253
  • 3
  • 14
  • 6
1
2 3
99 100