I need SQL query for MySQL to select top 10 people with most followers
my table
id | user_id | follow_id
1 3 6
2 3 7
3 4 6
4 5 6
5 7 3
6 9 7
From example user with id 6 have 3 time followed , 7->2 and 3->1, so TOP 10 will be
user with id 6,7,3 ...
SELECT `follow_id`, COUNT(1) AS `followers`
FROM `tbl`
GROUP BY `follow_id`
ORDER BY COUNT(1) DESC
LIMIT 10;
You want to use MySQL GROUP BY aggregation funciton
SELECT user_id, COUNT(follow_id) AS total_followers
FROM users
GROUP BY follow_id
ORDER BY total_followers LIMIT 10;
SELECT follow_id,count(id) AS cnt FROM table
GROUP BY follow_id ORDER BY cnt DESC LIMIT 10
You can use
SELECT user_id , COUNT(id) AS count FROM tbl GROUP BY follow_id ORDER BY count DESC LIMIT 10;
You need to group the results by the follow_id and then count how many results are in this group and sort this by the number of results per group in a descending order and then define you want to limit it to only 10 results which can be done by using LIMIT 0,10
The following query works perfectly in MySQL 5
SELECT follow_id, COUNT(follow_id) AS nr
FROM test.testtable
GROUP BY follow_id
ORDER BY nr DESC
LIMIT 0,10
Try some thing like this:
select follow_id
from myTable
group by follow_id
order by count(user_id)
Limit 10
SELECT follow_id,
COUNT(user_id) AS number_of_followers
FROM table
GROUP BY follow_id
ORDER BY number_of_followers DESC
LIMIT 10;
Related
I have a table with non-unique column auth_id. I need to select the auth_id value with maximum number of entries.
SELECT auth_id, cnt
FROM (SELECT auth_id, COUNT(auth_id) AS cnt
FROM articles_authors
GROUP BY auth_id) articles_num
WHERE cnt = (SELECT MAX(articles_num.cnt))
Here's the data example:
auth_id article_id
1 2
1 1
1 3
2 2
3 1
3 2
And the output:
auth_id cnt
1 3
But SQL doesn't see the alias table articles_num.
How do I make this WHERE clause with this alias?
Using a limit clause would be much simpler - you simply order a query according to some field, and then just take the first row:
SELECT auth_id, COUNT(auth_id) AS cnt
FROM articles_authors
GROUP BY auth_id
ORDER BY 2 DESC
LIMIT 1
Order your data in descending order in your inner query then just take the first one:
SELECT auth_id, cnt
FROM (
SELECT auth_id, COUNT(auth_id) AS cnt
FROM articles_authors
GROUP BY auth_id
ORDER BY cnt DESC
)
LIMIT 1
If I understand correctly, you actually want to get one row of the max of the count:
SELECT auth_id, count(auth_id) as cnt
FROM articles_authors
GROUP BY 1
ORDER BY 2 DESC
LIMIT 1
If more than one auth_id have same max count, we need to update this SQL
Try this.......
select auth_id, count(auth_id) as cnt
from articles_authors
group by auth_id
order by cnt DESC
limit 1;
Let me know if it resolves your issue
I want to select 5 random rows from a table but only from the 20 most recent rows. I know the 2 statements separately would be something like:
SELECT * FROM table ORDER BY RAND() LIMIT 5
SELECT * FROM table ORDER BY date DESC LIMIT 20
How would I combine these 2 statements so it will select 5 random rows from the 20 most recent rows? Thanks.
Use an nested select
SELECT foo.* FROM (SELECT * FROM table ORDER BY date DESC LIMIT 20 ) as foo
ORDER BY RAND() LIMIT 5
Simply nest them:
SELECT * FROM (
SELECT * FROM table ORDER BY date DESC LIMIT 20
) ORDER BY RAND() LIMIT 5
Look up subqueries!
SELECT d.* FROM (SELECT * FROM table ORDER BY date DESC LIMIT 20) as d ORDER BY RAND() LIMIT 5;
If you have a column called userID, what query would you use to find the userID with the greatest number of rows in that table?
Thank you
You can use COUNT, ORDER BY that count DESC and LIMIT the result to the top one:
SELECT user_id, COUNT(*)
FROM tableName
GROUP BY user_id
ORDER BY 2 DESC
LIMIT 1;
select user_id, sum(1) as counter
from TABLE
group by user_id
order by 2 desc
limit 1
This should be enough to get only one user, even if more than one user share the maximum amount of rows:
SELECT user_id FROM table
GROUP BY user_id
ORDER BY COUNT(*) DESC
LIMIT 1
If you need to return all matching users:
SELECT user_id FROM table
GROUP BY user_id
HAVING COUNT(*) = (
SELECT COUNT(*) FROM table
GROUP BY user_id
ORDER BY COUNT(*) DESC
LIMIT 1
)
I have a table like the following
item_id position_number position_date
1 9 2013-06-29 15:12:58
2 7 2013-07-25 15:12:58
18 5 2013-07-08 12:07:00
13 9 2013-07-08 12:07:00
I want to get the items group by position_number and order by position_date DESC, so the query will return the following:
item_id position_number position_date
13 9 2013-07-08 12:07:00
2 7 2013-07-25 15:12:58
18 5 2013-07-08 12:07:00
I've been implementing some of the solutions that use DISTINCT and GROUP BY, but not get the desired result.
Does anyone have an idea about how to solved it?
SELECT a.*
FROM tableName a
INNER JOIN
(
SELECT position_number, MAX(position_date) position_date
FROM tableName
GROUP BY position_number
) b ON a.position_number = b.position_number AND
a.position_date = b.position_date
ORDER BY a.position_number DESC
SQLFiddle Demo
Given your example data, this query will return the specified resultset:
SELECT t.item_id
, t.position_number
, t.position_date
FROM ( SELECT MAX(n.item_id) AS max_item_id
FROM mytable n
GROUP BY position_number
) m
JOIN mytable t
ON t.item_id = m.max_item_id
ORDER BY t.position_number DESC
NOTE This is choosing a single item_id for each position_number. This is assuming that a given item_id will appear only once, and have a single position_number. (If an item_id can be associated with multiple postion_number, the query can be tweaked. This is using the MAX() function to choose the item_id with the largest value. (The only example of a row being excluded is item_id=1.)
try this
select * from your_table group by position_number order by position_date DESC
EDIT:
SELECT `item_id`, max(`position_number`) position_number , max(`position_date`) position_date FROM TABLENAME
GROUP BY POSITION_NUMBER
ORDER BY POSITION_DATE DESC
DEMO
Here is SQLFiddle
SELECT * FROM TABLE_NAME
GROUP BY POSITION_NUMBER
ORDER BY POSITION_DATE DESC
Try this code:
SELECT * FROM TABLE_NAME GROUP BY POSITION_NUMBER ORDER BY POSITION_DATE DESC
Use GROUP_BY and either MIN or MAX to decide which date in the group you want to use for sorting.
SELECT * FROM my_table GROUP BY position_number ORDER BY MAX(position_date) DESC;
I have a table that goes as follows:
content_id | contact_count
1 23
2 4
3 89
I want to select the content_id with the highest contact_count from the last 25 rows of the table.
I've tried many different things such as:
select content_id from research_products_content
where contact_count=(select max(contact_count)
from research_products_content order by rating_total_id desc limit 25)
order by rating_total_id desc limit 1
In your example, limit 25 is applied after the result (max, which is a single row) is selected. Try this instead:
SELECT tmp.content_id FROM (
SELECT *
FROM research_products_content
ORDER BY rating_total_id DESC
LIMIT 25
) AS tmp
WHERE tmp.contact_count = (
SELECT max(tmp2.contact_count)
FROM (
SELECT *
FROM research_products_content
ORDER BY rating_total_id DESC
LIMIT 25
) AS tmp2
)
LIMIT 1
Since the column number will be the same use UNION
select content_id from research_products_content
where contact_count=(select max(contact_count)
from research_products_content order by rating_total_id desc limit 25)
UNION
select content_id from research_products_content
where contact_count=(select max(contact_count)
from research_products_content order by rating_total_id desc limit 1
You might want to implement caching along the way