I’m having these schema
STUDENT (Sid, Name, City, Adm_date, Paper1, Paper2)
And I want to find the maximum number of students were admitted in which date and the city where maximum number of students lives.
I’m using these queries in ORACLE 11g Express Edition:
For City:
SELECT COUNT(Sid) AS No_of_students, City
FROM STUDENT
GROUP BY City
ORDER BY No_of_students DESC
LIMIT 1;
And For Date:
SELECT COUNT(Sid) AS No_of_students, Adm_date
FROM STUDENT
GROUP BY Adm_date
ORDER BY No_of_students DESC
LIMIT 1;
But here I’m getting error for both these tables:
ORA-00933: SQL command not properly ended
Here is my Table that I’m Using
>Solution :
Please try rownum with subquery (Since Limit is not available in Oracle):
For city wise:
select * from
(SELECT COUNT(Sid) AS No_of_students, City
FROM STUDENT
GROUP BY City
ORDER BY No_of_students DESC) where rownum=1
For Adm_date wise:
select * from
(SELECT COUNT(Sid) AS No_of_students, Adm_date
FROM STUDENT
GROUP BY Adm_date
ORDER BY No_of_students DESC) where rownum=1
Found a great article explaining the mechanism of selecting top-N rows using rownum:
https://blogs.oracle.com/oraclemagazine/post/on-rownum-and-limiting-results