the table videos has the folowing feels
id,average,name
how can i write the query, to select the name of video, which have the max average!!!
i can do that vith two queries, by selecting the max(avege) from the table, and then find out the name, where ihe average equal to max!!! but i want to do that in one query!!!
help me please!!!
You don't need a group by for this, you just want to select the highest average!
SELECT * FROM videos ORDER BY average DESC LIMIT 1;
You can use an ORDER BY with a LIMIT:
SELECT id, average, name FROM videos ORDER BY average DESC LIMIT 1
ORDER BY average DESC orders the rows in order of descending average (i.e. the first row will have an average equal to MAX(average)). LIMIT 1 causes only the first row to be returned.
SELECT id,name,MAX(average) FROM videos;
All fields you choose to SELECT will be returned. Getting more data back is just a case of SELECTing more fields.
Related
I have the following table:
I am trying to find the "source" with the highest income. In this case the "Ads".
Should I make combined query to find the SUM of each "source", then a second query to find the MAX value among them?
No. Just use order by with limit:
select source, sum(amount)
from t
group by source
order by sum(amount) desc
limit 1;
I have a SQL query in mySQL that generates the difference between two columns that's limited to 100 entries and sorted by another column. It gives me a list of 100 values, and I want to be able to find the average of the generated answers from the query; I know how to find the AVG of all the differences, but I don't need that value.
My Query:
SELECT ABS(VAL1-VAL2)
FROM USER.TABLE1
ORDER BY JOB DESC
LIMIT 100;
What I want to get is how to find the average of this query's results
You would do it as follows:
SELECT AVG(t.result)
FROM (SELECT ABS(VAL1-VAL2) AS result
FROM USER.TABLE1
ORDER BY JOB DESC
LIMIT 100) t;
I'm not expert in MYSQL,
I have a list of videos, (ID,ANIME_ID,TIME)
I want to select top 25 rows ordered by TIME without repeating same ANIME_ID
I mean only top TIMEs of each ANIME_ID and ordered by TIME not by ANIME_ID
Since you're using mysql, you can just use group by for this -- however, realize this will just return an arbitrary id if ties exist:
select id, anime_id, max(time) time
from videos
group by anime_id
order by max(time) desc
limit 25
Condensed Fiddle Demo
If you need a specific id for ties, you can establish a row number using user-defined variables and filter accordingly.
You should use:
Select distinct(anime_id) order by time limit 25
I have table (name "friend") in mysql and i want to select 2nd or 3rd or 4th highest amount (i.e salary) from that table.
I am using this method:
`select * from friends order by salary desc limit 1; -- for highest salary.`
and
`select * from friends order by salary desc limit 1 offset 1; -- for second highest salary.`
and
`select * from friends order by salary desc limit 1 offset 2; -- for third highest salary.`
is this method correct or do I have to use another logical method like.
`select * from friends where salary = (select max(salary) from friends where salary < (select max(salary) from friends)); -- for second highest salary`.
please tell me which is the professional method.
The limit with offset is preferred and more legibale;
select *
from friends
order by salary desc
limit 3,1;
The difference between the two approaches is time complexity
In a general sense sorting (or doing an order by) only to look at highest or second highest or such is an overkill in terms of time. Think of if you want to find the largest element in a list, would you look to sort the entire list and look at the first element after sorting it, or loop through the list to find the largest one.
There is no right way, it depends on if you can afford taking slightly more time in arriving at the solution, in favour of more readable query, this would in turn be dependent on size of data etc.
I'm creating an advertisement platform for my website, where we need select 3 rows randomly and number of times of banner display also should be balanced.
We can get the random rows like this,
SELECT column FROM table ORDER BY RAND() LIMIT 0,3
and we can balance the number of times by incrementing count field while selecting the row each time and select the rows with less count like this,
SELECT * FROM table ORDER BY display_count LIMIT 0,3.
But it will return the values like 1,2,3,4,5,6 and so. But i need to select rows with minimum count randomly. Any suggestion or idea on this would be great?
is this what you are looking for:
SELECT *
FROM table
ORDER BY display_count ASC, RAND()
LIMIT 0,3;
Did you tried
SELECT column FROM table ORDER BY RAND(), display_count LIMIT 0,3
?
I think this is what you want.