Two methods to obtain the first piece of data after sorting in Oracle: use the ROWNUM pseudo column to limit the query to return data with the current row number 1. Use the FETCH FIRST 1 ROWS ONLY clause to limit the query to return only the first 1 row in the result set.
Two methods to obtain the first piece of data after sorting in Oracle
In Oracle, you can use two There are two main ways to get the first piece of data in the sorted data set:
1. Use the ROWNUM pseudo column
ROWNUM
pseudo column to return the current The row number in the query result set. The following query uses the ROWNUM
pseudo column to obtain the first data of the sorted data set:
<code class="sql">SELECT * FROM ( SELECT * FROM table_name ORDER BY column_name ) WHERE ROWNUM = 1;</code>
2. Use the FETCH FIRST 1 ROWS ONLY clause
FETCH FIRST 1 ROWS ONLY
clause limits the query to return only the first 1 row in the result set. The following query uses the FETCH FIRST 1 ROWS ONLY
clause to get the first piece of data in the sorted dataset:
<code class="sql">SELECT * FROM table_name ORDER BY column_name FETCH FIRST 1 ROWS ONLY;</code>
Example:
Suppose we There is a table named "my_table" that contains the following data:
id | name |
---|---|
1 | John |
2 | Mary |
3 | Bob |
The following query will use the ROWNUM
pseudo column to get the first data sorted by the "name" column in ascending order:
<code class="sql">SELECT * FROM ( SELECT * FROM my_table ORDER BY name ) WHERE ROWNUM = 1;</code>
Result:
id | name |
---|---|
1 | John |
The following query will use the FETCH FIRST 1 ROWS ONLY
clause to get the first data sorted by the "name" column in ascending order:
<code class="sql">SELECT * FROM my_table ORDER BY name FETCH FIRST 1 ROWS ONLY;</code>
Result:
id | name |
---|---|
1 | John |
The above is the detailed content of How to get the first piece of data after sorting in Oracle. For more information, please follow other related articles on the PHP Chinese website!