'How do I ignore if case condition is not met in SQL?

I have the following query:

SELECT 
    CASE WHEN ROW_NUMBER() OVER (ORDER BY COUNT(*) desc) <=10 THEN name ELSE, --ignore-- END AS name
    COUNT(*) as COUNT
FROM person
GORUP BY name

So I'm simply trying to get top 10 occurencies of a person in the list. But I only want the top 5, are there any ways to make not select lines that falls outside top 5? I know I can make a nested select, but I want to know if it's possible to replace the --ignore-- with something that ignores?



Solution 1:[1]

Just use the upper limit of 5

SELECT 
    CASE WHEN ROW_NUMBER() OVER (ORDER BY COUNT(*) desc) <=10 THEN name ELSE '' END AS name
FROM ...
LIMIT 5

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dean Van Greunen