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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:51:14+00:00 2026-06-09T23:51:14+00:00

If I have a table with columns pid, sid as primary key, and price

  • 0

If I have a table with columns pid, sid as primary key, and price and color.

How do I create a SQL query that can fetch the cheapest price of each pid (product ID) which has color equal to green?

For example I have rows :

(1, 1, 10, "green")
(1, 2, 8, "green")
(1, 3, 11, "green")
(2, 1, 7, "green")
(2, 2, 15, "green")
(2, 3, 22, "green")
(3, 1, 10, "red")
(1, 2, 8, "red")
(1, 3, 11, "red")

Then I will receive : (1, 2, 8)->pid=1 and (2, 1, 7)->pid=2. No pid=3 because its color is red.

Thanks in advance.

  • 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-09T23:51:16+00:00Added an answer on June 9, 2026 at 11:51 pm
    SELECT pid, min(Price)
    from table
    Where Color='green'
    group by pid
    

    Or if you want the whole row (and your sql platform supports ranking)…

    select * 
    from
    (
     select *, ROW_NUMBER() over (partition by pid order by price) rn
     from #t
     where color='green'
    ) v
    where rn=1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have mysql table tmp with columns pid,city,state,country. I write queries so i can
I have a table with columns: s_Id (primary int), sup_type (int), sup_date (datetime), sup_req
I have the following tables (removed columns that aren't used for my examples): CREATE
Let's asume I have a parent-child structure setup in SQL (server 2005): CREATE TABLE
I have a table: Person with columns: pID(PK) FName LName plID(FK) Another table Place
I have a table (TABLE1) which has 4 columns: UID | PID | VALUE1
The table columns have the data type BLOB and CLOB. What are the corresponding
I have table with two columns (varchar from, varchar to). This table represents connections
I have table users with columns id, username, email and password. And i have
I have table with 3 columns A B C. I want to select *

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.