SQL FIRST() and LAST() Functions
SQL FIRST() Function
SQL SELECT FIRST() function returns the first value of selected column.
Syntax:
SELECT FIRST(Column_name) FROM table_name;
OR
SELECT FIRST(Column_name) AS First_Name FROM table_name;
Example : Query using FIRST() Function
Consider the following table titled as
'Stationary', which contains the information of products.
Write a query to display a first name from table 'Stationary'.
ID | Name | Quantity | Price |
---|
1 | Pen | 10 | 200 |
2 | Ink | 15 | 300 |
3 | Notebook | 20 | 400 |
4 | Pencil | 30 | 150 |
SELECT FIRST(Name) AS First_name FROM Stationary;
The result is shown in the following table.
Note: The FIRST() function is only supported in MS Access.SQL LAST() Function
SQL SELECT LAST() function returns the last value of selected column.
Syntax:
SELECT LAST(Column_name) FROM table_name;
OR
SELECT LAST(Column_name) AS Last_Name FROM table_name;
Example : Query using LAST() Function.
Consider the following table titled as
'Stationary', which contains the information of products.
Write a query to display a last name from table 'Stationary'.
ID | Name | Quantity | Price |
---|
1 | Pen | 10 | 200 |
2 | Ink | 15 | 300 |
3 | Notebook | 20 | 400 |
4 | Pencil | 30 | 150 |
SELECT Last(Name) AS Last_name FROM Stationary;
The result is shown in the following table.
Note: The LAST() function is only supported in MS Access.