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 8405249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:48:21+00:00 2026-06-09T22:48:21+00:00

I have two tables in my DB – products & orders. An order can

  • 0

I have two tables in my DB – products & orders. An order can only be of one kind of product.

Here’s the basic idea:

enter image description here

What I’m trying to do is a query that given a copmany_id returns all the products (from that company) that have less than 10 orders (including 0)

my query looks like this:

SELECT p.*
FROM   product p,
       order o
WHERE  p.company_id =?
       AND o.product_id = p.id
GROUP  BY p.id
HAVING Count(o.id) < 10
ORDER  BY p.id DESC

The query works fine for products which have 0 < orders but doesn’t return ones with 0 orders. What do I need to do to return them as well?

  • 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-06-09T22:48:23+00:00Added an answer on June 9, 2026 at 10:48 pm

    You’re INNER JOINING your two tables, which means that only those products are returned for which there is at least one order.
    You will need to LEFT OUTER JOIN the order table:

    SELECT p.*
    FROM   product p
           LEFT OUTER JOIN order o
                        ON o.product_id = p.id
    WHERE  p.company_id = ?
    GROUP  BY p.id
    HAVING Count(o.id) < 10
    ORDER  BY p.id DESC  
    

    A left outer join will return every record on the left-hand side of the JOIN operation at least once, regardless if there is a matching record to the right-hand side of the JOIN operation.

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

Sidebar

Related Questions

I have two tables: product and sale . How can I write a SQL
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 two tables connected with one to many relationship. Parent Table is a
I have two tables inside a database. One stores unique userNames and a unique
I have two tables with this structure: Table one: ID Description Table two: ID
I have two tables. One table contains information about Assets, another Table about their
I have two tables. One is temp table and another is main table. In
I have two tables: one table (okay, it's a view), vComputer, lists many computers

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.