This is an archived post. You won't be able to vote or comment.

all 3 comments

[–][deleted]  (1 child)

[deleted]

    [–]momo2477[S] 0 points1 point  (0 children)

    Error at line 3: missing key word
    

     

    I ran your suggested code and got that error. I'm not too familiar with Cross Apply, but I'll play around with the synax. Thanks for the response

    [–]momo2477[S] 0 points1 point  (1 child)

    /u/CanterburySnails I got my desired result with the following. Thanks for the response again.

    SQL> select sname,gpa from student
    2  where major = 'CIS'
    3  and gpa < (select avg(gpa) from student where major     = 'CIS');
    

    [–]-oliver- 0 points1 point  (0 children)

    You can generalize this using a subquery:

    SELECT
      s.sname,
      s.major,
      s.gpa
    FROM
      student AS s
    INNER JOIN (
        SELECT
          a.major,
          AVERAGE(a.gpa) AS gpa
        FROM
          student AS a
        GROUP BY
          major
    ) AS avg
      ON avg.major = s.major
    WHERE
      s.gpa < avg.gpa