find top entries in MYSQL database - mysql

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;

Related

How to select 1 random row from the latest 30 rows in MySql?

I want to select one row from the latest 30 rows based on the date.
And with the below it is selecting the random row from all the rows and thats not what I want.
SELECT * FROM product where userId=1 ORDER BY RAND() LIMIT 1
So how can I select from the latest 30? Thanks.
A sub-select will return the last 30, and then you can choose a random row from there
SELECT pr.* FROM
( SELECT * from product
WHERE pr.userId=1
ORDER BY created_at DESC
LIMIT 30 ) AS pr
ORDER BY RAND()
LIMIT 1

How can I select the row from a table whose 1 field is maximum or minimum?

In my table I have a field name rt_stars which includes integer anyone between 1 to 5. Suppose there are 5 rows having rt_stars 4,4,3,2,1 respectively. Here, 4 is the highest and 1 is the lowest. If I want to select the row with the maximum value 4 how can I do that? Since there are two 4 here the last one will be selected. Can I have query something like this?
SELECT * FROM ratings WHERE MAX(rt_stars) ORDER BY rt_id DESC LIMIT 1
I know this is wrong but that's how I want to select all the values from the rows if the rt_stars field has the maximum value in it. How can I achieve this kind of a query?
You can select one row using:
select r.*
from ratings r
order by rt_stars desc, rt_id desc
limit 1;
The problem with your query is that you cannot use max() in the where clause. Beyond that, you don't need aggregation at all -- just ordering the rows and then selecting the first one.
If you want all rows with the maximum stars you can do:
select *
from ratings
where rt_stars = (select max(rt_stars) from ratings)
If you just want one of them randomly you can do:
select *
from ratings
order by rt_stars desc
limit 1
I think the below SQL code will help.
SELECT * FROM ratings WHERE rt_stars = MAX(rt_stars) ORDER BY rt_id DESC;
Sorry the code modified below
SELECT * FROM ratings WHERE rt_stars = ( SELECT MAX( user_05_pk ) FROM ratings )
ORDER BY rt_id DESC;

MySQL select last 'n' records by date, but sorted oldest to newest

I have a table that has transactions with a datetime column. I'm trying to select the last 'n' records (i.e. 20 rows) but have it sorted oldest to newest.
SELECT *
FROM table
WHERE 1=1
ORDER BY table.datefield DESC
LIMIT 20;
Gives me the 20 most recent, but in the opposite order.
Is this possible in one query, or will I have to do a query to get total rows and then adjust the limit based on that so I can do the table.datefiled ASC and then limit (total rows - n), n
Building a SELECT around your original SELECT and convert this to a derived table should do it
SELECT t.*
FROM (
SELECT *
FROM table
WHERE 1=1
ORDER BY table.datefield DESC
LIMIT 20
) t
ORDER BY t.datefield

How do I select the 1st row from a selection of 10 random rows

Lets say I have a 1000 rows in my table.
I want to select 10 of those at random.
SELECT * FROM table ORDER BY RAND() LIMIT 10
Then I want to select the row in that result with the highest value for number
SELECT * FROM table ORDER BY number DESC LIMIT 1
Can anyone help me come up with an efficient way of doing this?
Just use a subquery:
SELECT *
FROM (
SELECT * FROM table ORDER BY RAND() LIMIT 10
)
ORDER BY number DESC LIMIT 1

Fetching top 5 records of maximum salary

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