Hi Saddam,
You can use the GROUP BY option in a SOQL query to avoid iterating through individual query results. That is, you specify a group of records instead of processing many individual records.
You can use GROUP BY with aggregate functions, such as SUM() or MAX(), to summarize the data and enable you to roll up query results rather than having to process the individual records in your code.
The following query returns the distinct set of values stored in the LeadSource field.
SELECT LeadSource
FROM Lead
GROUP BY LeadSource