S. Rain
S. Rain

Reputation: 25

Calculating the difference between max and min in different table

Table major

Table student

How do I calculate the age difference between the oldest and the youngest Computer Sciences major?

In this the case, should I use INNER JOIN or OUTER JOIN?

Upvotes: 2

Views: 3943

Answers (1)

Jacobm001
Jacobm001

Reputation: 4549

This can be computed easily using a combination of the min() and max() aggregate functions.

... should I use INNER JOIN or OUTER JOIN

It doesn't make sense to keep any records that don't have a matching row in the other table. That means that an inner join would make the most sense here.


select
    m.mname
    , max(s.age) - min(s.age) as age_difference
from
    student s
    join major m
        on s.sid = m.sid
where
    m.mname = 'Computer Sciences'
group by
    m.mname

Upvotes: 1

Related Questions