All for Joomla All for Webmasters

SQL SELECT LAST

The last() function is used to return the last value of the specified column.

Syntax for SQL SELECT LAST() FUNCTION:

  1. SELECT LAST (column_name) FROM table_name;

You should note that the last() function is only supported in MS Access. But there are ways to get the last record in MySql, SQL Server, Oracle etc. databases.

My SQL syntax:

  1. SELECT column_name FROM table_name
  2. ORDER BY column_name DESC
  3. LIMIT 1;

SQL Server syntax:

  1. SELECT TOP 1 column_name FROM table_name
  2. ORDER BY column_name DESC;

Oracle syntax:

  1. SELECT column_name FROM table_name
  2. ORDER BY column_name DESC
  3. WHERE ROWNUM <=1;

Let us take the example of CUSTOMERS to examine SQL SELECT LAST command:

Table CUSTOMERS

CUSTOMER_NAME AGE ADDRESS EXPENDITURE
KAMAL SHARMA 26 GHAZIABAD 6000
ROBERT PETT 23 NEWYORK 26000
SHIKHA SRIVASTAV 22 DELHI 9000

If you want to retrieve the last value of the “customer_name” column from the “customers” table, you need to write following query:

  1. SELECT LAST (CUSTOMER_NAME) AS LAST_CUSTOMER FROM CUSTOMERS;
  2. After that query, you will find the result:
  3. SHIKHA SRIVASTAV
PinIt
submit to reddit

Leave a Reply

Top