473,394 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

how to find maximum occurences of distinct value?

101 100+
Say we have a couple of tables like so:

Departments_to_Employees
department_id
employee_id

Employee
employee_id

I want to find how many employees the department with the maximum amount of employees has.

On other words, I want to find the department with the most amount of employees, then find out how many employees are in that department.

Is there any way to do this using a single SQL statment? I've searched on google, but I haven't seen any articles that go over this specific issue.
Jun 26 '08 #1
5 2948
coolsti
310 100+
First of all, you are showing the wrong table structure for this kind of thing. A department has employees, yes, but it is the employee that belongs to a department, not a department that belongs to an employee. Changing the design like this will make queries easier. In other words:

CREATE TABLE `departments` (
`iddepartment` smallint(5) unsigned NOT NULL auto_increment,
`departmentname` varchar(50) default NULL,
PRIMARY KEY (`iddepartment`)
)
CREATE TABLE `employees` (
`idemployee` smallint(5) unsigned NOT NULL auto_increment,
`iddepartment` smallint(5) unsigned default NULL,
`employeename` varchar(50),
PRIMARY KEY (`idemployee`)
)

Now you can do something like this:

select t1.iddepartment,departmentname,count(*) as number_employees
from departments t1, employees t2 where t1.iddepartment=t2.iddepartment
group by t1.iddepartment order by number_employees desc limit 1

This will not give you more than one row returned if more than one department has the same amount of employees as the maximum. For that, you would need a more complicated query. But this will give you what you asked for, namely the maximum count of employees in all departments. In the above example, I also extract the department ID and the department name. You could also get a deliminated list of the employees in the department using a group_concat(employeename).
Jun 27 '08 #2
zensunni
101 100+
First of all, you are showing the wrong table structure for this kind of thing. A department has employees, yes, but it is the employee that belongs to a department, not a department that belongs to an employee.
You assumed that the relationship was one-to-many. That would be easy. But, this scenario is a many-to-many relationship. An employee can have more than one department and a department can have more than one employee.

Department_to_employee is a lookup table.
Jun 27 '08 #3
coolsti
310 100+
ah ok, I didn't realize that.

Then why would this not work?

select department_id, count(*) as number_employees
from Departments_to_Employees
group by department_id order by number_employees desc limit 1
Jun 28 '08 #4
zensunni
101 100+
Never thought to put it together like that. Thanks!
Jun 29 '08 #5
coolsti
310 100+
Just to let you know, there is a more complicated way of doing this, which can be used if you require a bit more from your select. In your case, you only want one row returned, which is for the department with the maximum value. On the other hand, if you wanted many rows returned, you would need the more complicated way of doing it.

Example: let us say you have another table that holds the number of sick days per employee, and you then wanted a query that shows the employee with the maximum number of sick days per department. Then your result would not have just one row, but many, one for each department.

Unfortunately, I cannot show you right now the solution to this, as I cannot remember it!!! Each time I need this myself I need to search through all my code for an example of how I did it, which I initially found via a search on the net. But it is possible!
Jun 30 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Cam Bevis | last post by:
I'm having trouble getting my head around this, and no one in the groups has posted exactly the problem. The table below tracks site traffic across a network. There is 1 row per pageview and...
0
by: JR | last post by:
Hi, i found a user control on the net see code below it is part of VSEssential but I like the coolProgressBar. Zo I want to use it into my program and I always get the error that it can't find...
5
by: Sunny123 | last post by:
hello i am trying to find the maximum value of a function and where it occur. i.e there is an array x |y ============= | |
4
by: Dameon | last post by:
Hi All, I have a process where I'd like to search the contents of a file(in a dir) for all occurences (or the count of) of a given string. My goal is to focus more on performance, as some of the...
8
by: Daneel | last post by:
Hello! I'm looking for an algorithm which finds all occurences of a bit sequence (e.g., "0001") in a file. This sequence can start at any bit in the file (it is not byte aligned). I have some...
6
by: =?Utf-8?B?U2hhcm9u?= | last post by:
I'm using the VScrollBar and set it as follow: m_vScrollBar.Minimum = -19602; m_vScrollBar.Maximum = 0; m_vScrollBar.SmallChange = 1; m_vScrollBar.LargeChange = 1089; m_vScrollBar.Value =...
2
choke
by: choke | last post by:
I need to write a function in devc++ that creates an array of n integers, each element is equal to n*n+i*i-n*i where i is from 0 to n-1 as the array index. Within the same function I need to find...
18
by: Neehar | last post by:
Hello For one of the interviews I took recently, I was given an offline programming quiz. In 30 minutes I had to write code in C++ to counts the number of times each unique word appears in a...
4
by: raylopez99 | last post by:
I would like to know if there's a quick "Linq" way to find the index of an array having a particular value. I can do this the long way by sequential iteration, but would like to know if there's a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.