Query Data with Advanced T-SQL Components Flashcards
What are 4 forms of table expressions?
Derived tables, common table expressions (CTEs), views, and inline table-valued functions
Because a table expression is supposed to represent a relation, the inner query defining it needs to be relational. What are 3 requirements to satisfy this?
- All columns returned by the inner query must have names
- All column names must be unique
- The inner query is not allowed to have an ORDER BY clause
Normally the inner query of a table expression is not allowed to have an ORDER BY clause. What is the exception?
The exception is if you use the TOP or OFFSET-FETCH option in the inner query. In this case, the ORDER BY clause serves a meaning that is not related to presentation ordering, rather it’s part of the filter’s specification
When should you use a temporary table/table variable over a table expression?
When you need to persist the result of a query for further processing.
Is the result set of a view stored in the database?
No, only the view’s definition is stored in the database
Syntax to create a view
CREATE VIEW viewname
AS
SELECT col1, col2 FROM table1;
What is the main difference between VIEWS and inline table-valued functions?
Views do not accept input parameters but inline table-valued functions do.
Syntax to create an inline table-valued function
CREATE FUNCTION fxname(@parameter datatype)
RETURNS TABLE
AS
RETURN
SELECT col1, col2
FROM table1
WHERE parameter = @parameter
How do you retrieve data from an inline table-valued function? (Syntax)
SELECT alias.col1, alias.col2, alias.col3
FROM schema.fxname(parameter) as alias
Notes:
- Always use 2-part function name (schema and function)
- Always alias the function
True or False. Derived tables:
- Must have an alias
- Can be referred to multiple times in the same query
- Can use internal or external aliases for columns
- Can refer to parameters or variables
- Cannot be nested within other derived tables
- Must have an alias - True
- Can be referred to multiple times in the same query - False
- Can use internal or external aliases for columns - True
- Can refer to parameters or variables - True
- Cannot be nested within other derived tables - False
In which clause do you use derived tables?
In the FROM clause
Common table expression (CTE) syntax
WITH ctealias AS
( innerquery )
outerquery
Syntax for multiple CTEs
When can you refer to a another CTE within a CTE?
WITH C1 AS
( SELECT .. FROM T1 WHERE… ),
C2 AS
( SELECT … FROM C1 WHERE …)
outerquery;
Each CTE can refer to a previously defined CTE
The following is an example of a recursive CTE. Explain what it’s doing.
WITH EmpsCTE AS
( SELECT empid, mgrid, firstname, lastname
FROM HR.Employees
WHERE empid = 9
UNION ALL
SELECT M.empid, M.mgrid, M.firstname, M.lastname
FROM EmpsCTE AS S
JOIN HR.Employees AS M
ON S.mgrid = M.empid )
SELECT empid, mgrid, firstname, lastname
FROM EmpsCTE;
The first query is the “anchor” member and returns a valid relational result. The anchor member is invoked only once. The second query acts as the “recursive” member. It has to have a reference to the CTE name. It is invoked repeatedly until it returns an empty result set. Think of it as if the results of the second query are added to the first in each repetition. Here, in each round, the recursive member joins the previous result set with the HR.Employees table to return the direct manager of the employee from the previous round. The recursive query stops when it doesn’t find a manager.
What does APPLY do?
Difference between CROSS APPLY and OUTER APPLY
APPLY evaluates the left input first, and for each of the left rows, it applies the table expression provided as the right input. The right input can be a derived table query or table function. The left input provides info to the right input.
CROSS APPLY does not return the left row if the right table expression returns an empty set, but OUTER APPLY does
Syntax for window functions
Fx OVER(PARTITION BY col1
ORDER BY col2
ROWS BETWEEN UNBOUNDED PRECEDING//FOLLOWING AND CURRENT ROW)
Note: instead of UNBOUNDED can also have n rows PRECEDING/FOLLOWING
In which clauses can you use window functions?
SELECT and ORDER BY
What are 4 window ranking functions and what do they do?
ROW_NUMBER: creates unique incrementing integers starting with 1 within the window partition based on the window ordering
RANK: rank values based on window ordering. If two values tie, they’ll receive same rank, e.g. 2. Then it’ll skip a value for the next rank, e.g. 1, 2, 2, 4
DENSE RANK: same as RANK but doesn’t have gaps
NTILE: Can arrange rows within partition in a requested number of equally sized tiles
What are 4 window offset functions and how do they work?
LAG(col, numrows): returns an element from the row in the current partition that is a requested number of rows before the current row.
LEAD(col, numrows): returns an element from the row in the current partition that is a requested number of rows after the current row.
FIRST_VALUE(col): returns a value expression from the first row in the window frame
LAST_VALUE(col): returns a value expression from the last row in the window frame
What 3 elements do you need to pivot data?
- What do you want to see on rows? i.e. on rows or grouping element
- What do you want to see on columns? i.e. on cols or spreading element
- What do you want to see in the intersection of each distinct row and column value? i.e. data or aggregation element.
Syntax to pivot data
WITH PivotData AS
( SELECT groupingcol, spreadingcol, aggregationcol
FROM tablename )
SELECT (select list)
FROM PivotData
PIVOT( aggregatefx(aggregation col)
FOR spreadingcol IN (distinct spreading values)) AS alias.
Notes:
- You should define a table expression that returns the three elements involved in pivoting.
- The PIVOT operator returns a table result. Remember to alias it.
In some cases you might get NULLs in the pivoted table. What should you do if you want to return something else instead of a NULL in those cases?
Use the ISNULL or COALESCE function in the outer query’s select list. e.g.
WITH PivotData AS
( SELECT custid, shipperid, freight
FROM Sales.Orders )
SELECT custid,
ISNULL([1], 0.00) as [1],
ISNULL([2], 0.00) as [2],
ISNULL([3], 0.00) as [3]
FROM PivotData
PIVOT(SUM(freight) FOR shipperid IN ([1], [2], [3])) as P;
Why should you define a table expression that returns the 3 elements involved in pivoting instead of querying the underlying table directly?
Note that you don’t specify the grouping element in the PIVOT operator. It’s determined by elimination. So, if the table has more than the 3 cols, all extra cols will be considered a grouping element
What are 4 limitations of the PIVOT operator?
- The aggregation and spreading elements cannot directly be results of expressions; instead, they must be column names from the queried table. You can, however, apply expressions in the query defining the table expression, assign aliases to those expressions, and then use the aliases in the PIVOT operator
- The COUNT(*) function isn’t allowed as the aggregate function. You need to use COUNT(col).
- Limited to using only one aggregate function
- The IN clause of the PIVOT operator accepts a static list of spreading values. It doesn’t support a subquery as input.










/AWMI:root/AWMI:Location
') as Result
FROM Production.ProductModel
where ProductModelID=7;

















