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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:31:51+00:00 2026-05-18T02:31:51+00:00

table 1 id name class 1 ab A 2 cd A 3 ef B

  • 0

table 1

id    name   class
1     ab     A
2     cd     A
3     ef     B    
4     ab     B
5     cd     B

table 2

name   test   marks
ab     1      90
ab     2      70
cd     2      80
cd     3      85
ef     3      85
ef     4      60

Hi, I have 2 tables above, my question is what is the most efficient/best or simplest way to get the highest marks from table 2 for each person and join to table 1 such that returns:

id   name   class    [highest marks]
1    ab     A        90
2    cd     A        85
3    ef     B        85
  • 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-18T02:31:52+00:00Added an answer on May 18, 2026 at 2:31 am

    Assuming SQL Server 2005+, using analytic/ranking/windowing functionality:

    WITH example AS (
      SELECT a.id,
             a.name,
             a.class,
             b.marks,
             ROW_NUMBER() OVER(PARTITION BY a.id
                                   ORDER BY b.marks DESC) AS rank
        FROM TABLE_1 a
        JOIN TABLE_2 b ON b.name = a.name)
    SELECT e.id,
           e.name,
           e.class,
           e.marks
      FROM example e
     WHERE e.rank = 1
    

    Using aggregates:

    SELECT a.id,
           a.name,
           a.class,
           b.marks      
      FROM TABLE_1 a
      JOIN (SELECT t.name,
                   MAX(t.mark) AS max_mark
              FROM TABLE_2
          GROUP BY t.name) b ON b.name = a.name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this class called Table: class Table { public string Name { get
i have domain classes: package test class Credit { String name; static hasMany =
I have a script that takes a table name and generates a control file
I have the following code: ListBox.DataSource = DataSet.Tables(table_name).Select(some_criteria = match) ListBox.DisplayMember = name The
I have below code, running on 32-bit windows, visual-studio. template <class T> class Test
Why can't I pass the table name to a prepared PDO statement? $stmt =
Do you prefix each field in a table with abbreviated table name? Example: Table:
For example, mysql quote table name using SELECT * FROM `table_name`; notice the `
The SQL Server (2000/2005) function gets the table name and the field name as
I have two tables, users and images which need to be joined. there is

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.