function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anoop Kumar 31Anoop Kumar 31 

Select id,Min(age__c) from Contact GROUP BY Name

I have multiple contacts with their different ages. I want to fetch out minimum and max age among all contacts. 
I tried this query....Select id,Min(age__c) from Contact GROUP BY Name.
please correct me
Best Answer chosen by Anoop Kumar 31
v varaprasadv varaprasad
Hi Anoop,

Try this:

Ex : 
select min(amount),max(amount),Sum(amount),Avg(amount) from opportunity

AggregateResult agr = [select min(amount),max(amount),Sum(amount),Avg(amount) from opportunity];
system.debug('==agr =='+agr);

Select Min(age__c),Max(age__c) from Contact

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For SFDC Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1


 

All Answers

Steven NsubugaSteven Nsubuga
Using MIN and MAX functions, together with GROUP BY is best if you are returning more than 1 row of data, for example, return the youngest Contact in each Department.
List<AggregateResult> ars = [SELECT Department, MIN(Age__c) FROM Contact GROUP BY Department];
This returns 1 row for each department, showing the youngest contact for each department.
 
Raj VakatiRaj Vakati
try this 
 
List<AggregateResult> ars = [SELECT Department, MIN(Age__c),Max(Age__c) FROM Contact GROUP BY Department];

 
v varaprasadv varaprasad
Hi Anoop,

Try this:

Ex : 
select min(amount),max(amount),Sum(amount),Avg(amount) from opportunity

AggregateResult agr = [select min(amount),max(amount),Sum(amount),Avg(amount) from opportunity];
system.debug('==agr =='+agr);

Select Min(age__c),Max(age__c) from Contact

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.

Thanks
Varaprasad
@For SFDC Support: varaprasad4sfdc@gmail.com
Blog: http://salesforceprasad.blogspot.com/

Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1


 
This was selected as the best answer
Anoop Kumar 31Anoop Kumar 31
v varaprasad Thanks, I was looking for this.