SQL FIRST()
FIRST() function
FIRST() function returns the value of the first record in the specified column.
SQL FIRST() syntax
SELECT FIRST(column_name) FROM table_name;
##Note: Only MS Access supports FIRST () function.
SQL FIRST() Workspace in SQL Server, MySQL, and OracleSQL Server Syntax
SELECT TOP 1
Examplecolumn_nameFROM table_name
ORDER BY column_name ASC;
ORDER BY column_name ASC;
SELECT TOP 1 name FROM Websites
MySQL SyntaxORDER BY id ASC;
SELECT
Instancecolumn_name FROM table_name
ORDER BY
column_name ASCLIMIT 1;
column_name ASCLIMIT 1;
SELECT name FROM Websites
Oracle SyntaxORDER BY id ASC
LIMIT 1;
LIMIT 1;
SELECT
Examplecolumn_name FROM table_name ORDER BY
column_name ASCWHERE ROWNUM <=1;
column_name ASCWHERE ROWNUM <=1;
SELECT name FROM Websites
SQL FIRST() ExampleThe following SQL statement selects the first record in the "name" column of the "Websites" table Value:
ORDER BY id ASC
WHERE ROWNUM <=1;
WHERE ROWNUM <=1;
Demo Database
In this tutorial, we will use the php sample database.
The following is the data selected from the "Websites" table:
+----+--------------+--- ------------------------+------+---------+
| id | name | url --------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | Taobao | https://www.taobao.com/ | 13 | CN |
| 3 | php Chinese website | http://www.php.cn/ | 4689 | CN |
| 4 | Weibo | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
| 6 | Baidu | https://www.baidu.com/ | 4 | CN |
| 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
+----+---- ----------+--------------------------+-------+-- -------+
| id | name | url --------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | Taobao | https://www.taobao.com/ | 13 | CN |
| 3 | php Chinese website | http://www.php.cn/ | 4689 | CN |
| 4 | Weibo | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
| 6 | Baidu | https://www.baidu.com/ | 4 | CN |
| 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
+----+---- ----------+--------------------------+-------+-- -------+
SQL FIRST() ExampleThe following SQL statement selects the first record in the "name" column of the "Websites" table Value:
Instance
SELECT name AS FirstSite FROM Websites LIMIT 1;The result of executing the above SQL is as follows: