sql-order-by

Default row ordering for select query in oracle

◇◆丶佛笑我妖孽 提交于 2019-11-26 01:36:21
In Oracle, what is the the default ordering of rows for a select query if no "order by" clause is specified. Is it the order in which the rows were inserted there is no default ordering at all none of the above. According to Tom Kyte: "Unless and until you add "order by" to a query, you cannot say ANYTHING about the order of the rows returned. Well, short of 'you cannot rely on the order of the rows being returned'." See this question at asktom.com. As for ROWNUM, it doesn't physically exist, so it can't be "freed". ROWNUM is assigned after a record is retrieved from a table, which is why

Ordering by specific field value first

ぐ巨炮叔叔 提交于 2019-11-26 01:07:10
问题 I have a table with 3 columns: id | name | priority -------------------- 1 | core | 10 2 | core | 9 3 | other | 8 4 | board | 7 5 | board | 6 6 | core | 4 I want to order the result set using priority but first those rows that have name=core even if have lower priority. The result should look like this id | name | priority -------------------- 6 | core | 4 2 | core | 9 1 | core | 10 5 | board | 6 4 | board | 7 3 | other | 8 回答1: There's also the MySQL FIELD function. If you want complete

PostgreSQL DISTINCT ON with different ORDER BY

被刻印的时光 ゝ 提交于 2019-11-26 01:06:50
问题 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 must match initial ORDER BY expressions Adding address_id as first ORDER BY expression silences the error, but I really don\'t want to add sorting over address_id . Is it possible to do without ordering by address_id ? 回答1: Documentation says:

SQL best practice to deal with default sort order

醉酒当歌 提交于 2019-11-26 01:00:25
问题 A lot of SQL code I\'ve read, it seems like the developer assumes that the default sort order always holds. For example when building an HTML select list they would just SELECT id, name FROM table without issuing an ORDER BY clause. From my own experience it seems like dbms alway orders data using FIFO if no ORDER BY clause is given and no index. However, the order is not guaranteed. But I have never seen a dbms reordering data if there no change to the table. Have you ever experienced a dbms

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

巧了我就是萌 提交于 2019-11-26 00:46:27
问题 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 last one, you\'re not guaranteed to really get the most recent 20 articles. 回答1: It will order first, then get the first 20. A database will also process anything in the WHERE clause before ORDER BY . 回答2: The LIMIT clause can be used to constrain

Why does MYSQL higher LIMIT offset slow the query down?

元气小坏坏 提交于 2019-11-26 00:09:32
问题 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 than SELECT * FROM large ORDER BY `id` LIMIT 10000, 30 That only orders 30 records and same eitherway. So it\'s not the overhead from ORDER BY. Now when fetching the latest 30 rows it takes around 180 seconds. How can I optimize that simple query? 回答1: It

MySQL Orderby a number, Nulls last

浪子不回头ぞ 提交于 2019-11-25 23:45:39
问题 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 NULL appear before those with 1,2,3,4. eg: NULL, NULL, NULL, 1, 2, 3, 4 Is there a way to achieve the following ordering: 1, 2, 3, 4, NULL, NULL, NULL. 回答1: MySQL has an undocumented syntax to sort nulls last. Place a minus sign (-) before the column

MySQL 'Order By' - sorting alphanumeric correctly

有些话、适合烂在心里 提交于 2019-11-25 23:44:25
问题 I want to sort the following data items in the order they are presented below (numbers 1-12): 1 2 3 4 5 6 7 8 9 10 11 12 However, my query - using order by xxxxx asc sorts by the first digit above all else: 1 10 11 12 2 3 4 5 6 7 8 9 Any tricks to make it sort more properly? Further, in the interest of full disclosure, this could be a mix of letters and numbers (although right now it is not), e.g.: A1 534G G46A 100B 100A 100JE etc.... Thanks! update: people asking for query select * from

ORDER BY the IN value list

柔情痞子 提交于 2019-11-25 23:43:42
问题 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 order which in my happens to be ids like 1,2,3,4 . I want the resulting rows sorted like the list in the IN construct: (1,3,2,4) . How to achieve that? 回答1: You can do it quite easily with (introduced in PostgreSQL 8.2) VALUES (), (). Syntax will be

MySQL order by before group by

纵然是瞬间 提交于 2019-11-25 23:11:40
问题 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 the latest post for each author in the database. The example query produces unusable results as its not always the latest post that is returned. SELECT wp_posts.* FROM wp_posts WHERE wp_posts.post_status=\'publish\' AND wp_posts.post_type=\'post\'