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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:55:50+00:00 2026-06-14T03:55:50+00:00

Here is the database schema: [redacted] I’ll describe what I’m doing with the query

  • 0

Here is the database schema:

[redacted]

I’ll describe what I’m doing with the query below:

Innermost query: Select all the saleIds satisfying the WHERE conditions

Middle query: Select all the productIds that were a part of the saleId

Outermost query: SUM the products.cost and select the vendors.name.

And here is the SQL query I came up with:

SELECT vendors.name AS Company
, SUM(products.cost) AS Revenue
FROM
    products
    INNER JOIN sold_products
        ON (products.productId = sold_products.productId)
    INNER JOIN vendors
    ON (products.vendorId = vendors.vendorId)
            WHERE sold_products.productId IN (
                    SELECT sold_products.productId
                    FROM
                        sold_products
                    WHERE sold_products.saleId IN (
                            SELECT sales.saleId     
                            FROM
                                markets
                                INNER JOIN vendors
                                ON (markets.vendorId = vendors.vendorId)
                                INNER JOIN sales_campaign
                                ON (sales_campaign.marketId = markets.marketId)
                                INNER JOIN packet_headers
                                ON (sales_campaign.packetHeaderId = packet_headers.packetHeaderId)
                                INNER JOIN packet_details
                                ON (packet_details.packetHeaderId = packet_headers.packetHeaderId)
                                INNER JOIN sales
                                ON (sales.packetDetailsId = packet_details.packetDetailsId)
                            WHERE vendors.customerId=60
                            )
                    )
GROUP BY Company
ORDER BY Revenue DESC;

Any help in optimizing this?

  • 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-14T03:55:51+00:00Added an answer on June 14, 2026 at 3:55 am

    Since you are just using inner joins you normally simplify the query to smth like this:

    SELECT  ve.name     AS Company
    ,       SUM(pr.cost) AS Revenue
    FROM    products        pr
    ,       sold_products   sp
    ,       vendors         ve
    ,       markets         ma
    ,       sales_campaign  sc
    ,       packet_headers  ph
    ,       packet_details  pd
    ,       sales           sa
    Where pr.productId = sp.productId
    And   pr.vendorId  = ve.vendorId
    And   ve.vendorId  = ma.vendorId
    And   sc.marketId = ma.marketId
    And   sc.packetHeaderId = ph.packetHeaderId
    And   pd.packetHeaderId = ph.packetHeaderId)
    And   sa.packetDetailsId = pd.packetDetailsId
    And   ve.customerId = 60
    GROUP BY ve.Company
    ORDER BY pr.Revenue DESC;
    

    Please try if this works and if it is faster and let me know.

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

Sidebar

Related Questions

Here is the schema of the database I'm working with - [redacted] I'm trying
Just getting into SQL stored queries right now... anyway, here's my database schema (simplified
The Media Wiki database Schema, here , has taught me a lot. Cannot-understand: In
Here is my current setup: Database Role - MyDbRole Schema - MySchema User -
I have the following database schema: Here's how I populate a DropDownList in my
Here's my database schema: create table Post ( PostId int primary key identity, ImageUrl
here's a more general question on how you handle database schema changes in a
Here is the most relevant part of my database schema: create table TEST (
Here is my database schema for my MySQL database: create table noticia ( id
Below I provide my database schema, the database data currently inserted (which is only

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.