Average sales cycle

dates

Average sales cycle

Salesforce SQL Interview Question

Salesforce's revenue team wants to know how long each sales rep takes to win a deal, measured from the day an opportunity is created to the day it closes.

For each sales_rep, find the average number of days it took to close the deals they won, as avg_days_to_close, rounded to 1 decimal place. Sort the rows by avg_days_to_close, fastest first.

Asked of

  • Data Analyst
  • Business Analyst
  • BI Analyst

opportunitiesTable12 rows

Column NameType
opportunity_idBIGINT
account_nameVARCHAR
sales_repVARCHAR
stageVARCHAR
amountBIGINT
created_dateDATE
close_dateDATE

opportunitiesExample Input

opportunity_idaccount_namesales_repstageamountcreated_dateclose_date
1Acme CorpPriya NairClosed Won480002024-01-082024-02-19
2GlobexPriya NairClosed Lost220002024-01-152024-03-01
3InitechMarcus HillClosed Won150002024-01-202024-02-10
4Umbrella HealthMarcus HillClosed Won670002024-02-012024-04-12
5Stark LogisticsDana BrooksClosed Lost310002024-02-052024-03-18
6Wayne RetailDana BrooksClosed Won540002024-02-112024-03-22
7HooliPriya NairClosed Lost390002024-02-202024-03-30
8Vandelay ImportsMarcus HillClosed Lost120002024-03-012024-03-25

Example Output

sales_repavg_days_to_close
Dana Brooks40
Priya Nair42
Marcus Hill46

Explanation

Marcus Hill won two deals in the example. Initech took 21 days, from January 20 to February 10, and Umbrella Health took 71 days, so the average is 46.0. The lost Vandelay Imports deal is not counted. Dana Brooks won one deal in 40 days and is listed first.

The example above is a small slice of the data. Your query runs against the full tables.