When executing SQL queries, it is important to understand the order of operations so you can produce the results you expect. Below are two examples of the same query, but one uses grouping to achieve the intended result.
When working with demographics database, you want to find all the cities in the Pacific time zone that either have at least 500,000 people or are state capitals. If you were to run the query in the image as is, it would first evaluate whether the city is in the Pacific time zone and greater than 500,000 people. Then it would evaluate whether a city is a state capital and return the cities that meet either criteria. This is because And has a higher operator precedence than Or. As a result, the And operator will be evaluated before the Or operator.
If you group clauses, you can control the order of operations. All items in a group are evaluated before items outside of the group. You can hold the Shift key to select clauses you want to group together, right-click the group, or use Group Clause on the window.
With grouping applied, this query will first evaluate whether a city has at least 500,000 people or is a state capital. It will then evaluate whether a city is in the Pacific time zone and return the cities that meet both criteria. Grouping clauses is equivalent to enclosing them in parentheses. In SQL edit mode, this query would be SUB_REGION = 'Pacific' And (POP1990 > 500000 Or CAPITAL = 'Y').