How to sort query results in oracle
In Oracle, you can use the "SELECT" statement and the "ORDER BY" clause to sort the query. You can sort the query result set of one or more columns in ascending or descending order. The syntax "SELECT field name" FROM data table name ORDER BY field name [ASC|DESC]".
The operating environment of this tutorial: Windows 7 system, Oracle 11g version, Dell G3 computer.
In Oracle, you can use the "SELECT" statement and the "ORDER BY" clause to sort queries.
In Oracle, row data records are stored in the table in a non-specified order, regardless of the order in which the rows are inserted into the database. To query row records by column in ascending or descending order, you must explicitly instruct the Oracle database how to sort.
For example, you might want to list all customers alphabetically by name, or display all customers in order from lowest to highest credit limit.
To sort the queried data, add the ORDER BY
clause to the SELECT statement as follows.
ORDER BY
clause can sort the result set of one or more columns in ascending or descending order.
Syntax:
SELECT 字段名, FROM table_name ORDER BY 字段名 [ASC | DESC] [NULLS FIRST | NULLS LAST]
To sort the result set by a column, you can list the column after the ORDER BY clause.
It is a sorting order according to the column name, which can be:
ASC
means sorting in ascending orderDESC
means sorting in descending order
By default, the ORDER BY
clause is used whether or not ASC
is specified. Both sort the rows in ascending order. If you want to sort the rows in descending order, use DESC
explicitly.
NULLS FIRST
Place NULL
values before non-NULL
values, NULLS LAST
before non-## Place the NULL
value after the #NULL value.
ORDER BY clause can sort data by multiple columns, and each column may have a different sort order.
ORDER BY clause is always the last clause in a
SELECT statement.
customers table in the sample database for demonstration.
customers) table:
name,
address and
credit limit:
SELECT name, address, credit_limit FROM customers;
1. Example of sorting rows by column
#To sort customer names alphabetically in ascending order, use the following statement:SELECT name,address,credit_limit FROM customers ORDER BY name ASC;
ASC instructs Oracle to sort the rows in ascending order. But
ASC is optional, and if omitted, by default, the
ORDER BY clause sorts the rows by the specified column in ascending order.
ORDER BY name ASC
ORDER BY name
Explicitly use DESC
after the column name in the clause, as shown below: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>SELECT name, address, credit_limit
FROM customers
ORDER BY name DESC;</pre><div class="contentsignin">Copy after login</div></div>
Execute the above query statement and get the following results-
2. Example of sorting rows by multiple columns To sort multiple columns, you can separate them with commas
ORDER BY each column in the sentence. See the following Contacts (
) table in the sample database. For example, to sort by
in ascending order and the last_name
column in descending order, use the following statement: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>SELECT first_name, last_name
FROM contacts
ORDER BY first_name, last_name DESC;</pre><div class="contentsignin">Copy after login</div></div>
Execute From the above sample code, you can see the following results -
##As you can see from the screenshot above, contact_id
91 and
311The values of
first_name of these two records are the same, and the values of
last_name are arranged in descending order.
In this example, Oracle first sorts the rows by
first_name
last_name in descending order.
See results similar to the following:
In the above result:
- 首先,按
first_name
升序排序。 - 其次,如果两个名字相同,按
last_name
降序排列,如Daniel Glass
和Daniel Costner
,Dianne Sen
和Dianne Derek
,Doretha Tyler
和Dorotha Wong
。
3、按列位置排序行示例
不需要指定用于排序数据的列名称。如果您愿意,可以使用ORDER BY
子句中列的位置。
请参考下语句 -
SELECT name, credit_limit,address FROM customers ORDER BY 2 DESC, 1;
在这个例子中,name
列的位置是1
,credit_limit
列的位置是2
。相当于以下查询语句 -
SELECT name, credit_limit,address FROM customers ORDER BY credit_limit DESC, name;
在ORDER BY
子句中,使用这些列位置来指示Oracle对行进行排序。
4、用NULL值排序行的示例
请参阅示例数据库中的以下地区(locations
)表:
以下语句按城市(city
)和州(state
)检索位置并对其进行排序:
SELECT country_id, city, state FROM locations ORDER BY state DESC;
执行上面查询语句,得到以下结果 -
state
列有NULL
值,这意味着state
数据与某些城市(例如:Beijing
, Hiroshima
和 London
)无关。
当使用非NULL
值对混合NULL
进行排序时,Oracle允许指定哪个应该首先出现。
例如,以下语句按升序对state
列进行排序,并首先将NULL
值放置在前面。
SELECT country_id, city, state FROM locations ORDER BY state ASC NULLS FIRST;
要放置NULL
值在后面,可以使用NULLS LAST
,如下面的语句所示:
SELECT country_id, city, state FROM locations ORDER BY state ASC NULLS LAST;
执行上面示例查询语句,得到以下结果:
5、按函数或表达式排序数据
ORDER BY
子句可在一列上应用一个函数,例如字符串函数,数学函数等,并按函数的结果对数据进行排序。
例如,以下语句使用ORDER BY
子句中的UPPER()
函数来区分客户名称的大小写:
SELECT customer_id, name FROM customers ORDER BY UPPER( name );
执行上面查询语句,得到以下结果 -
推荐教程:《Oracle教程》
The above is the detailed content of How to sort query results in oracle. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

Oracle database paging uses ROWNUM pseudo-columns or FETCH statements to implement: ROWNUM pseudo-columns are used to filter results by row numbers and are suitable for complex queries. The FETCH statement is used to get the specified number of first rows and is suitable for simple queries.

To stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node

SQL statements can be created and executed based on runtime input by using Oracle's dynamic SQL. The steps include: preparing an empty string variable to store dynamically generated SQL statements. Use the EXECUTE IMMEDIATE or PREPARE statement to compile and execute dynamic SQL statements. Use bind variable to pass user input or other dynamic values to dynamic SQL. Use EXECUTE IMMEDIATE or EXECUTE to execute dynamic SQL statements.

When Oracle log files are full, the following solutions can be adopted: 1) Clean old log files; 2) Increase the log file size; 3) Increase the log file group; 4) Set up automatic log management; 5) Reinitialize the database. Before implementing any solution, it is recommended to back up the database to prevent data loss.
