Fetching top 5 records of maximum salary - mysql

I have 3 columns with id,usrnameand salary. I want to find maximum salary of 5 records.
How will i write query in mysql ?

In MySQL you can use ORDER BY to sort the rows in descending order and use LIMIT to return only the top 5 rows:
SELECT id, usrname, salary
FROM yourtable
ORDER BY salary DESC
LIMIT 5

You have to use LIMIT, like so:
SELECT * FROM mytable ORDER BY salary DESC LIMIT 5

Related

How to use multiple order by in a sql query with limit keyword?

I want to get nth highest salary in a table by first limiting result set to n top salaries first and then to reverse the result set and limiting it to 1.
I have used this query which is resulting into an error:-
select *
from salaries
where emp_no=(select * from salaries order by salary desc limit 2) order by salary asc limit 1;
The error states that subquery return more than one row.
If you want second highest value you could use:
SELECT *
FROM salaries
ORDER BY salary DESC
LIMIT 1,1;
db<>fiddle demo
Here's one way:
SELECT s.* FROM
(SELECT * FROM `salaries` ORDER BY salary DESC LIMIT 2) s
ORDER BY s.salary ASC LIMIT 1;
Never use SELECT *, see http://www.parseerror.com/blog/select-*-is-evil
select * from salaries
order by salary
desc limit 1 offset 4;
https://www.db-fiddle.com/f/2itHSFs2enyNpJ3MK6Nxcz/0

find top entries in MYSQL database

In my database each row has a column for average rating.
Now lets say I have thousands of rows with a variety of averages such as 4.45 4.78 3.21 2.13 4.91
How would I get the rows with the top 3 highest average?
You can order rows in descending with order by average_rating desc and limit output to the top 3 results:
select average_rating
from tbl
order by average_rating desc
limit 3
Syntax for TOP Clause:
SELECT column_name(s) FROM table_name LIMIT number;
SELECT *
FROM Table order by column Desc
LIMIT 5;
or u can use
SELECT TOP 2 * FROM Table;
but it will give you top 2 based on your primary key
Order the avg by DESC order to find largest avergaes and then LIMIT 5 to find the top 5 avg
SELECT avg FROM table_name ORDER BY avg DESC LIMIT 5;

Fetching the last 3 rows in mysql in ascending order

I have a table named comments.I want to retrieve the last 3 rows in the ascending order of time.The following query fetches it in the descending order of time.What should I do to get the desired result?
select * from comments where id=1 order by time desc limit 0,3
Use a subquery:
select c.*
from (select c.*
from comments c
where id = 1
order by time desc
limit 0, 3
) c
order by time asc;

How can I print the first 2 maximum values in a database column using SQL?

How can I print the first two maximum values under an attribute (column) in a database table using SQL?
I have a column named salary that contains different rows, (approximately 10 rows). We need to print out the first two maximum values.
I know we can get the first maximum value by using max function, but what can I do if I need the first two values?
Perhaps something like this? (mysql)
select `salary` from `mytable` order by `salary` desc limit 2
As per Alex's answer, you can add the distinct keyword to ensure that you get two different values.
select distinct `salary` from `mytable` order by `salary` desc limit 2
In SQL Server
SELECT TOP 2 salary
from table
order by salary desc
In MySQL
SELECT salary
from table
order by salary desc
limit 2
select distinct salary from mytable order by salary desc limit 2;
Ok If you need only the two top, even if the are equal, the others have rigth, but if you need the top 2 number (and you don't care if there are multiple instance from them) you can use this:
SELECT
Salary
FROM Salaries
GROUP BY Salary
ORDER BY Salary DESC
LIMIT 2

How to select maximum two values using LIMIT in MySQL?

I have the database in MySQL like this
id employee_id salary
1 1 10000
2 2 20000
3 3 10000
4 4 40000
5 5 30000
I want to select the maximum two salary by using LIMIT.So how to select that?
Just sort your results in descending order of salary and limit the resultset as desired:
SELECT * FROM mytable ORDER BY salary DESC LIMIT 2
See it on sqlfiddle.
SELECT
employee_id,
salary
From
employee
order by salary desc
limit 2 ;