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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:42:40+00:00 2026-06-14T20:42:40+00:00

I have two tables, orders and ordered_products . ORDERS |ORDERS_ID|CUSTOMER NAME|… |1 |PIPPO |…

  • 0

I have two tables, orders and ordered_products.

ORDERS
|ORDERS_ID|CUSTOMER NAME|...
|1        |PIPPO        |...
|2        |PLUTO        |...

ORDERED PRODUCTS
|ORDERED_ID|ORDERS_ID|PRODUCT  |PRODUCT_TYPE|...
|1         |1        |ProdottoA| 1          |...
|2         |1        |ProdottoB| 2          |...
|3         |1        |ProdottoC| 1          |...
|4         |2        |ProdottoD| 2          |...

I need two queries, the first to select all orders that have at least one product of type 1, the second to select all orders that have ALL products of type 1.

For the first one i have solved with the following query:

select distinct orders_id from ORDERS o left join ORDERED_PRODUCTS op on (o.orders_id=op.orders_id) where op.product_type = '1'

But I can’t find a solution for the second query.


Solution found! I used:

select distinct orders_id from ORDERS o left join ORDERED_PRODUCTS op on (o.orders_id=op.orders_id) 
where
(select count(ordered_id) from ordered_products op where op.orders_id = o.orders_id)
=
(select count(ordered_id) from ordered_products op where op.orders_id = o.orders_id and op.orders_products_categorizzazione='1')

Thank you for the help

  • 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-14T20:42:42+00:00Added an answer on June 14, 2026 at 8:42 pm

    Not tested, but this should work :

    select orders_id
    from orders
    where 
      ( 
      select count(distinct product)
        from ordered_products
        where ordered_products.orders_id = orders.orders_id
        and ordered_products.product_type = 1
      )
      =
      (
      select count(distinct product)
        from ordered_products
        where ordered_products.product_type = 1
      );
    

    What it does :

    • Count all distinct products of type 1 in the current order
    • Count all distinct products of type 1 within all orders
    • Compare results

    It’s far from optimized and there probably are better methods, but it does the work and it’s easily understandable.

    PS : If you have the choice of the database structure, I’d go for having a different table for products themselves. Fits better in UML specifications, less redundancy, better indexing.

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

Sidebar

Related Questions

Let's say I have two tables -- Products and Orders. For the sake of
Using sqlite3, I have two tables: products, orders. I want to know how many
I have two tables as follows PRODUCT table Id | Name | Price And
I have two tables: orders and orders_items. Both sharing the field orderID. I want
Hie frnds, I have two tables say Orders and OrdersXML now I wish to
If I have two tables, Customers and Orders, and I want to look up
I have these two tables Orders and Employees Orders Table OrderID EmployeeID 1 1
I have two tables Orders(ID,ORDERDATE,DELIVERYDATE,GOODID,QUANTITY,COLLECTIONFROM,DELIVERYTO,NOTES) and ROLLINGSTOCK_ORDER(ORDERID,ROLLINGSTOCKID,DEPARTUREDATE,DELIVERYDATE,ROUTEID) i have created a trigger to update
I have two tables Orders table and customers table, both have customerid as common
I have two tables, and am doing an ordered select on each of them.

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.