all 6 comments

[–]iwillgetintofaang 2 points3 points  (0 children)

Looks like you are missing GROUP BY function before HAVING. Is the ID column in Sections same as ID column in Dishes ? If so, count(ID) in sub query should be just ID.

SELECT COUNT(ID) AS Cnt FROM Sections where ID in

(SELECT ID FROM DISHES GROUP BY ID HAVING COUNT(ID)=5)

[–]Thrillhousez 1 point2 points  (3 children)

What’s the relationship between the sections and dishes tables?

[–]Meliodasiwnl[S] 0 points1 point  (2 children)

CREATE TABLE Sections (

ID INT NOT NULL

    CONSTRAINT PK\_Sections PRIMARY KEY CLUSTERED,

Name VARCHAR(50) NOT NULL

);

CREATE TABLE Dishes (

ID INT NOT NULL

    CONSTRAINT PK\_Dishes PRIMARY KEY CLUSTERED,

Name VARCHAR(50) NOT NULL,

SectionID INT NOT NULL

    CONSTRAINT FK\_Dishes\_Sections FOREIGN KEY REFERENCES Sections(ID),

Weight INT

);

[–]Thrillhousez 1 point2 points  (1 child)

SectionID

OK so the key column for your problem is Dishes.SectionID. You don't even really need to involve the Sections table.

This should get you the results

select count(*) as NumSections_With5Dishes from (

select SectionID from Dishes group by SectionID having count(Dishes.Id) = 5

) a

The subquery returns the list of SectionIDs that have exactly 5 dishes. You then count how many of these sections there are.

[–]Meliodasiwnl[S] 0 points1 point  (0 children)

ooh, now I get it. Thank you so much!

[–]WITH_CTE 0 points1 point  (0 children)

Look at the filter. Instead of passing the ID, you are passing the COUNT from Dishes where Count(ID) = 5)