FruitJuice
FruitJuice

Reputation: 33

SQL COUNT how many times a value appears

I have a database and I'm tasked to find out how many times the book 'Hood' has been borrowed. I know it's twice

Given Database

I have to write SQL to return how many times it was borrowed. So far I have this but it only returns how many unique Books there are not how many times 'Hood' was borrowed

select count('Hood') as lenttimes
from
(
    select distinct bTitle from borrow
);

Upvotes: 3

Views: 14662

Answers (4)

MariaVincent
MariaVincent

Reputation: 214

This will return number of rows in your table:

SELECT COUNT(*) FROM borrow WHERE bTitle='Hood';

Upvotes: 0

Tajinder
Tajinder

Reputation: 2338

Hoping, i understood your problem correctly.

Please check below query.

select count(0) from borrow where btitle='Hood'

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1269753

I think you want:

select sum(iff(btitle = 'Hood', 1, 0)) as lenttimes
from borrow;

This uses sum(iff()) so you can count more than one title (in another column).

Upvotes: 3

Mureinik
Mureinik

Reputation: 311228

All you need to do is apply a where clause and count the results:

SELECT COUNT(*)
FROM   borrow
WHERE  bTitle = 'Hood'

Upvotes: 2

Related Questions