user2948897
user2948897

Reputation: 169

To use HAVING in SQL, JOIN and COUNT

I need to show the number of salespeople who represent at least one studio, each. My studios table resembles the following:

Studios
-------
StudioID, Studioname, Email, SalesID

The Salespeople table resembles the following:

Salespeople
---------------
SalesID, Firstname, Lastname

I formulated the following query:

 SELECT sp.firstname, 
           sp.lastname, 
           Count(sp.salesid) AS NumberOfSalesPeople 
    FROM   salespeople sp 
           JOIN studios s 
             ON s.salesid = sp.salesid 
    HAVING Count(s.studioid) > 0; 

However, I do not think it is completely correct. Again, the question is: show the number of salespeople who represent at least one studio each.

Upvotes: 2

Views: 249

Answers (1)

Mosty Mostacho
Mosty Mostacho

Reputation: 43434

I need to show the number of salespeople who represent at least one studio, each

The answer to that requirement would be:

SELECT COUNT(DISTINCT SalesId) NumberOfSalesPeople FROM Studios

Not sure why you've added so much additional information in the query you've formulated.

Upvotes: 2

Related Questions