Count the GROUP BY rows

A friend of mine asked me to solve his university#%92s exercise. In part of that exercise, I had to count a GROUP BY result rows.

create table book (
ISBN char(10),
Title char(20) not null,
category char(12),
primary key (ISBN)
);

Insert into book (isbn, title, category) values ('BN1001', 'Advanced SQL', 'Tech');
Insert into book (isbn, title, category) values ('BN1002', 'Harry Porter', 'Fiction');
Insert into book (isbn, title, category) values ('BN1003', 'Java Without Pain', 'Tech');
Insert into book (isbn, title, category) values ('BN1004', 'Rocky Mountains', 'Travel');
Insert into book (isbn, title, category) values ('BN1005', 'Banff', 'Travel');

The following query returns 3 rows.

SELECT Category
FROM Book
GROUP BY Category

But I need only the number 3 not three lines. I could do it by the following script but is there any other way?

SELECT Count( DISTINCT Category)
FROM Book
GROUP BY Category

2. Luis MartinModerator

Script:

SELECT Count( DISTINCT Category)
FROM Book
GROUP BY Category

Show:
1
1
1

no number 3.
Do you want number 3, because there is 3 differentes categories, or last output?

Luis Martin
Moderator
SQL-Server-Performance.com

All postings are provided â€œAS ISâ€ with no warranties for accuracy.

3. bambolaNew Member

Maybe he means

SELECT Count(*), Category
FROM Book
GROUP BY Category
4. ChappyNew Member

Or simply

SELECT COUNT(DISTINCT Category) from Book

Oh! I am so sorry Luis. You are right, The script that solved my problem was as below:

SELECT Count(DISTINCT Category) FROM Book

quote:Originally posted by LuisMartin

Script:

SELECT Count( DISTINCT Category)
FROM Book
GROUP BY Category

Show:
1
1
1

no number 3.
Do you want number 3, because there is 3 differentes categories, or last output?

Yes, this was the script that solved my problem but now the question is that: is there any other way?

quote:Originally posted by Chappy

Or simply

SELECT COUNT(DISTINCT Category) from Book