AND as useful as all that is, we can really start to pile it on when we introduce our first logical operator AND
.
When we use the WHERE clause to filter results, a single condition may not be sufficient for our needs. By using the AND
logical operator, we can combine multiple conditions that all need to be true for a record to be included in the results. For example, consider a scenario where we want to find Opportunities that are both high-value and currently in the negotiation stage. Here's how we can do that:
SELECT Name, Amount, StageName FROM Opportunity WHERE Amount > 100000 AND StageName = 'Negotiation/Review'
Name | Amount | StageName |
---|---|---|
United Oil Office Portable Generators | 125000 | Negotiation/Review |
United Oil Installations | 270000 | Negotiation/Review |
Suppose we want to filter Opportunities to find those that are not new customers, have a probability of winning higher than 50%, and are not yet closed. We would use the AND
logical operator to ensure all these conditions are met simultaneously. Here’s how that query might look:
SELECT Name, Type, Probability, IsClosed FROM Opportunity WHERE Type != 'New Customer' AND Probability > 50 AND IsClosed = false
Name | Type | Probability | IsClosed |
---|---|---|---|
United Oil Office Portable Generators | Existing Customer - Upgrade | 90 | FALSE |
Grand Hotels Kitchen Generator | Existing Customer - Upgrade | 60 | FALSE |
United Oil Refinery Generators | Existing Customer - Upgrade | 75 | FALSE |
... | ... | ... | ... |
Lastly, while tempting, we don't necessarily need to show these columns in the SELECT
output for which we have specified the condition for. We always know what the value will be in some cases.
SELECT Name, ExpectedRevenue FROM Opportunity WHERE StageName = 'Negotiation/Review' AND Type = 'Existing Customer - Upgrade' AND ExpectedRevenue > 100000
Name | ExpectedRevenue |
---|---|
United Oil Office Portable Generators | 112500 |
United Oil Installations | 243000 |