SQL Getting Sta...login
SQL Getting Started Tutorial Manual
author:php.cn  update time:2022-04-12 14:15:40

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 Oracle

SQL Server Syntax

SELECT TOP 1
column_nameFROM table_name
ORDER BY
column_name ASC;
Example

               SELECT TOP 1 name FROM Websites
ORDER BY id ASC;
MySQL Syntax

            SELECT
column_name FROM table_name ORDER BY
column_name ASCLIMIT 1;
Instance

          SELECT name FROM Websites
ORDER BY id ASC
LIMIT 1;
Oracle Syntax

SELECT
column_name FROM table_name ORDER BY
column_name ASCWHERE ROWNUM <=1;
Example

            SELECT name FROM Websites
ORDER BY id ASC
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 |
+----+---- ----------+--------------------------+-------+-- -------+

SQL FIRST() Example

The 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: