3

I have an SQL database with one table: Product (maker, type, model)

For each row we have a different maker (brand), a type of product(pc, tablet, laptop, etc.) and a model (1.1 / 2.0 / 3.4.5 etc)

Q: How do I get the makers who produce only one product type and more than one model.

Output: maker, type.

Hints: GROUP BY and HAVING statements

I tried the following query, but it didn't work out, as it was not returning any value in the resulting table:

SELECT
maker, type
FROM
product
GROUP BY maker, type
HAVING count(model)>1 and count(type)=1

What am I doing wrong in this SELECT? Thank you very much!

Victor A
  • 63
  • 5

1 Answers1

2

You were very close:

SELECT maker, MIN(type) type
FROM product
GROUP BY maker
HAVING COUNT(DISTINCT type) = 1
AND COUNT(DISTINCT model) > 1;
Lamak
  • 69,480
  • 12
  • 108
  • 116