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

  • Home
  • SEARCH
  • 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 3281694
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:50:38+00:00 2026-05-17T19:50:38+00:00

I have two tables for an online shop: one for categories: id, title one

  • 0

I have two tables for an online shop:

  • one for categories: id, title
  • one for products: id, ownerid, title, price (ownerid is the id of the parent category)

I want to select all the categories and also select the minimum and maximum price in each, hence the following query:

SELECT
sc.*, MIN(s.price) AS minp, MAX(s.price) AS maxp
FROM
categories AS sc, products AS s
WHERE s.ownerid=sc.id
GROUP BY sc.id

It works pretty much as expected with the only exception that if a category doesn’t have any product in it, then it is not selected. While this seems logical since I ask for “s.ownerid=sc.id”, I don’t know enough SQL to make it work as intended. I need all the categories and for the ones that don’t have products minp and maxp should be 0.

Any advice? Thanks.

  • 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-17T19:50:39+00:00Added an answer on May 17, 2026 at 7:50 pm

    To do this you need an outer join. By the way, the way you are writing your query with an implicit join is outdated and no longer recommended. Using the JOIN keyword is recommended. This also makes it easier to change an inner join to an outer join.

    FROM categories AS sc
    LEFT JOIN products AS s
    ON s.ownerid=sc.id
    

    To return 0 instead of NULL use IFNULL(..., 0). The entire query becomes:

    SELECT
        sc.*,
        IFNULL(MIN(s.price), 0) AS minp,
        IFNULL(MAX(s.price), 0) AS maxp
    FROM categories AS sc
    LEFT JOIN products AS s
    ON s.ownerid = sc.id
    GROUP BY sc.id
    

    You may also want to consider if it would be better to return the default NULL instead of 0 for categories that have no products.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables connected with one to many relationship. Parent Table is a
I have two tables 1.Products prod_id prod_name 1 honda 2 hero 3 marcedes 4
I have two tables one with ID and NAME table 1 ID | NAME
I have two tables in my MySQL database, one is a library of all
I have an odd problem. There are two databases. One has all products in
I have two tables nested (master view) for representing my data. The parent table
This is for an online photo library. I have two tables; photoSearch and photos.
I have two tables in my application. The first one, Sections, stores content for
I have two tables, one is 'tags' that stores tid (auto increment) and name
I have two tables images2 and image_data So the goal is to have 1

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.