Javatpoint Logo
Javatpoint Logo

SQL Query to Display Last 5 Records "employee" Table

Introduction:

Structured Query Language is one of the main tools for manipulating and managing relational databases in database administration. Typical tasks include collections of data from tables, usually to track the newest entries, changes, and any new trends. In this tutorial, we put emphasis on query to display only the last five records in an employee database.

SQL Query to Display Last 5 Records

Understanding the Scenario:

Let's say that the structure of your database is like this example "employee" table. Each line in this table illustrates a single employee and the characteristics like name, ID, department, and hire date (attributes). For listing the last five employees on this table that was created, we will demonstrate querying capability of SQL.

Constructing the SQL Query:

Let's break down the SQL query step by step:

  • Select Statement: We shall apply the SELECT statement to select the columns that we feel relevant to be displayed.
  • From Clause: This sentence indicates the table that we are using to obtain data for our analysis. For us -- this sentence concerns our case, so the subject of the sentence is to use "our" not "we" as the reference point that is the "employee" table.
  • Order by Clause: Our records will be sorted according to hire date in descending order, which means that the most recent hires will be top of the list.
  • Limit Clause: Lastly, we will make use of the LIMIT clause to confine the result set to the last top five entries.

Putting it all together, here's the SQL query:

SQL Code:

Explanation of the Query:

  • SELECT *: The WHERE clause specifically chooses all the columns for the employee table.
  • FROM employee: Shows the table that contains the data, in this example, the one with employee's data.
  • ORDER BY hire_date DESC: In that query, the record sort is descending and then based on hire date, meaning the most recent hires will be first.
  • LIMIT 5: Confining the output to only five records filters out the latest five from the employees table.

Example Implementation:

Let's consider a sample "employee" table with columns:

ID, name, department, and hire_date.

SQL CODE:

Output:

SQL Query to Display Last 5 Records

Executing our SQL query on this table will result in:

SQL Code:

Output:

SQL Query to Display Last 5 Records

Conclusion:

In this tutorial, we have picked to check how a query on the employee's table selects the last five records. SELECT, FROM, ORDER BY, and LIMIT are the crucial elements for an efficient DB query, which allows us to extract the data we need. Comprehending SQL queries, together with their proper implementation, gets database administrators and developers ready to administer the database properly and transform the data into practical outputs.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news