SQL Server – Querying Bridge Tables for Aggregates

aggregatesql server

I have three tables dealing with books: Authors, Titles of books written and a bridge table TitleAuthor, from from which I am trying to find all books (Titles) with more than one author. These are the tables:

Authors (Au_Id  PK, Au_fname, Au_lname, ...)

Titles (Title_Id PK, Title, Title_type, ...)

TitleAuthor (Au_Id PK, Title_Id PK, Au_order, ...)

In TitleAuthor, I am not sure of what Au_order means, but I suspect it is the order in which an author's name is cited in a book with more than one author. Au_order is a TinyInt data type, btw. These queries are supposed to be introductory, i.e. no fancy joins or subqueries.

This is what I have tried:

1)

Select Au_Id from `TitleAuthor` where  Count(Auth_id)>1;

But this gives us the author name, not the book name (remember: intro query)

2) Figuring out which books/titles are mentioned more than once in TitleAuthor:

Select Title_id from Title-Author where Count(Title_Id)>1;

This gives me no results for some reason, though no error messages either. I suspect I may need grouping and having, but this is not working either.

Any Ideas?

Best Answer

select Titles.Title_Id, Titles.Title
  from Titles 
  join TitleAuthor 
        on TitleAuthor.Title_Id =  Titles.Title_Id 
 group by Titles.Title_Id, Titles.Title 
having count(*) > 1