Book Image

Getting Started with SOQL

By : Magulan D
Book Image

Getting Started with SOQL

By: Magulan D

Overview of this book

<p>This practical guide will tell you everything you need to know about SOQL statements. You will learn the optimum way to write complex SOQL statements with this easy-to-understand guide. Beginning with basic SOQL statements, you will progress quickly onto more advanced statements, such as how to filter multiselect picklist values to escape sequences.</p> <p>This book will teach you how to sort records with more than one field, sorting with more than one field–one in ascending order and another field in descending order—and sorting null records in the first or in the last. You will learn about all the features provided while you are writing SOQL statements. This book will make you a SOQL expert by teaching you how to write SOQL statements in an optimized and effective way.</p>
Table of Contents (14 chapters)
Getting Started with SOQL
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using the HAVING clause


The HAVING clause is very similar to the WHERE clause. However, the only difference between the HAVING and WHERE clause is that the HAVING clause is used only with the aggregate functions.

The HAVING clause is used to specify the search condition in the GROUP BY clause or the aggregate functions. The HAVING clause limits the grouped records returned by a SOQL statement. However, the WHERE clause limits the records returned by a SOQL statement.

A HAVING clause in SOQL is used to specify that the SOQL SELECT statement should only return the records whose aggregate values meet the specified conditions.

A sample query is given as follows:

SELECT City__c, COUNT(Employee_Name__c) FROM Employee__c GROUP BY City__c HAVING COUNT(City__c) >= 1

The following screenshot shows us the output of the preceding query execution:

The output of the SOQL execution shows us the number of employees in each and every city whose number of records in each and every city is greater than one...