# Large-Deal Lead Source Analysis

**Type:** SOQL · **Difficulty:** Hard · **Topics:** COUNT_DISTINCT, AVG, HAVING

Practice this problem interactively (write and run your SOQL against realistic data): https://www.forcechallenges.com/problems/large-deal-lead-source-analysis

## Problem

**Scenario**
Marketing wants to know which lead sources produce the biggest paying customers. Small deals and one-off sources would distort the picture, so only meaningful volumes of large won deals should be compared.

**Task**
Write one SOQL query on `Opportunity` that:
1. Considers only **won** Opportunities (`IsWon = true`) with an `Amount` **greater than 100,000**.
2. Groups them by `LeadSource`.
3. Keeps only lead sources with **at least 3** such deals.
4. Sorts by the average deal amount, **highest first**. Break ties by `LeadSource` ascending.

**Expected output**
One row per lead source with four columns: `LeadSource`, `deals` (number of qualifying deals), `accts` (number of distinct Accounts behind those deals) and `avgAmt` (average `Amount`).

**Example**
Qualifying won deals: Web 300,000 (Acme), Web 500,000 (Acme), Web 700,000 (Globex), Partner 200,000 (Acme), Partner 400,000 (Initech), Trade Show 900,000 (Acme).
- Web has 3 deals across 2 Accounts with an average of 500,000, so it is returned.
- Partner has only 2 deals, so it is dropped by the volume rule.
- Trade Show has only 1 deal, so it is dropped.

Result: Web / deals 3 / accts 2 / avgAmt 500,000.

## Constraints

Use one aggregate query on Opportunity: WHERE for the won and amount filters, GROUP BY LeadSource, HAVING on the deal count, COUNT_DISTINCT for accounts and AVG for the average. Alias the columns exactly as `deals`, `accts` and `avgAmt`. Order by average amount descending, then LeadSource.

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