Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 680647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:23:45+00:00 2026-05-14T01:23:45+00:00

In sql server 2008, I have the following query: select c.title as categorytitle, s.title

  • 0

In sql server 2008, I have the following query:

select      
    c.title as categorytitle,
    s.title as subcategorytitle,
    i.title as itemtitle
from categories c
join subcategories s on c.categoryid = s.categoryid
left join itemcategories ic on s.subcategoryid = ic.subcategoryid 
left join items i on ic.itemid = i.itemid and i.siteid = 132
where (ic.isactive = 1 or ic.isactive is null)
order by c.title, s.title

I am trying to get items in their subcategories, but I still want to return a record if there are no items in the category or subcategory. Subcategories that have no items are never returned. What am I doing wrong?

Thank you

EDIT

Modified query with a second left join and where clause, but it’s still not returning nulls. :/

EDIT 2

Moved siteid to item left join. When I do this I get way more records than expected. Some items have a null siteid and I only want to included them when they have a specific id.

EDIT 3

Table structure:

Categories Table 
-------
CategoryID
Title

SubCategories Table
-------
SubCategoryID
CategoryID
Title

ItemCategories Table
-------
ItemCategoryID
ItemID
SubCategoryID
IsActive

Items Table 
--------
ItemID
Title
SiteID
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:23:45+00:00Added an answer on May 14, 2026 at 1:23 am

    change join items i… to LEFT join items i… and your query should work as you expect.

    EDIT
    You can not filter LEFT JOIN tables in the where clause unless you account for nulls, because the left join allows those columns to have a value or be null when no rows matches:

    and i.siteid = 132 will throw away any of your rows that have a NULL i.siteid, where none existed. Move this to the ON:

    left join items i on ic.itemid = i.itemid and i.siteid = 132

    or make the WHERE handle NULLs:

    WHERE ... AND (i.siteid = 132 OR i.siteid IS NULL)

    EDIT based on OP’s edit 3

    SET NOCOUNT ON
    DECLARE @Categories table (CategoryID int,Title varchar(30))
    INSERT @Categories VALUES (1,'Cat AAA')
    INSERT @Categories VALUES (2,'Cat BBB')
    INSERT @Categories VALUES (3,'Cat CCC')
    
    DECLARE @SubCategories table (SubCategoryID int,CategoryID int,Title varchar(30))
    INSERT @SubCategories VALUES (1,1,'SubCat AAA A')
    INSERT @SubCategories VALUES (2,1,'SubCat AAA B')
    INSERT @SubCategories VALUES (3,1,'SubCat AAA C')
    INSERT @SubCategories VALUES (4,2,'SubCat BBB A')
    
    DECLARE @ItemCategories table (ItemCategoryID int, ItemID int, SubCategoryID int, IsActive char(1))
    INSERT @ItemCategories VALUES (1,1,2,'Y')
    INSERT @ItemCategories VALUES (2,2,2,'Y')
    INSERT @ItemCategories VALUES (3,3,2,'Y')
    INSERT @ItemCategories VALUES (4,4,2,'Y')
    INSERT @ItemCategories VALUES (5,7,2,'Y')
    
    DECLARE @Items table (ItemID int, Title varchar(30), SiteID int)
    INSERT @Items VALUES (1,'Item A',111)
    INSERT @Items VALUES (2,'Item B',111)
    INSERT @Items VALUES (3,'Item C',132)
    INSERT @Items VALUES (4,'Item D',111)
    INSERT @Items VALUES (5,'Item E',111)
    INSERT @Items VALUES (6,'Item F',132)
    INSERT @Items VALUES (7,'Item G',132)
    SET NOCOUNT OFF
    

    I’m not 100% sure what the OP is after, this will return all info that can be joined when the siteid=132 as given in the question

    SELECT
        c.title as categorytitle
            ,s.title as subcategorytitle
            ,i.title as itemtitle
            --,i.itemID, ic.SubCategoryID, s.CategoryID
        FROM @Items                          i
            LEFT OUTER JOIN @ItemCategories ic ON i.ItemID=ic.ItemID
            LEFT OUTER JOIN @SubCategories   s ON ic.SubCategoryID=s.SubCategoryID
            LEFT OUTER JOIN @Categories      c ON s.CategoryID=c.CategoryID
        WHERE i.siteid = 132
    

    OUTPUT:

    categorytitle                  subcategorytitle               itemtitle
    ------------------------------ ------------------------------ ------------------------------
    Cat AAA                        SubCat AAA B                   Item C
    NULL                           NULL                           Item F
    Cat AAA                        SubCat AAA B                   Item G
    
    (3 row(s) affected)
    

    This will list all categories, even if there is no match to the siteid=132

    ;WITH AllItems AS
    (
    SELECT
        s.CategoryID, ic.SubCategoryID, ItemCategoryID, i.ItemID
            ,c.title AS categorytitle, s.title as subcategorytitle, i.title as itemtitle
        FROM @Items                          i
            LEFT OUTER JOIN @ItemCategories ic ON i.ItemID=ic.ItemID
            LEFT OUTER JOIN @SubCategories   s ON ic.SubCategoryID=s.SubCategoryID
            LEFT OUTER JOIN @Categories      c ON s.CategoryID=c.CategoryID
        WHERE i.siteid = 132
    )
    SELECT
        categorytitle, subcategorytitle,itemtitle
        FROM AllItems
    UNION
    SELECT
        c.Title, s.Title, null
        FROM @Categories                     c
            LEFT OUTER JOIN @SubCategories   s ON c.CategoryID=s.CategoryID
            LEFT OUTER JOIN @ItemCategories ic ON s.SubCategoryID=ic.SubCategoryID
            LEFT OUTER JOIN AllItems         i ON c.CategoryID=i.CategoryID AND  s.SubCategoryID=i.SubCategoryID
        WHERE i.ItemID IS NULL
    ORDER BY categorytitle,subcategorytitle
    

    OUTPUT:

    categorytitle                  subcategorytitle               itemtitle
    ------------------------------ ------------------------------ ------------------------------
    NULL                           NULL                           Item F
    Cat AAA                        SubCat AAA A                   NULL
    Cat AAA                        SubCat AAA B                   Item C
    Cat AAA                        SubCat AAA B                   Item G
    Cat AAA                        SubCat AAA C                   NULL
    Cat BBB                        SubCat BBB A                   NULL
    Cat CCC                        NULL                           NULL
    
    (7 row(s) affected)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.