Is there a way to access a related module's fields in a formula field?

Is there a way to access a related module's fields in a formula field?

Let's say you have a 'Student" module and a "Classes" module. Within the Classes module, there is a "name" field and also an "grade" field.

In the "Student" module, is it possible to create a formula field labeled "Math Grade" that retrieves classes named "Math" and returns the "grade" field from the first matching record?

I understand that subfields can display this information, in this case I would like to denormalize the database for a couple of relations in each record.

Is this possible using custom functions? Is there an SQL query function where one can cache the results of a custom query in a record?