My table is
f1(drinker,shop)
The table has a list of drinkers and the shops which they visit.I need to find the most popular shop.I know I can do a simple group by of shops and order it in a descending way and limit the results to 1 but my doubt is what if two or more shop have the same number of drinkers in that case my query fails.I can't use limit 2 or 3 because I want a general working query and not a one specific to the data.I am running out of ideas.
Note:
Please don't show this way:
select shop from f1 group by shop order by count(*) desc limit 1
In that case, you have to compare the counts. Not so pretty with a group by query:
select shop
from f1
group by shop
having count(*) = (select max(cnt)
from (select count(*) as cnt
from f1
group by shop
) s
);
You could also do this as a subquery:
select shop
from f1 join
(select count(*) as cnt
from f1
group by shop
order by count(*) desc
limit 1
) f1max
group by shop
having count(*) = max(f1max.cnt);
Related
Actually I'm working with the following table fsa_areas:
Note that each area has a responsible
Now, what I need to do, is to order the same table as following:
Note that now the results are ordered by the the responsible with more areas and at the end the responsible with less areas.
Is there a way to order them in that way?
You can use a COUNT subquery in the ORDER BY clause:
select a.*
from fsa_areas a
order by (select count(*) from fsa_areas a1 where a1.Responsible = a.Responsible) desc
Another way is to get the count in a derived table and join the base table to it
select a.*
from (
select Responsible, count(*) as cnt
from fsa_areas
group by Responsible
) r
join fsa_areas a using(Responsible)
order by r.cnt desc
In MySQL 8 you can use COUNT() as window function:
select *, count(*) over (partition by Responsible) as cnt
from fsa_areas
order by cnt desc
Which team has the most number of members on their roster?
Okay so below is the code that I have input currently. It returns all the teams as well as how many people are on each team. I am not sure how to code it to only display the team with the most members as when I try to use a max function and a count function I get an error.
SELECT Team_Name, COUNT(Member.Student_ID_Num)
FROM Teams
JOIN Member ON Teams.Team_Number = Member.Team_Number
GROUP BY Team_Name
you can try below - using limit and order by desc
Select Team_Name, count(Member.Student_ID_Num) as cnt
from Teams join Member on Teams.Team_Number = Member.Team_Number
group by Team_Name
order by cnt desc
limit 1
If you are using MySQL 8+, then the ROW_NUMBER function comes in handy here:
SELECT Team_Name, cnt
FROM
(
SELECT t.Team_Name, COUNT(*) AS cnt,
ROW_NUMBER() OVER (ORDER BY COUNT(*) DESC) rn
FROM Teams t
INNER JOIN Member m
ON t.Team_Number = m.Team_Number
GROUP BY t.Team_Name
) t
WHERE rn = 1;
If you instead want all ties for the highest count, should two or more teams be tied, then replace ROW_NUMBER with RANK.
If you have to do this the old fashioned way, without LIMIT or ROW_NUMBER, then get ready for a really ugly query:
SELECT
t.Team_Name,
COUNT(*) AS cnt
FROM Teams t
INNER JOIN Member m
ON t.Team_Number = m.Team_Number
GROUP BY t.Team_Name
HAVING COUNT(*) = (SELECT MAX(cnt) FROM (SELECT COUNT(*) AS cnt
FROM Teams t
INNER JOIN Member m
ON t.Team_Number = m.Team_Number
GROUP BY t.Team_Name) t );
In order to get the team with the highest member count, we need no join (unless we also want to show the member count). One thing to keep in mind that there may be multiple teams sharing the same maximum member count.
The old fashioned way in standard SQL, before there was any limit clause (FETCH FIRST ROWS in standard SQL) was this:
Count members per team number.
Get the maximum count.
Get the team number(s) with this maximum count.
Get the team(s) for these team numbers.
The query:
select *
from teams
where team_number in
(
select team_number
from member
group by team_number
having count(*) =
(
select max(cnt) as max_cnt
from
(
select count(*) as cnt
from member
group by team_number
) counted
)
);
As of MySQL 8 we would rather use a wnindow function, however:
select *
from teams
where (team_number, 1) in
(
select team_number, rank() over (order by count(*) desc)
from member
group by team_number
);
**Above is a picture of this particular table*
I need to write a query for a database that lists the name of a department for the department that controls the most projects.
In my database, departments are identified by dnums.
So my question is, how can I write something that checks for the greatest occurrence of a Dnum in SQL? Because that's how I will identify the department that controls the most projects.
I've tried several different queries, but none of them work properly.
Could anyone explain a method that could compare occurrences?
You know already how to count per department:
select dnum, count(*) from project group by dnum;
In SQL Server it is easy to select the dnum(s) with the maximum occurrences; you order by count descending and take the top row(s) using TOP() WITH TIES.
select top(1) with ties dnum from project group by dnum order by count(*) desc;
(In standard SQL that would be order by count(*) desc fetch 1 row with ties).
In standard SQL (and SQL Server) you also have the option of ranking your records per count:
select dnum
from (select dnum, rank() over (order by count(*) desc) as rnk from project) ranked
where rnk = 1;
MySQL doesn't give you any of these options, lacking both a WITH TIES clause and analytic functions such as RANK.
So in MySQL you would not find the departments with the maximum count in one step, but only the maximum count alone first. You would get the according department(s) only in a second step.
Two approaches here:
select count(*) from project group by dnum order by count(*) desc limit 1;
or
select max(cnt) from (select count(*) as cnt from project group by dnum) counted;
Then join the counted departments again:
select p.dnum
from
(
select count(*) as cnt
from project
group by dnum
order by count(*) desc limit 1
) m
(
select dnum, count(*) as cnt
from project
group by dnum
) p on p.cnt = m.cnt;
The last step is the same in both DBMS:
select dname
from departments
where dnumber in (select dnum ...);
(Or join the departments table instead, so you can show both name and count.)
You can use the COUNT function:
SELECT dnum, COUNT(*)
FROM project
GROUP BY dnum
ORDER BY COUNT(*)
If you need the department_name, you'll have to join to your department table (assuming you have one). It could look something like this:
SELECT d.dnum, d.name, COUNT(p.pnumber)
FROM department d
INNER JOIN projects p ON d.dnum = p.dnum
GROUP BY d.dnum, d.name
ORDER BY COUNT(p.pnumber)
I use mysql. My table look like this:
Last I try to use this query
SELECT * FROM movie GROUP BY `group` HAVING cateogry = 'TV'
I want with this query result as: show all but only GROUP BY TV category, where category = 'TV'
I want this Result
But my query give me this result (HAVING in query work as WHERE clouse)
IF I use this QUERY
SELECT * FROM movie GROUP BY `group`
It give me this result
I want -> QUERY -> GROUP BY group (ID no 9 and ID 1,2,3 treat as different group name)
IF group has all same values BUT category='movie' (RETURN ALL ROWS
group by NOT APPLY)
IF group has all same values BUT category='TV' (RETURN 1 ROW group by APPLY)
You seem to want this query:
select m.*
from movie m join
(select `group`, min(id) as minid
from movie
group by `group`
) g
on m.id = g.minid;
SELECT min(ID) as ID, min(Name), `group`, Category
FROM movie
GROUP BY `group`, Category
ORDER BY ID
Have you tried the below? I think you are pretty close. As when you are grouping your 'group' t. You are also grouping the one whose category is movie as well. So you just need to create a separate group Category.
SELECT * FROM movie
WHERE group = 't'
GROUP BY group, Category
ORDER BY ID
I'm having some difficulties with a query:
SELECT d.*,
(SELECT COUNT(id) FROM downloads WHERE category = d.category) AS count
FROM downloads d
GROUP BY d.category
ORDER BY count DESC
So, I'm trying to get the total downloadcount of each category but this query hangs each time I run it.
The downloads table has +- 20000 rows
What am I doing wrong?
SELECT category, count(id) as count
FROM downloads d
GROUP BY d.category
ORDER BY count DESC
I think you'd be better off doing this:
SELECT category, count(*) as cnt
FROM downloads
GROUP BY category
ORDER BY cnt desc;