Examples of How to Select the Top 10 Rows From Table

For better understanding we will create an table called salaries to perform various queries and operations.

Query:

CREATE TABLE salaries 
(
id INT PRIMARY KEY,
name VARCHAR(50),
salary DECIMAL(10, 2),
hired DATE
);

Output:

salaries table

Example 1: Letā€™s Fetch Only Top 10 Records From the Salaries

Suppose we want to fetch all columns from the ā€œsalariesā€ table and limits the result set to the first 10 rows.

Query:

SELECT * FROM salaries LIMIT 10;

Output:

only 10 records

Explanation: In this query, We have limited the output to the top 10 records only.

Example 2: Letā€™s Fetch TOP 10 Highest(maximum) Salaries

Suppose we want to fetch all columns from the ā€œsalariesā€ table, orders the result set in descending order based on the ā€œsalaryā€ column, and then limits the output to the first 10 rows.

Query:

SELECT * FROM salaries ORDER by salary DESC LIMIT 10;

Output:

max 10 salaries

Explanation: In this query, We are fetching top 10 salary records by sorting(ordering descending) by salary.

Example 3: Letā€™s Fetch TOP 10 Salary Records for Employees Who Hired in the Year 2023

Suppose we want to fetch the ā€œnameā€ and ā€œsalaryā€ columns from the ā€œsalariesā€ table for employees hired in the year 2023 and limits the output to the first 10 rows.

Query:

SELECT name, salary FROM salaries 
WHERE YEAR(hired) = 2023 LIMIT 10;

Output:

year 2023 salary

Explanation: In this query, we are retrieving the name and salary of TOP 10 who hired in 2023.

Example 3: Suppose We to need to Retrieve the Name and Salary of the Top 10 Employees Who were Hired Before ā€˜2023-01-01ā€™ and have a salary More Than the Average Salary.

Suppose we want to fetch the ā€œnameā€ and ā€œsalaryā€ columns from the ā€œsalariesā€ table for employees hired before January 1, 2023, with a salary greater than the average salary of all employees. The result set is ordered by salary in descending order, and the output is limited to the first 10 rows.

Query:

SELECT name, salary
FROM salaries
WHERE hired < '2023-01-01' AND
salary > (SELECT AVG(salary) FROM salaries)
ORDER BY salary DESC
LIMIT 10;

Output:

top 10 have more than average salary

Explanation: In this query we have fetched top 10 records which were hired before ā€˜2023-01-01ā€˜ and having salary more than the average salary.

Example 4: Letā€™s Retrieve the Name, Salary, and the Difference Between the Salary

Suppose we want to fetch the ā€œname,ā€ ā€œsalary,ā€ and calculates the ā€œsalary_differenceā€ by subtracting the average salary of all employees in the ā€œsalariesā€ table. The below query focuses on employees hired in the year 2022, orders the result set by the salary difference in descending order, and limits the output to the first 10 rows.

Query:

SELECT name, salary, 
(salary - AVG(salary) OVER ()) AS salary_difference
FROM salaries
WHERE YEAR(hired) = 2022
ORDER BY salary_difference DESC
LIMIT 10;

Output:

output

Explanation: In the above Query, We are fetching the nameĀ­ and salary of 2022 hired in this query. we are also showing how their salarieĀ­s compare to the averageĀ­. Itā€™s ranked from highest to lowest diffeĀ­rence.we are limiting it to the top 10.

How to Select the Top 10 Rows From a Table in MariaDB

In the data managing systems, MariaDB stands as a robust and adaptableĀ­ MariaDB stands as a robust and adaptableĀ­ choice in data managing systems. Itā€™s known for efficiently retrieving data with lots of tools and instructions. Weā€™reĀ­ looking at the SELECT TOP clause, a key part of fetching data from MariaDB tables. The SELECT TOP in MariaDB limits the number of rows as queĀ­ried results. we can MariaDB stands as a robust and adaptableĀ­ choice in the data managing systems for the top N rows, by a set order criteria.

Similar Reads

How to Select Top Rows

In the MariaDB to select TOP N rows and TOP 10 rows firstly we have to use the LIMIT clause with the SELECT query, and how the select query, Order By Clause works. We will understand through various examples....

Examples of How to Select the Top 10 Rows From Table

For better understanding we will create an table called salaries to perform various queries and operations....

Conclusion

In the MariaDb slecting top N rows from a table is easy using with the LIMIT clause. This clause helps manage in the big dataseĀ­ts. It sets a limit on the number of rows a queĀ­ry returns. This helps us to improve speĀ­ed and the use of reĀ­sources. It could be taking the top N reĀ­cords using some conditions or clauses. The LIMIT clause in MariaDB giveĀ­s flexibility and control in handling data....