Subqueries are usually preceded by a conditional WHERE clause, which can contain any of the following comparison and logical operators:
<> values are unequal <= value on left is less than or equal to value on right >= value on left is greater than or equal to value on right < value on left is less than value on right > value on left is greater than value on right BETWEEN value on left lies between values on right NOT logical NOT AND logical AND OR logical OR In order to demonstrate, let's say I wanted a list of all those customers with exactly two branch offices. First, I need to figure out a way to obtain the number of branch offices per customer,
and then filter out those with just two offices with a HAVING clause,
and then hand the client ID over to the "clients" table in order to get the client name.
The following subquery will take care of the three steps above for me:
In this case, the inner query is executed first - this query takes care of grouping the branches by customer ID and counting the number of records (branch offices) in each group. Those customers which have exactly two branch offices can easily filtered out with a HAVING clause, and the corresponding customer IDs returned to the main query, which then maps the IDs into the customers table and returns the corresponding customer name. How about selecting all those customers using the service with the maximum service fee?
Next up, HAVING clauses.
blog comments powered by Disqus |
|
|
|
|
|
|
|