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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:59:42+00:00 2026-05-31T19:59:42+00:00

I asked a question about making a greatest n per group type query yesterday

  • 0

I asked a question about making a “greatest n per group” type query yesterday (at the time not knowing it was called “greatest n per group”) except I asked how to get the least per group. The table structure was as follows:

type    name    value
=====================
1       item1    1
1       item2    20
2       item3    0
3       item4    5
3       item5    2
3       item6    50

I received several great answers, and the most helpful one was this:

SELECT t1.type, t1.name, t1.value
FROM mytable t1
LEFT JOIN mytable t2 ON t1.type = t2.type AND t1.value > t2.value
WHERE t2.value IS NULL

The above query results in this:

type    name    value
=====================
2       item3    0
1       item1    1
3       item5    2

However, since asking the question, I realized that I left out an important requirement, one which I can’t seem to figure out how to add to the above query. I need to add a conditional statement that, instead of selecting the row with the lowest value for a column per group, selects the row with the lowest value for a column per group but where that row has another column with a value greater than some minimum value.

Here is my new question/problem:


I have the following table (products):

+-----------------------------------------------------------+
|   id   |   type   |   name   |   popularity   |   price   |
+-----------------------------------------------------------+
|    0   |    0     |   item1  |      3.5       |   0.99    |
|    3   |    1     |   item2  |      3         |   1.99    |
|    4   |    1     |   item3  |      6         |   2.95    |
|    6   |    1     |   item4  |      9         |   2.50    |
|    9   |    1     |   item5  |      12        |   3.75    |
|    12  |    2     |   item6  |      16        |   5.25    |
|    13  |    2     |   item7  |      32        |   10.95   |
|    14  |    2     |   item8  |      48        |   7.50    |
+-----------------------------------------------------------+

I need to get the lowest priced item in each group (the groups being the different values of type) with the popularity being greater than some amount (if no items in the group have popularity greater than the specified amount, then no items from that group should be returned). The last part of that sentence is how this question differs from my last question. Preferably the results should be returned sorted by the price in ascending order.

So, say that the popularity needs to be greater than 3, then the result should be:

+-----------------------------------------------------------+
|   id   |   type   |   name   |   popularity   |   price   |
+-----------------------------------------------------------+
|    0   |    0     |   item1  |      3.5       |   0.99    |
|    6   |    1     |   item4  |      9         |   2.50    |
|    12  |    2     |   item6  |      16        |   5.25    |
+-----------------------------------------------------------+

If the popularity needs to be greater than 6, then the result should be:

+-----------------------------------------------------------+
|   id   |   type   |   name   |   popularity   |   price   |
+-----------------------------------------------------------+
|    6   |    1     |   item4  |      9         |   2.50    |
|    12  |    2     |   item6  |      16        |   5.25    |
+-----------------------------------------------------------+

Hopefully I did both of those examples correctly. In any case, I think you get the idea.

Would it be possible to do what I’m asking in one query?

  • 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-31T19:59:43+00:00Added an answer on May 31, 2026 at 7:59 pm

    This requires some attention where to put the condition (to where or to on?) so that you don’t get tricked 🙂 You need to add the condition for t1 to where clause and for t2 to on clause:

    SELECT t1.type, t1.name, t1.value
    FROM mytable t1
    LEFT JOIN mytable t2 ON t1.type = t2.type AND t1.value > t2.value 
        AND t2.popularity > 3 /* here */
    WHERE t2.value IS NULL 
        AND t1.popularity > 3 /* and here */
    

    Haven’t tested it, but it should work.

    Attempt for an explanation: the condition in where clause affects which elements you consider as potential elements with lowest value. Whereas the condition in on clause affects the linkage: to what other elements you want to compare it? It defines the group within which you compare. Technically, it has an impact on when t2.* will be NULL. Had you given the condition on t2.popularity to where clause instead, you would not receive any NULLs (i.e. not find elements w/lowest value) for groups where the lowest elements have low popularity.

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

Sidebar

Related Questions

I've asked a very similar question before, but this time is not about VB
I asked a question about different testing frameworks yesterday. This question can be found
I recently asked a question about what I called method calls. The answer referred
I asked a question about C-type sizes which I get a pretty good answer
I asked a question yesterday about namespaces that got answered, and so I tarried
I asked a question about creating temporary/ virtual ids for query results, mysql &
Just asked a question about linking Boost libraries in the make file. Thanks to
I asked a question about building custom Thread Safe Generic List now I am
I asked a question about Garbage Collection in Java in this topic . But
I recently asked a question about IIf vs. If and found out that there

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.