Consider I'm trying to represent a simple query at this:
SELECT student.name, MAX(grades.final_grade) as max_grade
From grades , students
where student.id == grade.student_id and max_grade == 90
My question is focues on syntax and how to represent the same raw sql query.
In sqlalchemy orm it may look like this:
Student
and Grade
representing tables as orm objects.
query= session.query(Student.name , func.max(Grade.final_grade).join(Grade, Student.id == Grade.student_id).filter(text("max_grade = 90")).all()
But in this case I get excetion:
{Operationalerror}{_mysql_exceptions.Operationalerror)(1054, "Unknown column 'max_grade' in where clause"
Any idea how to solve this, why isn't my label recognized?
Text
construct usage manual reference is here
Edit: question about raw sql, since we got a feeling that something isn't right.
My original sql query was:
SELECT student.name, MAX(grades.grade) as grade
From grades , students
where student.id = grade.student_id and grade = 90
In the where
clause grade=90
was referring to grade
from table grades
, meaning grades.grade =90
and in no way to MAX(grades.grade)=90
?
So query worked because grades was referring to table column rather than label in select clause?
Thank you