MYSQL: group by and count issues - mysql

I've done research on this problem, but am having trouble finding a solution.
How can I get the output like this ?

You can join your original table to a subquery which tallies the students attending each school.
SELECT t1.student,
t2.count,
t1.school
FROM yourTable t1
INNER JOIN
(
SELECT school, COUNT(*) AS count
FROM yourTable
GROUP BY school
) t2
ON t1.school = t2.school
ORDER BY t2.count DESC,
t1.school
Note that your intended order was not clear from the question. You might have been looking for this ordering:
ORDER BY t1.school,
t1.count -- or t2.count DESC

If you really want every student appears in rows, you should do it like this:
select
yourtable.student, t.cnt as `count`, t.school
from (
select count(id) as cnt, school
from yourtable
group by school
) t
left join yourtable
on yourtable.school = t.school
order by t.school, yourtable.id
or if you just want all the student for every school, you can use group_concat
select
group_concat(student) as student,
count(id) as `count`,
school
from yourtable
group by school
the student will be separeted by , in one row for each school.

Related

Mysql Order By count?

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

Trying to figure out how to find the max count

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
);

Counting number of records from two different tables and grouping them

Table 1: Student(name,enrollment_no,dept,college_id)
Table 2: Faculty(name,faculty_id,dept,college_id)
Desired o/p:
Department, No. of Student, No. of faculty in one table form
query i've been trying :
select
dept,
count(distinct student.dept) as total,
count(distinct faculty.dept) as total1
from student
join faculty
on student.college_id=faculty.college_id
group by dept;
query is not giving any output.
after your help
query is now
select * from (
(select count(*)as stu_count,student.department,student.college_id
from student group by department)T1
inner join
(select count(*)as fact_count,faculty_per.department,faculty_per.college_id
from faculty_per group by department)T2
on T1.college_id=T2.college_id);
Thank you.
select * from ((select count(*)as stu_count,t2.dept,t2.college_id from Student group by dept)T1
inner join
(select count(*)as fact_count,t3.dept,t3.college_id from Facultygroup by dept)T2
on T1.college_id=T2.college_id);
No of students and faculty can be extracted by performing inner join on two separate queries. Distinct is not required as we are doing group by

MySQL take rows and override ones without user_id

I have table like this one:
I would like to all rows, but if there is user_id 5 if this case, override other rows which have no user_id.
I tried both with MAX(user_id) and GROUP BY country_name, but it still returns, wrong results.
Final result I'm expecting:
Try this;)
select t1.*
from yourtable t1
inner join (
select max(user_id) as user_id, country_name from yourtable group by country_name
) t2 on t1.country_name = t2.country_name and t1.user_id = t2.user_id
This is just a solution based on your sample data. If you have a variety of user_id, it should be more different.
As of SQL Select only rows with Max Value on a Column you can easily get rows with max value on a column by using both MAX(column) and GROUP BY other_column in one statement.
But if you want to select other columns too, you have to this in a subquery like in the following example:
SELECT a.*
FROM YourTable a
INNER JOIN (
SELECT country_name, MAX(user_id) user_id
FROM YourTable
GROUP BY country_name
) b ON a.country_name = b.country_name AND a.user_id = b.user_id

How to select the MAX from the COUNT in SQL

That is a picture of my table.
I must select "Fastanumer" of all cars where "Tegund" is the most common value (which is Toyota in this example)
This is the code i tried
SELECT Fastanumer FROM `Bill`
WHERE Tegund =
(SELECT MAX(y.cnt) FROM (SELECT COUNT(Tegund) AS cnt FROM Bill ) AS y)
Which i had to work pretty hard to figure out only to end up beating myself in the head over the fact that MAX will only turn into a number. (And since Tegund isn't a list of numbers...)
Is this even possible? How can i do this?
I guess it should work this way:
SELECT Fastanumer
FROM `Bill`
WHERE Tegund = (
SELECT Tegund
FROM (
SELECT Tegund,COUNT(*) FROM Bill GROUP BY Tegund ORDER BY COUNT(*) DESC LIMIT 1
) t1
)
Or even like this:
SELECT Fastanumer
FROM `Bill`
WHERE Tegund = (
SELECT Tegund FROM Bill GROUP BY Tegund ORDER BY COUNT(*) DESC LIMIT 1
)
Here's my solution:
SELECT Bill.*
FROM Bill
WHERE Tegund IN (
SELECT Tegund
FROM Bill
GROUP BY Tegund
HAVING COUNT(*) = (
SELECT MAX(cnt) FROM (
SELECT COUNT(*) cnt
FROM Bill
GROUP BY Tegund
) s
)
)
A little more complicated than others, but if more than one Tegund shares the same number of rows, this query will show all Tegunds which are the most common.
Please see fiddle here or here.
What you want to do first is figure out which Tegund appears the most in your table. That is what the subquery is doing. Then you will select the Fastanumer which matches that Tegund.
SELECT DISTINCT Fastanumer
FROM 'BILL'
WHERE Tegund = (
SELECT TOPT 1 Tegund,
COUNT(*) as Count
FROM `BILL`
GROUP BY Tegund
ORDER BY Count DESC)
Depends on the DB (and associated SQL). Try
select fastanumber from bill
inner join
(select count(*) as cnt, tegund from Bill group by tegund) grpby
on bill.tegund = grpby.tegund
and grpby.cnt = (select max(cnt) from (select count(*) as cnt, tegund from Bill group by tegund))