I'm trying to select the user who has the MAX microposts count:
SELECT "name", count(*) FROM "users"
INNER JOIN "microposts" ON "microposts"."user_id" = "users"."id"
GROUP BY users.id
and this returns
"Delphia Gleichner";15
"Louvenia Bednar IV";10
"Example User";53
"Guadalupe Volkman";20
"Isabella Harvey";30
"Madeline Franecki II";40
But I want to select only "Example User";53
, (user who has MAX microposts count)
I tried to add HAVING MAX(count*)
but this didn't work.
And the short answer to the above question is, no. You can't. It is not possible to nest aggregate functions.
To find the maximum value of a column, use the MAX() aggregate function; it takes a column name or an expression to find the maximum value. In our example, the subquery returns the highest number in the column grade (subquery: SELECT MAX(grade) FROM student ).
To get the maximum value from three different columns, use the GREATEST() function. Insert some records in the table using insert command. Display all records from the table using select statement.
Count Max (Italian:Il conte Max) is a 1957 Italian-Spanish comedy film directed by Giorgio Bianchi and starring Alberto Sordi, Vittorio De Sica and Anne Vernon. It is a remake of the 1937 film Il signor Max in which De Sica had played the title role. This film was itself remade in 1991.
I'd try with a ORDER BY max DESC LIMIT 1, where maximum is the count(*) field. Something like:
SELECT "name", count(*) maximum FROM "users"
INNER JOIN "microposts" ON "microposts"."user_id" = "users"."id"
GROUP BY users.id
ORDER BY maximum DESC
LIMIT 1
I dont' have mysql available now, so I'm doing this on the paper (and it might not work), but it's just an orientation.
SELECT x.name, MAX(x.count)
FROM (
SELECT "name", count(*)
FROM "users" INNER JOIN "microposts" ON "microposts"."user_id" = "users"."id"
GROUP BY users.id
) x
GROUP BY x.name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With