Use COUNT(), ORDER BY and WHERE user = ? simultaneously [duplicate] - mysql

This question already has answers here:
Rank function in MySQL
(13 answers)
Closed 7 years ago.
I have a table with highscores. When I read them I order them by score DESC.
scores
id name score
i.e.
SELECT name, score FROM scores ORDER BY score DESC
Now I would like to know the rank of a person. I am trying to find a way to combine this without having to loop through all the highscores. This is what I thought of, but I know this will not work. Any ideas?
SELECT COUNT(id), name, score FROM scores WHERE name = ? ORDER BY score DESC
Should I use WHERE?

You could count everyone with a higher score in a subquery:
select coalesce((select count(1) from scores b where b.score > a.score),0) + 1 Rank
, Name
, Score
from Scores a
where name = 'Sarah'
SQL Fiddle: http://sqlfiddle.com/#!9/ff0133/3

Related

Get the most often occurring year [duplicate]

This question already has answers here:
Find most frequent value in SQL column
(11 answers)
Closed 4 years ago.
I have the following table from a factory database called world in MySQL Workbench:
Table: country
Relevant Columns:
Code char(3) PK,
IndepYear smallint(6)
I'd like to get the year (or possibly years) when the most countries have become independent. I don't want to list anything else just the year(s), one row per year.
EDIT:
I have found the right solution that actually works.
SELECT IndepYear
FROM (SELECT IndepYear,
COUNT(IndepYear) AS Years
FROM world.country
GROUP BY IndepYear
ORDER BY Years DESC) AS T1
WHERE Years IN (SELECT Years
FROM (SELECT IndepYear,
COUNT(IndepYear) AS Years
FROM world.country
GROUP BY IndepYear
ORDER BY Years DESC
LIMIT 1) AS T2
GROUP BY IndepYear)
ORDER BY IndepYear ASC;
It sounds like you just want to ORDER BY the COUNT of IndepYear. Don't forget that you'll also want to GROUP BY this column, along with adding a LIMIT of 1 to get the most frequently occuring year when sorting it in descending order.
SELECT `IndepYear`,
COUNT(`IndepYear`) AS `CommonYear`
FROM `country`
GROUP BY `IndepYear`
ORDER BY `CommonYear` DESC
LIMIT 1;
Use aggregation to get the count of each year and limit it to the maximum count in a HAVING clause. Use a subquery to get the maximum count, aggregating the aggregation.
SELECT indepyear
FROM country
GROUP BY indepyear
HAVING count(*) = (SELECT max(c)
FROM (SELECT count(*) c
FROM country
GROUP BY indepyear) x);
SQL Fiddle

Count value in SQL [duplicate]

This question already has answers here:
How to count occurrences of a column value efficiently in SQL?
(7 answers)
Closed 5 years ago.
I have got a table with ID's:
Id
===
1
2
2
3
1
2
And I want to create a table with two columns like this:
Id COUNT
=============
1 2
2 3
3 1
How can I do that?
Let's say you called your table 'user', you can try this :
SELECT user.Id as ID, count(user.Id) as COUNT_ID
FROM user
GROUP BY ID;
Hope it helps,
WaLinke
You have to group by your id.
Select id, count(1) as COUNT
from yourtable
group by ID
order by id
That way you tell your sql, that you want to count the number of rows per id.
If you need more examples feel free to google sql count.
There are many good examples out there.
Or check this stackoverflow question:
How to use count and group by at the same select statement
You can use GROUP BY and Count(columnname) functions like this
SELECT
Id,
Count(Id) AS COUNT
FROM
tablename
GROUP BY Id
Something like this should work
SELECT id, COUNT(id) AS Expr1 FROM dbo.Table1 GROUP BY id

Top 5 records from each week [duplicate]

This question already has answers here:
Using LIMIT within GROUP BY to get N results per group?
(14 answers)
Closed 8 years ago.
I have a table where I record multiple scores from users daily. I'm trying to create a query where I get the distinct top 5 weekly winners for each week that passes...
Is it that I need to do a sub query grouping results in both max score and date week? or do I need to do 2 sub queries one for the date another for max score then use the outer query to group?
Well the table structure would be:
NAME,
SCORE,
DATE
I came up with this
SELECT *
FROM `highscores`
WHERE id IN ((SELECT id
FROM highscores
WHERE WEEK(date) IN (SELECT DISTINCT WEEK(date)
FROM highscores)
ORDER BY score DESC))
GROUP BY email
ORDER BY date, score DESC
But apparently I can't use LIMIT in sub-queries
I think this should work for you. It should also bring back ties, in the event there are any (let's say the 5th highest score for one week were a tie between 2 people, this would bring them both back for that week, and so you'd have 6 rows for that week).
select *
from highscores x
where x.score >=
(select max(e.score)
from highscores e
where week(e.date) = week(x.date)
and e.score <
(select max(d.score)
from highscores d
where week(d.date) = week(x.date)
and d.score <
(select max(c.score)
from highscores c
where week(c.date) = week(x.date)
and c.score <
(select max(b.score)
from highscores b
where week(b.date) = week(x.date)
and b.score <
(select max(a.score)
from highscores a
where week(a.date) = week(x.date))))))
order by date, score desc

how to select from this table and add average value and then sort [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to select, average and sort in mysql table
i have a table in mySql like in this picture
and i want to write a query which result will group by LESSON column, and add new row which is average value of LESSON column and sum CNT column values....
i use this query but it gives result like in picture 3 and i cant sort by PERC in this case
select no, STUD_ID,CLASS,LESSON, AVG(PERC) as PERC,SUM(CNT) as CNT from t_lesson where LESSON='CHEM' group by CLASS
union all
select no,STUD_ID,CLASS,'AVERAGE' as LESSON, AVG(PERC) as PERC, SUM(CNT) as CNT from t_lesson where LESSON='CHEM' group by LESSON
select * from <your query> order by PERC
wich :
select * from (
select no, STUD_ID,CLASS,LESSON, AVG(PERC) as PERC,SUM(CNT) as CNT from t_lesson where LESSON='CHEM' group by CLASS
union all
select no,STUD_ID,CLASS,'AVERAGE' as LESSON, AVG(PERC) as PERC, SUM(CNT) as CNT from t_lesson where LESSON='CHEM' group by LESSON
) as sub order by PERC DESC

SQL Select most common values [duplicate]

This question already has answers here:
Get most common value for each value of another column in SQL
(9 answers)
Closed 8 years ago.
I'm pretty new to SQL (I'm using MySQL) and need some help. I'm currently trying to select the most common age(s) from a table called PERSON. Suppose PERSON has an AGE column which has values: 10, 10, 20, 20, 30. The query should return the values 10 and 20.
The following query only retrieves the top row (20):
SELECT AGE FROM PERSON GROUP BY AGE ORDER BY COUNT(*) DESC LIMIT 1;
My other thought was to try something like:
SELECT AGE FROM PERSON GROUP BY AGE HAVING COUNT(AGE) = MAX(COUNT(AGE));
This returns an error, stating that it is invalid use of group function.
Any help would be greatly appreciated. Thanks!
This will do:
select age from persons
group by age
having count(*) = (
select count(*) from persons
group by age
order by count(*) desc
limit 1)
SELECT *, COUNT(AGE) as age_count
FROM PERSON
GROUP BY AGE
ORDER BY age_count DESC
LIMIT 1
Can't test it here but it should work.
WITH x AS (
SELECT age, COUNT(*) numOfAge
FROM person
GROUP BY age
)
SELECT age
FROM x
WHERE numOfAge = ( SELECT MAX(numOfAge) FROM x)
ORDER BY age