SQL - Displaying entries that are the max of a count?

后端 未结 7 1438
悲&欢浪女
悲&欢浪女 2020-12-01 12:35
CREATE TABLE doctor( patient CHAR(13), docname CHAR(30) );

Say I had a table like this, then how would I display the names of the doctors that have

相关标签:
7条回答
  • 2020-12-01 13:29

    This should do it.

    SELECT docname, COUNT(*) FROM doctor GROUP BY name HAVING COUNT(*) = 
        (SELECT MAX(c) FROM
            (SELECT COUNT(patient) AS c
             FROM doctor
             GROUP BY docname))
    

    On the other hand if you require only the first entry, then

    SELECT docname, COUNT(docname) FROM doctor 
    GROUP BY name 
    ORDER BY COUNT(docname) DESC LIMIT 1;
    
    0 讨论(0)
提交回复
热议问题