The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+5 votes

Consider a relation examinee (regno, name, score), where regno is the primary key to score is a real number.

Write an SQL query to list the regno of examinees who have a score greater than the average score.

asked in Databases by Veteran (97.7k points) | 338 views

2 Answers

+6 votes
Best answer

There are many ways to write a query, all of which will perform the same task. One way is:

SELECT regno
FROM examinee
WHERE score > (SELECT AVG(score)
               FROM examinee )

Here, the inner query is returning the average of the scores. And outer query selects those regno that have a score greater than this average.

answered by Boss (16.5k points)
selected by
What are some other ways of writing it?
–1 vote
SELECT regno
FROM examinee
HAVING score > avg(score)
answered by (89 points) 1 flag:
✌ Edit necessary (MRINMOY_HALDER “Query is incorrect grammatically”)
As far as I know Having keyword can only be used with aggregate functions (like AVG,COUNT,MIN,MAX). So this is not correct.
SELECT regno
FROM examinee
GROUP BY score
HAVING score > avg(score)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
49,793 questions
54,514 answers
75,179 users