It's a while since I have used SQL statements, but could you use the having clause to do something like that? (HAVING MIN(count(grade))), or something like that?
select grade,count(*)
from student
group by grade
having
min(select count(*) from student group by grade)
<
max(select count(*) from student group by grade)
No luck (syntax error this time). This is why I prefer C for such things...
USE [Master]
GO
IF (EXISTS(SELECT * FROM [SYSOBJECTS] WHERE [Name] = 'Students' AND TYPE = 'U'))
BEGIN
DROP TABLE [Students]
END;
GO
CREATE TABLE [Students] (StudentID INT PRIMARY KEY NOT NULL IDENTITY(0, 1),
StudentName VARCHAR(50) NOT NULL UNIQUE,
Grade INT NOT NULL);
GO
BEGIN TRANSACTION [Inserts]
INSERT INTO [Students] ([StudentName], [Grade]) VALUES ('Name1', 100);
INSERT INTO [Students] ([StudentName], [Grade]) VALUES ('Name2', 100);
INSERT INTO [Students] ([StudentName], [Grade]) VALUES ('Name3', 50);
INSERT INTO [Students] ([StudentName], [Grade]) VALUES ('Name4', 20);
INSERT INTO [Students] ([StudentName], [Grade]) VALUES ('Name5', 20);
COMMIT TRANSACTION
GO
SELECT COUNT(*) AS 'Count of the least Grade' FROM [Students] WHERE
[Grade] = (SELECT MIN([Grade]) FROM [Students])
Coded in SQL Query Analyzer 2000.
On the field with sword and shield amidst the din of dying of men's wails. War is waged and the battle will rage until only the righteous prevails.
Hey pcmattman, did you find a working query using having? This is the best I could come up with, but I shudder to think how inefficient it probably is:
SELECT DISTINCT grade g FROM student WHERE
(SELECT count(*) FROM student WHERE grade=g) =
(SELECT min(z.cnt) FROM (SELECT count(*) cnt FROM student GROUP BY grade) z)
khumba.net - prototype capability language and operating environment
"Lose your questions and you will find your answers" -D'ni Proverb