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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:21:12+00:00 2026-06-12T23:21:12+00:00

Please help me to write a SQL query to extract unique orders from below

  • 0

Please help me to write a SQL query to extract unique orders from below tables. To do that I Have prepared a SQL query. It’s working fine but in some place its returning some duplicate rows as some orders have more than one products.

For Example:

Customer can buy “IPAD” in one order. However, if they purchase “Ipad” and “Android” as bundle they will receive 50% discount and they can save money. In this case, we will get two products under 1 order. However, customer will pay as one product(i.e $120). To allocate order properly, we have created an extra row for the extra product(if products two). If bundle contain three products, it will create three rows in “product_orders” table respectively.

Example “product_orders” table for multiple products:

id orders_id product_id qty
 1   3      2         1
 2   2      1         1
 3   3      3         1

Please check above example table, one order_id copied twice for the bundle order.
If we able to extract one order information, we will get the whole bundle information. So, we don’t need the extra row.

The Query I have applied:

Select 
    orders.id, 
    orders.order_price, 
    orders.purchase_date,
    customers.email, 
    product_orders.qty, 
    products.name 
from 
    orders 
    INNER JOIN product_orders on orders.id=product_orders.orders_id 
    INNER JOIN products on product_orders.product_id=products._id 
    INNER JOIN customers on orders.customer_id =customers.id

Result of the above query:**

Id  order_price  purchase_date      email        qty         name 
3  20         12/6/2011     aa@gmail.com             1   Ipad

3  20         12/6/2011     aa@gmail.com             1   Android
1  40         10/5/2011     bb@gmail.com             2  Laser hair remover

Required Result:

remove duplicate order ID

Id  order_price  purchase_date      email        qty         name 
    3  20         12/6/2011     aa@gmail.com             1   Ipad


    1  40         10/5/2011     bb@gmail.com             2  Laser hair remover

Table 1: orders

id  customer_id order_price purchase_date
1   1                   0.20    12/6/2011
2   2                   0.20    12/6/2011
3   1                   0.20    12/6/2011
4   1                   0.20    12/6/2011
5   1                   0.20    12/7/2011
6   3                   199.00  12/7/2011
7   4                   199.00  12/7/2011
8   5                   199.00  12/7/2011
9   6                   199.00  12/7/2011
10  7                   199.00  12/7/2011

Table 2 : customers

id  email                   name
1   aa@dealboard.com.au aa
2   bb@dealboard.com.au bb
3   cc@live.com.au          cc
4   dd@acgglobal.com    dd
5   ee.heinrich@det.nsw.edu.au  ee
6   ff@optusnet.com.au  ff
7   ssy@hotmail.com         ss

table 3: products

id  name
1   A Home Portable Laser Hair Remover
2   Ipad
3   android
4   Asus
5   s
6   10 inch Android
7   A Fabric Steamer Cleaner
8   A Magnetic Fly Screen Door
9   pillopw
10  LCD

table 4: product_orders

id orders_id product_id qty
    1   1      1         1
    2   3      3         1
    3   3      4         1
    4   4      1         1
    5   4      2         1
    6   6      1         1
    7   7      1         1
    8   4      2         1
    9   4      3         1
    10  10     1         1

Please help me to extract only 1 order from “product_orders” table and the query will omit other extra rows

  • 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-12T23:21:14+00:00Added an answer on June 12, 2026 at 11:21 pm

    Add extra subquery which gets only one product per order_id

    Select 
        orders.id, 
        orders.order_price, 
        orders.purchase_date,
        customers.email, 
        product_orders.qty, 
        products.name 
    from 
        orders 
        INNER JOIN product_orders on orders.id=product_orders.orders_id 
        INNER JOIN
        (
            SELECT orders_id, MIN(Product_ID) prodID
            FROM product_orders
            GROUP BY orders_id
        ) c ON product_orders.orders_ID = c.orders_id AND
                product_orders.Product_ID = c.prodID
        INNER JOIN products on product_orders.product_id=products.id 
        INNER JOIN customers on orders.customer_id =customers.id
    

    SQLFiddle Demo

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

Sidebar

Related Questions

Please help me to write the following sql. I have a table like this,
Please help me to write an SQL query with the condition as NOT LIKE
i'm starter in linq, i have write this T-SQL Query select * from DOCUMENT_TYPES
Please help me to write a trigger that adds new rows to a table.
Please can anyone help me with this? I have 2 tables, location and tickets
I'm a newbie for SQL Server. Please help me out with my below problem.
i have write the date and time column query in Sql Server with IsNull
I need to write an sql with the below requirements.. I have a table
I need some help to write some queries. For this sql query (select *
I need to write an SQL query to take either from one of two

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.