I have a table named Student as followed:
CREATE TABLE "STUDENT"
( "ID" NUMBER(*,0),
"NAME" VARCHAR2(20),
"AGE" NUMBER(*,0),
"CITY" VARCHAR2(20),
PRIMARY KEY ("ID") ENABLE
)
I am trying to get all the records of the students having a larger age than the average age. This is what I tried:
SELECT *
FROM student
WHERE age > AVG(age)
and
SELECT *
FROM student
HAVING age > AVG(age)
Both ways did not work!
If you going to use an aggregation without a group by you can’t reference other fields. (You are with *)
However you can make a subquery that does.
This is easy to write and understand. However if you use analytic functions you can get better performance as Justin Cave explains in his answer