Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

MySQL Subquery Exercises: Query to select last 10 records from a table

MySQL Subquery: Exercise-18 with Solution

Write a query to select last 10 records from a table.

Sample table: employees


Code:

SELECT * FROM (
SELECT * FROM employees ORDER BY employee_id DESC LIMIT 10) sub 
ORDER BY employee_id ASC;

Explanation:

MySQL Subquery Syntax :

MySQL subquery syntax

- The subquery (inner query) executes once before the main query (outer query) executes.
- The main query (outer query) use the subquery result.

MySQL SubQueries: Find the names, salary of the employees who earn the same salary as the minimum salary for all departments.

 

MySQL Code Editor:

Structure of 'hr' database:

hr database

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous:Write a query to find the 4th minimum salary in the employees table.
Next:Write a query to list the department ID and name of all the departments where no employee is working.

What is the difficulty level of this exercise?