You are on page 1of 10

Sub-Queries &

aggregation part 1
RELATIONAL DATABASE
Aim
Sub-queries
Introduction to aggregation and grouping
subqueries
Not all queries can be done in one command
Nesting queries required
Inner query is treated as a separate data set
Seminar question
Which active consultants are not leading on
a contract?
◦ An inner join filtering on ACTIVE and
contract_end will give the consultants who
have had a contract at some point but are not
on a current contract
◦ An outer join filtering on ACTIVE and null
contract_id will give the consultants who have
never had a contract but will not cater for the
status of the contract
◦ What we need:
◦ A list of all consultants who are ACTIVE
excluding those who are current contract 37 consultants are active
leads
15 consultants are currently on contract
One solution?
The subquery returns a data set
The outer query uses that data in the filter
The clause NOT IN is used to say everything except something in this list.
The use of subquery can be used where join is not easy to filter on or it can be used where you need to
have aggregation in a filter

Select consultant_fname, consultant_sname


From consultant
Where consultant_id not in (
Select distinct(contract_lead)
From contract
Where contract_end is null)
And consultant_status = ‘ACTIVE’;
Grouping
Data can be grouped when we need to
aggregate the data
Data is collated by attributes
◦ If you select it you must group by it
◦ You do not have to select an attribute to group
by it

If we need to filter on aggregated data we


cannot use the where clause, it requires a
special clause.
Having clause
Having is use in place of where clause
Where filters row level attributes, having filters aggregated data
Aggregated functions
Sum
Max
Min
Average
Count
Functions can be combined, for example, max(sum(expense_value)) these can then be used in a
subquery to return information
Example
Test subquery to see if data
returns correctly

Build in query to the filter

1. Places all the data in groups of


contract id
2. Totals up the value to the
expenses
3. Which group has the largest
number?
4. Use that data in a filter
Seminar tasks
Assumes you have run the script in week 5 and built your data dictionary
Assumes you have amended the data dictionary to include the FK
Requires you to use aggregation and subqueries

You might also like