SQL Tutorial
SQL Database
SQL Table
SQL Select
SQL Order By
SQL Insert
SQL Update
SQL Delete
Difference
SQL Injection
SQL String Functions
Miscl
- SQL Formatter
- SQL group by
- SQL add/drop/update column operation
- SQL CAST Function
- SQL Comments
- SQL CONCAT Function
- CTE (Common Table Expression)SQL
- How to use distinct in SQL?
- Joining Three or More Tables in SQL
- What is Web SQL?
- How to create functions in SQL?
- How to run SQL Script?
- How to Delete Duplicate Rows in SQL?
- Nth Highest salary
- 12 Codd's Rules
- SQL EXCEPT
- Types of SQL JOIN
- Change datatype of column in SQL
- SQL Auto Increment
- SQL Like
- Commit and Rollback in SQL
- SQL Concatenate
- SQL get month from the date
- Savepoint in SQL
- SQL ORDER BY DATE
- TIME Datatype in SQL
- SQL BETWEEN
- CRUD Operations in SQL
- SQL INDEX
- Scalar Functions in SQL
- SET Operators in SQL
- Types of SQL Commands
- TCL Commands in SQL
- SQL Subquery
- SQL View
- Constraints in SQL
- Pattern Matching in SQL
- SQL Date Functions
- DDL Commands in SQL
- DML Commands in SQL
- SQL CASE
- SQL Inner Join
- SQL IN Operator
- Check Constraint in SQL
- SQL CLAUSES
- SQL LOGICAL OPERATORS
- Delete Column from Table
- Add Column in the Table
- Delete one row in SQL
- Change the Column Value
- How to Add Foreign Key in SQL
- Add a Primary Key
- Insert One or More rows
- How to Use LIKE in SQL
- Cursor in SQL
- Difference Between DROP and Truncate
- SQL Comparison Operators
- SQL COUNT WHERE
- SQL SELECT MIN
- SQL Stored Procedure
- SQL SELECT AVG
- SQL SELECT MAX
- SQL ADD COLUMN
- How to use Auto-Increment in SQL
- SQL Languages
- SQL Arithmetic Operators
- How to Use GROUP BY in SQL
- How to Use ORDER BY in SQL
- Trigger in SQL
- What is Race Condition
- SQL COUNT DISTINCT
PL/SQL Tutorial
Sql Interview Question
SQl Quiz
SQL SELECT LAST
The LAST() function in Structured Query Language shows the last value from the specified column of the table.
Note: This SQL function is only supported in Microsoft Access database. Oracle supports ORDER BY and ROWNUM keywords, and MySQL supports the LIMIT keyword for selecting the last record.
Syntax of LAST() Function
In the above syntax, the LAST keyword denotes the last row to be shown from the table in the output, and the Field_Name denotes the column whose value we want to show.
Example of the LAST function in SQL
Example 1:
Firstly, we have to create a table and insert the data into the table in SQL.
The following SQL statement creates the Student_Details table with Student_ID as the primary key:
(
Student_ID INT NOT NULL,
Student_Name varchar(100),
Student_Course varchar(50),
Student_Age INT,
Student_Marks INT
);
The following SQL queries insert the record of students into the above table using INSERT INTO statement:
INSERT INTO Student_Details VALUES (102, Raman, MCA, 24, 98);
INSERT INTO Student_Details VALUES (104, Shyam, BBA, 19, 92);
INSERT INTO Student_Details VALUES (107, Vikash, B.tech, 20, 78);
INSERT INTO Student_Details VALUES (111, Monu, MBA, 21, 65);
INSERT INTO Student_Details VALUES (114, Jones, B.tech, 18, 93);
INSERT INTO Student_Details VALUES (121, Parul, BCA, 20, 97);
INSERT INTO Student_Details VALUES (123, Divya, B.tech, 21, 89);
INSERT INTO Student_Details VALUES (128, Hemant, MBA, 23, 90);
INSERT INTO Student_Details VALUES (130, Nidhi, BBA, 20, 88);
INSERT INTO Student_Details VALUES (132, Priya, MBA, 22, 99);
INSERT INTO Student_Details VALUES (138, Mohit, MCA, 21, 92);
Let's see the record of the above table using the following SELECT statement:
Student_ID | Student_Name | Student_Course | Student_Age | Student_Marks |
---|---|---|---|---|
101 | Anuj | B.tech | 20 | 88 |
102 | Raman | MCA | 24 | 98 |
104 | Shyam | BBA | 19 | 92 |
107 | Vikash | B.tech | 20 | 78 |
111 | Monu | MBA | 21 | 65 |
114 | Jones | B.tech | 18 | 93 |
121 | Parul | BCA | 20 | 97 |
123 | Divya | B.tech | 21 | 89 |
128 | Hemant | MBA | 23 | 90 |
130 | Nidhi | BBA | 20 | 88 |
132 | Priya | MBA | 22 | 99 |
138 | Mohit | MCA | 21 | 92 |
The following query shows the last Student_Name from the above table in the output:
Output:
Syntax of LIMIT Clause in MySQL
In this MySQL syntax, we have to specify the value 1 just after the LIMIT keyword for indicating the single row/record.
Example of LIMIT Clause in MySQL
Let's take the following Employee table to explain how to use the LIMIT clause in MySQL for accessing the last record:
Employee_Id | Emp_Name | Emp_City | Emp_Salary | Emp_Bonus |
---|---|---|---|---|
101 | Anuj | Ghaziabad | 35000 | 2000 |
102 | Tushar | Lucknow | 29000 | 3000 |
103 | Vivek | Kolkata | 35000 | 2500 |
104 | Shivam | Goa | 22000 | 3000 |
The following MySQL query shows the last value of the Emp_City column from the above Employee table:
Output:
Goa
ROWNUM keyword in Oracle
The syntax for accessing the last record from the Oracle database is given below:
In this Oracle syntax, we have to specify the ROWNUM keyword, which is less than and equal to 1. In Oracle, the ROWNUM keyword is used in the WHERE clause for retrieving the last record from the table.
Example of ROWNUM Clause in Oracle
Let's take the following Cars table to explain how to use the ROWNUM keyword in MySQL:
Car_Number | Car_Name | Car_Amount | Car_Price |
---|---|---|---|
2578 | Creta | 3 | 900000 |
9258 | Audi | 2 | 1100000 |
8233 | Venue | 6 | 900000 |
6214 | Nexon | 7 | 1000000 |
The following MySQL query shows the last name of the car from the Car_Name column of the Cars table:
Output:
Nexon