0

I have a student table and address table. I want to find out what percentage of them come from London. I have a query that looks like

SELECT (SELECT COUNT(*) FROM student
INNER JOIN address on student.address_id = address.id 
WHERE city = 'Lodon')/ COUNT(*)  FROM student

When I run this query I get an error

SELECT (SELECT COUNT(*) FROM student ERROR at line 1: ORA-00937: not a single-group group function

Why is this happening and how can I fix it? When I run the subquery separately it returns a single value, but why does it return multiple values when I put in a subquery? Also, if there is a cleaner query please suggest it.

1 Answer 1

1

Use conditional aggregation:

SELECT AVG( CASE WHEN a.city = 'London' THEN 1.0 ELSE 0 END)
FROM student s JOIN
     address a
     ON a.address_id = s.id ;

Your query returns an error because subqueries with aggregation queries are tricky.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.