Home > Net >  SELECTING * From a Table But Only Taking One Row with Highest Value
SELECTING * From a Table But Only Taking One Row with Highest Value

Time:11-03

Suppose I have a table with the following schema

Student

Name SchoolFees
James $23
James $21
Paul $36
Tim $13

I am trying to do a SELECT * from this table, but the catch is that I only want to retrieve one row per student, where it's SchoolFees is the highest. So for example, my query should return

Name SchoolFees
James $23
Paul $36
Tim $13

How do I go about constructing this query? I am using MariaDB

CodePudding user response:

If all you want is name and the max of fees, a simple group by is simplest:

select name, max(school_fees) from student group by name;

However, if you want the record with the max of school_fees and any other fields as well a "window function" may be better.

CodePudding user response:

You can try this

select Name, max(SchoolFees) as SchoolFees from  [Table_Name]
group by Name
  • Related