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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:39:39+00:00 2026-05-20T22:39:39+00:00

I have two tables orders, and line_items with following structure: Orders (id = PK,

  • 0

I have two tables orders, and line_items with following structure:

Orders (id = PK, indexes on user_id)
-------------------------------------
id   user_id
==   ======
1     1
2     2
3     1
4     3
5     1

LineItems (id = PK, indexes on order_id and product_id)
id   order_id product_id quantity
==   ======   ========   ======
1      1         1       1
2      1         2       2
3      2         1       4
4      2         3       6
5      3         1       1
6      4         1       1
7      5         1       1

I am trying to find the most efficient way to solve the following requirements:

  • Given a user and a product find the LineItems belonging to Orders where given product is the only product ordered.
    E.g: If user_id is 1 and product_id is 1, the query should return line items 5 and 7.

  • Given a user and a product find the Orders where given product is the only product ordered.
    E.g: If user_id is 1 and product_id is 1, the query should return orders 3 and 5.

The Orders and LineItems table can have millions of rows.

I have a working solution that uses COUNT and HAVING. I am not certain that this is the most efficient solution.

Specifically, I am wondering if this can be addressed by using the technique outlined by Cletus in this answer.

Note:
I am using Orders and LineItems tables to describe the scenario. My actual table is quite different and it not related to order etc.

Edit 2

Is this query efficient than using GROUP BY and HAVING?

SELECT A.id
FROM   LineItems A
JOIN   Orders B ON B.id = A.order_id AND B.user_id = 1
LEFT OUTER JOIN LineItems C ON C.order_id = A.order_id AND 
                               C.product_id != A.product_id   
WHERE  A.product_id = 1 AND C.id IS NULL
  • 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-20T22:39:40+00:00Added an answer on May 20, 2026 at 10:39 pm
    select o.id OrderID, MIN(i.id) LineItemID
    from orders o
    inner join lineitems i on i.order_id = o.id
    where o.user_id= 1
    group by o.id
    having count(*)=1
    

    GROUP BY, HAVING, COUNT is the most efficient for this type of query. Basically it will scan the required data fully, but only within the user’s orders, but in that single pass will produce the result.

    You can kill two birds with one stone, since for orders with a single line item, min(i.id) gives you the (only) LineItemID.

    Indexes you NEED to have: orders.user_id, lineitems.order_id

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

Sidebar

Related Questions

I have two tables order [ order_id ] order_line [ order_id, product_id, cat_id ]
I have two tables Orders and Order_Details Order_Details tables's order_id field acts as foreign
I have two tables: Orders OrderProducts - An order can have 1 to many
I have these two tables: Table: ORDERS ID - NAME - DATA --------------------------------------------- 1
I have a two tables cstomers and orders table. These tables contain 20,000 records
I have two Tables. Order - With Columns OrderID, OrderStatusID OrderStatus - With Columns
Suppose I have a dataset with those two immortal tables: Employee & Order Emp
I have two tables that are joined together. A has many B Normally you
I have two tables, Book and Tag , and books are tagged using the
We have two Tables: Document: id, title, document_type_id, showon_id DocumentType: id, name Relationship: DocumentType

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.