Hope everyone's having a splendid day (or night).I'm battling with this query. I have 2 tables, Authors and Books. I'd like to pull out a list of Authors' names and any books they've written with > 400 pages. Easy for a simple inner join - but I want to show *all* authors, even if they don't have books which meet the criteria. That is, the book fields would be null.CREATE TABLE #testAuthors ( AuthorId int NOT NULL PRIMARY KEY CLUSTERED, AuthorName varchar(100) NOT NULL)CREATE TABLE #testBooks ( BookId int NOT NULL PRIMARY KEY CLUSTERED, AuthorId int NOT NULL, --REFERENCES #testAuthors (AuthorId), BookTitle varchar(100) NOT NULL, TotalPages int NOT NULL)GOINSERT INTO #testAuthors (AuthorId, AuthorName)SELECT 1, 'Douglas Adams' UNION ALLSELECT 2, 'Neil Gaiman' UNION ALLSELECT 3, 'Another Author'GOINSERT INTO #testBooks (BookId, AuthorId, BookTitle, TotalPages)SELECT 1, 1, 'Salmon of Doubt', 336 UNION ALLSELECT 2, 1, 'Mostly Harmless', 240 UNION ALLSELECT 3, 2, 'American Gods', 624 UNION ALLSELECT 4, 2, 'Anansi Boys', 416GOSELECT a.AuthorName, b.BookTitle, b.TotalPagesFROM #testAuthors a LEFT OUTER JOIN #testBooks b ON b.AuthorId = a.AuthorIdWHERE b.TotalPages > 400ORDER BY a.AuthorName, b.BookTitle, b.TotalPages DESCGO
The query above gets only the matching author:Neil Gaiman | American Gods | 624Neil Gaiman | Anansi Boys | 416
However I'd like to retrieve *all* authors, with NULL book fields if no books match (or none exist). Desired outcome is this:Another Author | NULL | NULLDouglas Adams | NULL | NULLNeil Gaiman | American Gods | 624Neil Gaiman | Anansi Boys | 416
How would I go about getting this result, in the most efficient way?Many thanks!