SQL Select Last
SELECT LAST statement used to fetch last value from specified column in existing table. SELECT LAST Statement works only in MS Access database only.
Syntax -
SELECT LAST (Column_name) FROM table_name;
- column_name - Specifies the name of the column on which LAST clause applied.
Syntax for creating user defined name to the required column –
SELECT LAST (column_name) AS user_defined_name FROM table_name;
- user_defined_name- Specifies the name of the output column name in result-set.
Example
Let us consider below table(s) as an example table(s) to frame the SQL query for getting the desired results.
employee_details -
emp_id | emp_name | designation | manager_id | date_of_hire | salary | dept_id |
---|---|---|---|---|---|---|
001 | Employee1 | Director | 2019-07-11 | 45000.00 | 1000 | |
002 | Employee2 | Director | 2019-07-11 | 40000.00 | 2000 | |
003 | Employee3 | Manager | Employee1 | 2019-07-11 | 27000.00 | 1000 |
004 | Employee4 | Manager | Employee2 | 2019-10-08 | 25000.00 | 2000 |
005 | Employee5 | Analyst | Employee3 | 2019-07-11 | 20000.00 | 1000 |
006 | Employee6 | Analyst | Employee3 | 2019-10-08 | 18000.00 | 1000 |
007 | Employee7 | Clerk | Employee3 | 2019-07-11 | 15000.00 | 1000 |
008 | Employee8 | Salesman | Employee4 | 2019-09-09 | 14000.00 | 2000 |
009 | Employee9 | Salesman | Employee4 | 2019-10-08 | 13000.00 | 2000 |
Scenario – Fetch the last value of the required column from table.
Requirement – Fetch last row value of emp_name column in employee_details table.
The query was as follows –
SELECT LAST (emp_name) FROM employee_details;
By executing above query, we can get output like as shown in below –
emp_name |
---|
Employee9 |
Scenario – Fetch last value of the required column from table and name the result column with user defined column name.
Requirement – Fetch last row value salary column from employee_details table and name the column as last_salary. The query was as follows –
SELECT LAST (salary) AS last_salary FROM employee_details;
By executing above query,we can get output like as shown in below -
last_salary |
---|
13000.00 |