0

I have a table products, and each product has a specific brand.

What I want to do is to count the times a specific brand has appeared and, together, show the unique product it has.

For example:

Table products

ID, ProductName, Brand
1, TV, Samsung
2, TV, Sony
3, TV, Samsung
4, Phone, Sony

The query should result:

ProductName, Brand, Count
TV, Samsung, 2
TV, Sony, 1
Phone, Sony, 1

Can you help?

Sorry I'm writing on a phone and on the bus that's why I didn't specify but please feel free to ask if you didn't understand.

3
  • 1
    This is really basic aggregation, any SQL tutorial should explain how to do it. Commented Jun 14, 2018 at 21:51
  • What have you done so far??? Try Google Commented Jun 14, 2018 at 21:52
  • Thanks! Wouldn't ask here if I found on Google, with all due respect. Was trying to use distinct but realized it was unnecessary. Commented Jun 14, 2018 at 22:21

2 Answers 2

3

Easy with Group by

SELECT productName, Brand, COUNT(*)
FROM yourdb
GROUP BY productName, Brand
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Was using distinct but I've realized it is unnecessary.
2

Looks like you are looking for GROUP BY: https://www.w3schools.com/sql/sql_groupby.asp

Give this a try:

 SELECT ProductName, Brand, count(id) AS Count FROM products GROUP BY ProductName, Brand

1 Comment

Thanks! Am giving you the correct answer for the quickness.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.