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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:47:00+00:00 2026-05-11T19:47:00+00:00

Every once and a while I have a scenario like this, and can never

  • 0

Every once and a while I have a scenario like this, and can never come up with the most efficient query to pull in the information:

Let’s say we have a table with three columns (A int, B int, C int). My query needs to answer a question like this: “Tell me what the value of column C is for the largest value of column B where A = 5.” A real world scenario for something like this would be ‘A’ is your users, ‘B’ is the date something happened, and ‘C’ is the value, where you want the most recent entry for a specific user.

I always end up with a query like this:

SELECT
    C
FROM
    MyTable
WHERE
    A = 5
    AND B = (SELECT MAX(B) FROM MyTable WHERE A = 5)

What am I missing to do this in a single query (opposed to nesting them)? Some sort of ‘Having’ clause?

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

    BoSchatzberg’s answer works when you only care about the 1 result where A=5. But I suspect this question is the result of a more general case. What if you want to list the top record for each distinct value of A?

    SELECT t1.*
    FROM MyTable t1
       INNER JOIN 
         (
           SELECT A, MAX(B)
           FROM MyTable
           GROUP BY A
         )  t2 ON t1.A = t2.A AND t1.B = t2.B
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Every once in a while, typically when I stop debugging in our UI assembly,
I have a multithreaded .NET Windows Service that hangs intermittently -- maybe once every
I have a Python program that uses the threading module. Once every second, my
I have a method that periodically (e.g. once in every 10 secs) try to
Running a rails site right now using SQLite3. About once every 500 requests or
Everyone has accidentally forgotten the WHERE clause on a DELETE query and blasted some
Everyone has this huge massively parallelized supercomputer on their desktop in the form of
Everyone remembers google browser sync right? I thought it was great. Unfortunately Google decided
Everyone I work with is obsessed with the data-centric approach to enterprise development and
Everyone (at least everyone who uses a compiled language) has faced compilation errors 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.