Instagram
youtube
Facebook
Twitter

List All Products and Their Categories, Even If the Category Is Null

List All Products and Their Categories, Even If the Category Is Null

Product Table:



Categories table:


Query Explanation:

  • SELECT Products.ProductName, Categories.CategoryName:
    This selects the product name and its category name.

  • FROM Products:
    You're selecting data starting from the Products table.

  • LEFT JOIN Categories:
    A LEFT JOIN ensures that all products will be included — even if they don't have a matching category.

  • ON Products.CategoryID = Categories.CategoryID:
    This connects each product to its category using the CategoryID.

    SQL Query:

    SELECT Products.ProductName, Categories.CategoryName
    FROM Products
    LEFT JOIN Categories ON Products.CategoryID = Categories.CategoryID;
    

    Output: