sql_func_first
SQL: Retrieving the First Row or Top N Rows
Section titled “SQL: Retrieving the First Row or Top N Rows”Getting the “First” Row: Importance of Ordering
Section titled “Getting the “First” Row: Importance of Ordering”In SQL, tables are inherently unordered sets of rows. To reliably retrieve what you consider the “first” row, you must use an ORDER BY clause to define a specific sequence. Without ORDER BY, the database might return rows in any order, which can change between queries.
Once ordered, you can use various SQL clauses to limit the result set to the first row (or the top N rows).
Note: The non-standard FIRST() function found in MS Access is not part of standard SQL. Below are common, standard, or widely supported methods.
Standard and Common Syntaxes for Retrieving the First Row
Section titled “Standard and Common Syntaxes for Retrieving the First Row”Standard SQL (SQL:2008 and later): FETCH FIRST N ROWS ONLY
Section titled “Standard SQL (SQL:2008 and later): FETCH FIRST N ROWS ONLY”This is the ANSI SQL standard way. Supported by PostgreSQL, Oracle (12c+), DB2, SQL Server (2012+ via OFFSET ... FETCH), and others.
SELECT column_name(s)FROM table_nameORDER BY sort_column [ASC|DESC]FETCH FIRST 1 ROW ONLY;Example: Get the oldest customer.
Section titled “Example: Get the oldest customer.”SELECT CustomerName, RegistrationDateFROM CustomersORDER BY RegistrationDate ASCFETCH FIRST 1 ROW ONLY;MySQL & PostgreSQL: LIMIT Clause
Section titled “MySQL & PostgreSQL: LIMIT Clause”SELECT column_name(s)FROM table_nameORDER BY sort_column [ASC|DESC]LIMIT 1;Example: Get the customer with the alphabetically first name.
Section titled “Example: Get the customer with the alphabetically first name.”SELECT CustomerNameFROM CustomersORDER BY CustomerName ASCLIMIT 1;SQL Server: TOP Clause
Section titled “SQL Server: TOP Clause”SELECT TOP 1 column_name(s)FROM table_nameORDER BY sort_column [ASC|DESC];Example: Get the top-priced product.
Section titled “Example: Get the top-priced product.”SELECT TOP 1 ProductName, PriceFROM ProductsORDER BY Price DESC;Oracle (Older versions, pre-12c): ROWNUM Pseudocolumn
Section titled “Oracle (Older versions, pre-12c): ROWNUM Pseudocolumn”This method requires a subquery because ROWNUM is assigned before ORDER BY in the same query block.
SELECT column_name(s)FROM ( SELECT column_name(s) FROM table_name ORDER BY sort_column [ASC|DESC])WHERE ROWNUM <= 1;Example: Get the first customer by CustomerID using ROWNUM.
Section titled “Example: Get the first customer by CustomerID using ROWNUM.”SELECT CustomerNameFROM ( SELECT CustomerName, CustomerID FROM Customers ORDER BY CustomerID ASC)WHERE ROWNUM <= 1;Demonstration Database: Northwind
Section titled “Demonstration Database: Northwind”We’ll use the Northwind sample database for context.
Below is a selection from the “Customers” table:
CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.05021Mexico
3Antonio Moreno TaqueríaAntonio MorenoMataderos 2312México D.F.05023Mexico
4Around the HornThomas Hardy120 Hanover Sq.LondonWA1 1DPUK
5Berglunds snabbköpChristina BerglundBerguvsvägen 8LuleåS-958 22SwedenExample: Finding the First Customer Alphabetically
Section titled “Example: Finding the First Customer Alphabetically”The following SQL statement selects the first customer alphabetically by “CustomerName” from the “Customers” table, using the LIMIT clause (common in MySQL/PostgreSQL).
SELECT CustomerName AS FirstAlphabeticalCustomerFROM CustomersORDER BY CustomerName ASCLIMIT 1;Practical Application: Retrieving the most recent entry, the highest scoring player, or the top-selling product are common use cases that rely on ordering and limiting results.