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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:45:55+00:00 2026-06-03T20:45:55+00:00

Got this as an interview question from Amazon to test basic SQL skills and

  • 0

Got this as an interview question from Amazon to test basic SQL skills and I kind of flopped it. Consider the following tables:

Student - Stid, Stname, Details
Subject - Subid, Subname
Marks - Stid, Subid, mark

Write a query to print the list of names of students who have scored the maximum mark in each subject.

The wrong answer which I gave was:

select A.Stname from A as Student, B as 
(select Stid, Subid, max(mark) from Marks groupby Subid) where A.Stid = B.Stid

I was thinking you can have a table B in which you can get the top marks alone and match it with the names in the student table A. But turns out my “groupby” is wrong.

One more variation of the question which I felt was can be asked is that, if there is more than one student having the highest mark in a subject, even their names should be included.

Can you please help solve these queries. They seem to be simple, but I am not able to get a hang of it.

Thanks!

  • 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-03T20:45:57+00:00Added an answer on June 3, 2026 at 8:45 pm

    You just have to partition the problem into some bite-sized steps and solve each one by one

    First, get the maximum score in each subject:

    select SubjectID, max(MarkRate)
    from Mark
    group by SubjectID;
    

    Then query who are those that has SubjectID with max MarkRate:

    select SubjectID, MarkRate, StudentID
    from Mark
    where (SubjectID,MarkRate)
    in
      (
      select SubjectID, max(MarkRate)
      from Mark
      group by SubjectID
      )
    order by SubjectID, StudentID;
    

    Then obtain the Student’s name, instead of displaying just the StudentID:

    select SubjectName, MarkRate, StudentName
    from Mark
    join Student using(StudentID)
    join Subject using(SubjectID)
    where (SubjectID,MarkRate)
    in
      (
      select SubjectID, max(MarkRate)
      from Mark
      group by SubjectID
      )
    order by SubjectName, StudentName
    

    Database vendors’ artificial differences aside with regards to joining and correlating results, the basic step is the same; first, partition the problem in a bite-sized parts, and then integrate them as you solved each one of them, so it won’t be as confusing.


    Sample data:

    CREATE TABLE Student
        (StudentID int, StudentName varchar(6), Details varchar(1));    
    INSERT INTO Student
        (StudentID, StudentName, Details)
    VALUES
        (1, 'John', 'X'),
        (2, 'Paul', 'X'),
        (3, 'George', 'X'),
        (4, 'Paul', 'X');
    
    CREATE TABLE Subject
        (SubjectID varchar(1), SubjectName varchar(7));    
    INSERT INTO Subject
        (SubjectID, SubjectName)
    VALUES
        ('M', 'Math'),
        ('E', 'English'),
        ('H', 'History');
    
    CREATE TABLE Mark
        (StudentID int, SubjectID varchar(1), MarkRate int);    
    INSERT INTO Mark
        (StudentID, SubjectID, MarkRate)
    VALUES
        (1, 'M', 90),
        (1, 'E', 100),
        (2, 'M', 95),
        (2, 'E', 70),
        (3, 'E', 95),
        (3, 'H', 98),
        (4, 'H', 90),
        (4, 'E', 100);
    

    Live test here: http://www.sqlfiddle.com/#!1/08728/3


    IN tuple test is still a join by any other name:

    Convert this..

    select SubjectName, MarkRate, StudentName
    from Mark
    join Student using(StudentID)
    join Subject using(SubjectID)
    
    where (SubjectID,MarkRate)
    in
      (
      select SubjectID, max(MarkRate)
      from Mark
      group by SubjectID
      )
    
    order by SubjectName, StudentName
    

    ..to JOIN:

    select SubjectName, MarkRate, StudentName
    from Mark
    join Student using(StudentID)
    join Subject using(SubjectID)
    
    join
      (
      select SubjectID, max(MarkRate) as MarkRate
      from Mark
      group by SubjectID
      ) as x using(SubjectID,MarkRate)
    
    order by SubjectName, StudentName
    

    Contrast this code with the code immediate it. See how JOIN on independent query look like an IN construct? They almost look the same, and IN was just replaced with JOIN keyword; and the replaced IN keyword with JOIN is in fact longer, you need to alias the independent query’s column result(max(MarkRate) AS MarkRate) and also the subquery itself (as x). Anyway, this are just matter of style, I prefer IN clause, as the intent is clearer. Using JOINs merely to reflect the data relationship.

    Anyway, here’s the query that works on all databases that doesn’t support tuple test(IN):

    select sb.SubjectName, m.MarkRate, st.StudentName
    from Mark as m
    join Student as st on st.StudentID = m.StudentID
    join Subject as sb on sb.SubjectID = m.SubjectID
    
    join
      (
      select SubjectID, max(MarkRate) as MaxMarkRate
      from Mark
      group by SubjectID
      ) as x on m.SubjectID = x.SubjectID AND m.MarkRate = x.MaxMarkRate
    
    order by sb.SubjectName, st.StudentName
    

    Live test: http://www.sqlfiddle.com/#!1/08728/4

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

Sidebar

Related Questions

I got this question in an interview with amazon. I was asked to perform
I got this in an interview question -- the question was more about what
I just got this question on an interview and had no idea how to
I got this question in a previous interview and couldnt do it , any
My girlfriend got this question in an interview, and I liked it so much
I got this question in an interview and I was not able to solve
I got asked this question in a interview. I clearly know what a decorator
I got this question in Interview for post of SSE in ASP.NET. the interview
Got this question in an interview today, and its optimized solution stopped me cold
I got this question in an interview the other day and would like to

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.