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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:05:47+00:00 2026-06-01T00:05:47+00:00

I have a database table named ‘student’ in which there is one column named

  • 0

I have a database table named ‘student’ in which there is one column named ‘marks’. I want the student record with highest marks in Maths. There is a simple solution to it using order_by()[0]:

Student.objects.filter(subject='Maths').order_by('-marks')[0]

But this sorts the table and then fetches me the first record. If my table is huge, this is redundant as I need only the max record. Is there any way to just get the largest value without sorting?

I want the whole object, not just the max value.

Thanks Anuj

  • 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-01T00:05:48+00:00Added an answer on June 1, 2026 at 12:05 am

    The SQL required would be something like this:

    SELECT *
    FROM STUDENT
    WHERE marks = (SELECT MAX(marks) FROM STUDENT)
    

    To do this via Django, you can use the aggregation API.

    max_marks = Student.objects.filter(
        subject='Maths'
    ).aggregate(maxmarks=Max('marks'))['maxmarks']
    Student.objects.filter(subject='Maths', marks=max_marks)
    

    Unfortunately, this query is actually two queries. The max mark aggregation is executed, the result pulled into python, then passed to the second query. There’s (surprisingly) no way to pass a queryset that’s just an aggregation without a grouping, even though it should be possible to do. I’m going to open a ticket to see how that might be fixed.

    Edit:

    It is possible to do this with a single query, but it’s not very obvious. I haven’t seen this method elsewhere.

    from django.db.models import Value
    
    max_marks = (
        Student.objects
               .filter(subject='Maths')
               .annotate(common=Value(1))
               .values('common')
               .annotate(max_marks=Max('marks'))
               .values('max_marks')
    )
    
    Student.objects.filter(subject='Maths', marks=max_marks)
    

    If you print this query in the shell you get:

    SELECT 
           "scratch_student"."id", 
           "scratch_student"."name", 
           "scratch_student"."subject", 
           "scratch_student"."marks" 
      FROM "scratch_student" 
     WHERE ( 
           "scratch_student"."subject" = Maths 
       AND "scratch_student"."marks" = (
           SELECT 
                  MAX(U0."marks") AS "max_marks" 
             FROM "scratch_student" U0 
            WHERE U0."subject" = Maths))
    

    Tested on Django 1.11 (currently in alpha). This works by grouping the annotation by the constant 1, which every row will group into. We then strip this grouping column from the select list (the second values(). Django (now) knows enough to determine that the grouping is redundant, and eliminates it. Leaving a single query with the exact SQL we needed.

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

Sidebar

Related Questions

I have one field named type in a database table. I want this field
I have a database table named call with columns call_time, location, emergency_type and there
I have a database table (named Topics) which includes these fields : topicId name
In ms-access database i have a table named tableA this table has a column
I have a database table that contains a column named type. For every row
I have a database created with table name Accounts There are THREE columns. One
I Have a DataBase in my project With Table named 'ProcessData' and columns named
I have a Database (SQL Server) with table named 'ProcessData' and columns named 'Process_Name'
I have a table in my Postgres database with columns named type, desc, and
We have a database where every table name starts with WW. I want 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.