JustAnotherGuyOnline
JustAnotherGuyOnline

Reputation: 47

SQL - Select all Rows, from specific columns, whilst counting individual things

I'm trying to work this out in SQLite3, and been failing for hours now!

Imagine I have this table (named products), for example purposes:

Company | Product | Price (£)
--------|---------|----------
Apple   | iPhone  |   349
Samsung | Galaxy  |   300
Sony    | PS4     |   229
Sony    | Xperia  |   249

What query would I use, to show how many products there are by each company? Presenting similar results to the following:

Company |   ???
--------|---------
Apple   | 1 
Samsung | 1 
Sony    | 2     

Any help would be much appreciated, as it's stressing me out!

Thanks!

Upvotes: 2

Views: 21

Answers (1)

juergen d
juergen d

Reputation: 204894

Group by the company and then use the aggregate function count(). Aggregate functions apply to each group if there are groups.

select company, count(*)
from your_table
group by company

Upvotes: 2

Related Questions