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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:05:28+00:00 2026-06-09T14:05:28+00:00

I wrote the query below, and it returns the data I need. However, I

  • 0

I wrote the query below, and it returns the data I need. However, I can’t help but think there’s a better way to write it. What I need is to find all the unique rows in Table1 that have prices greater than the price a specified vendor has in the categories to which the vendor belong. (In theory, there is no limit to the number of categories to which a vendor can belong).

  • Table1 is a cross-reference table containing VendorID, Category, and price
  • Table2 contains vendor information
  • There is a category table that contains the category names.

…

SELECT DISTINCT Table2.name, Table2.city, Table2.state
FROM Table1 INNER JOIN Table2 ON Table1.VendorID = Table2.VendorID
WHERE Table1.Category IN (49,50,45)
    AND Table1.price > (SELECT price FROM Table1 WHERE VendorID = 69041 AND Category = 50)
    AND Table1.price > (SELECT price FROM Table1 WHERE VendorID = 69041 AND Category = 49)
    AND Table1.price > (SELECT price FROM Table1 WHERE VendorID = 69041 AND Category = 45)
  • 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-09T14:05:29+00:00Added an answer on June 9, 2026 at 2:05 pm

    You could use a Common Table Expression or a subquery to pull in all the data you need first and then join your tables to that “view”:

    ;WITH MaxPricePerCategory
    AS
        (
        SELECT MAX(Price) AS [Price]
            -- You can remove category if you just need the maximum price
            , Category
        FROM Table1
        WHERE VendorID = 69041
        AND Table1.Category IN (49,50,45)
        GROUP BY Category
        )
    SELECT DISTINCT Table2.name
        , Table2.city
        , Table2.state
    FROM Table1 
    JOIN Table2 
        ON Table1.VendorID = Table2.VendorID
    JOIN MaxPricePerCategory MPPC
        ON Table1.Category = MPPC.Category
            AND Table1.price > MPPC.Price
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a sql query like the example below, however, I
I need to output the following query as csv. I can easily write php
I am doing project in cakephp . I want to write below query in
I use the below code to write code to query a web method in
I wrote the below code to retrieve data from the data base, in that
I have to write a query which returns the price of books greater than
how can i write a select query by combining IN and LIKE i have
Intro I have a complex query I need to write (for an Oracle DB).
I'm newbie in PHP. I wrote the below-given code to create the array data.
I'm trying to generate a data set, and I think recursion is required, but

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.