ApexSensei

Drills SOQL queries

Rank practice Accounts

ORDER BY sets the result order and LIMIT caps the row count.

Your task

Sort this run's four Accounts by Name from Z to A, and keep only the top two.

String tag = 'AS-Q303-' + UserInfo.getUserId() + '-' +
    String.valueOf(Datetime.now().getTime());
String tagPrefix = tag + '%';
List<Account> seeded = new List<Account>{
    new Account(Name = tag + '-A'),
    new Account(Name = tag + '-B'),
    new Account(Name = tag + '-C'),
    new Account(Name = tag + '-D')
};
insert seeded;

List<Account> topRows = new List<Account>();
System.debug('Top count: ' + topRows.size());
System.debug('Top first: false');
System.debug('Top second: false');
System.debug('Top B left out: false');

Requirements

  1. Top count: 2 should be built from the query result
  2. ORDER BY Name DESC should be present
  3. LIMIT 2 should be present
  4. Top count: 2 should be printed
  5. Top first: true should be printed
  6. Top second: true should be printed
  7. Top B left out: true should be printed

Sign in to open this drill