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

6957 questions
1664
votes
7 answers

Multiple "order by" in LINQ

I have two tables, movies and categories, and I 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
  • 18,966
  • 8
  • 36
  • 55
739
votes
5 answers

SQL multiple column ordering

How can I sort by multiple columns in SQL and in different directions. column1 would be sorted descending, and column2 ascending.
Señor Reginold Francis
  • 14,328
  • 15
  • 54
  • 72
467
votes
4 answers

LINQ Orderby Descending Query

I'm sure this will be a relatively simple one. I have a LINQ query that I want to order by the most recently created date. See: var itemList = from t in ctn.Items where !t.Items && t.DeliverySelection …
109221793
  • 15,397
  • 37
  • 100
  • 157
316
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
  • 14,847
  • 15
  • 63
  • 97
301
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,056
  • 2
  • 24
  • 25
272
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
  • 60,472
  • 154
  • 401
  • 592
260
votes
9 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
  • 6,682
  • 7
  • 48
  • 67
256
votes
5 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
249
votes
12 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
  • 14,512
  • 16
  • 95
  • 175
240
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
  • 4,046
  • 5
  • 18
  • 21
222
votes
11 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
  • 2,931
  • 4
  • 17
  • 10
196
votes
6 answers

how to customize `show processlist` in mysql?

I want to order by Time,but seems no way to do that ? mysql> show…
omg
  • 123,990
  • 135
  • 275
  • 341
193
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
  • 1,933
  • 2
  • 12
  • 6
189
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,409
  • 2
  • 15
  • 16
187
votes
2 answers

MySQL: Sort GROUP_CONCAT values

In short: Is there any way to sort the values in a GROUP_CONCAT statement? Query: GROUP_CONCAT((SELECT GROUP_CONCAT(parent.name SEPARATOR " » ") FROM test_competence AS node, test_competence AS parent WHERE node.lft BETWEEN parent.lft AND…
Ivar
  • 4,096
  • 6
  • 35
  • 50
1
2 3
99 100