# Write a SOQL query to retrieve the top 5 Accounts based on AnnualRevenue excluding null values (NULLS LAST).

**Topic:** Development · **Difficulty:** Easy · **Asked at:** Capgemini, Mindtree, Hexaware · **Tags:** soql, scenario, order-by

Read it on the site: https://www.forcechallenges.com/interview-questions/write-a-soql-query-to-retrieve-the-top-5-accounts-based-on-annualrevenue-excluding-null-values-nulls-last

## Answer

Order descending with `NULLS LAST` (or filter nulls) and limit to 5.

```soql
SELECT Id, Name, AnnualRevenue FROM Account
WHERE AnnualRevenue != null
ORDER BY AnnualRevenue DESC NULLS LAST
LIMIT 5
```

---
Source: ForceChallenges · last updated 2026-10-19
