What is a grouped query
A query that contains
1. An aggregate function
2. A group by clause
3. Both
SQL Server 70-461 05-01
How many rows are returned per group?
1
SQL Server 70-461 05-01
What is the difference between WHERE and HAVING
SQL Server 70-461 05-01
Does WHERE or HAVING come first in logical query processing?
SQL Server 70-461 05-01
What aggregate functions does T-SQL support?
SQL Server 70-461 05-01
How are nulls handled by aggregate functions?
SQL Server 70-461 05-01
What is the difference between count(*) and count(FieldName)
SQL Server 70-461 05-01
Where is DISTINCT placed when using it in an aggregate function
SELECT shipperid, count(DISTINCT FieldName) AS numshippingdates FROM sales.orders GROUP BY shipperid
SQL Server 70-461 05-01
What aggregate function is DISTINCT most commonly used with
count
SQL Server 70-461 05-01
Why would you use a grouping set clause
SQL Server 70-461 05-01
Example grouping set clause
GROUP BY GROUPINGSETS ( (shipperid, year(shippeddate)), (shipperid ), (year(shippeddate) ), ( ), );
Note: the blank parens represent an empty set
SQL Server 70-461 05-01
What is the output of a query with multiple grouping sets
SQL Server 70-461 05-01
Your three columns are shipperid, shipyear, numorders. In a GROUP BY GROUPINGSETS clause, what happens if the set doesn’t use all of the columns? For example, where shipper is is by itself in parens and shipyear is by itself in parens.
GROUP BY GROUPINGSETS ( (shipperid, shipyear), (shipperid ), (shipyear ), ( ), );
Note: count (*) AS numorders is always returned
SQL Server 70-461 05-01
What are the three clauses that allow multiple groupings in one query and where are they placed?
Place these right after GROUP BY
SQL Server 70-461 05-01
What is the general form of the GROUP BY CUBE expression and how is it similar to GROUP BY GROUPING SETS?
GROUP BY CUBE(col1, col2, col3, etc)
GROUP BY CUBE(shipperid, year(shippeddate)
-Is the same as-
GROUP BY GROUPINGSETS
(
(shipperid, year(shippeddate)),
(shipperid ),
(year(shippeddate) ),
( ),
);
SQL Server 70-461 05-01
What is the general format of the GROUP BY ROLLUP expression?
GROUP BY ROLLUP (col1, col2, col3, etc)
SQL Server 70-461 05-01
How does GROUP BY ROLLUP work? And how is it different from GROUP BY CUBE?
GROUP BY ROLLUP accepts a list of inputs and, unlike GROUP BY CUBE, it does not return all possible combinations of the inputs as grouping sets. It assumes a hierarchy.
GROUP BY ROLLUP(shipcountry, shipregion, shipcity) Is the same as (shipcountry, shipregion, shipcity), (shipcountry, shipregion ), (shipcountry ), ( ) )
There are 8 possible combinations of the fields. ROLLUP only gives 4. CUBE gives all 8.
SQL Server 70-461 05-01
How does GROUP BY CUBE work?
It takes a list of inputs and defines all possible grouping sets that could be generated, including the empty set.
SQL Server 70-461 05-01
When using GROUPING SETS, CUBE or ROLLUP, can you put more than one in the GROUP BY clause?
Yes, just separate them by commas. When you do this you get a multiplying effect. ROLLUP(x,y,z), CUBE(A,B,C) would generate 32 grouping sets. 4 sets from ROLLUP and 8 sets from CUBE for a total of 32 sets.
SQL Server 70-461 05-01
What does a multiple grouping set do?
It groups the data in more than one way in one query
SQL Server 70-461 05-01
How does HAVING work?
Very similar to WHERE, except it is applied per group instead of per row.
GROUP BY comes before HAVING, so you can refer to aggregate computations in HAVING
SQL Server 70-461 05-01
What does the empty set define?
One group with all rows for grand aggregates.
SQL Server 70-461 05-01
HAVING, SELECT and ORDER BY are the last three clauses in the logical query processing phases. Why is this important to know?
SQL Server 70-461 05-01
What are two ways you can distinguish between nulls that are a result of grouping sets and nulls that were in the original data prior to grouping?
SQL Server 70-461 05-01