Table Expressions Flashcards
What is special about the table expressions stored as database objects?
- They can be re-used
- Access to the objects can be controlled with permissions
SQL Server 70-461 04-02a
What are the four types of table expressions in T-SQL?
- Derived Tables
- Common Table Expressions (CTEs)
- Views
- Inline Table-Valued Functions
SQL Server 70-461 04-02a
Of the four table expressions, which are only available in the statement that defines them and which are preserved as database objects?
Only available to statement that defines them
- Derived Tables
- Common Table Expressions (CTEs)
Preserved as database objects
- Views
- Inline Table-Valued Functions
SQL Server 70-461 04-02a
If ORDER BY is used in a table expression to facilitate TOP or OFFSET-FETCH, what happens to the outer query order?
There is no guarantee of the order the outer query will have unless it has its own ORDER BY clause
SQL Server 70-461 04-02a
Describe in words where you place a Derived Table in a query and what its structure is like.
- Define the query that creates the Derived Table in parenthesis, like you would a subquery
- Place it in the FROM clause of the outer query
- Give the Derived Table a name, just like doing an alias, after the parenthesis.
SQL Server 70-461 04-02a
What is the general structure of a Derived Table?
SELECT * FROM ( Derived Table ) AS Derived_Table_Name
This is a very simple example where the outer select query selects all records from the Derived Table. Often you will see the derived table joined to another table.
SQL Server 70-461 04-02a
Can you refer to an alias that was created in a Derived Table from the WHERE clause of the outer query?
Yes. Since the Derived Table is in the FROM clause and the FROM clause is processed before the WHERE clause.
SQL Server 70-461 04-02a
What are the two ways to alias columns in a Derived Table?
- Inline and External
- Inline is like Normal Example: column AS alias
- External comes after the Derived Tables name. You must list all columns in the Derived Table, regardless of whether you want an alias or not, separated by commas between parentheses after the Derived Table’s name.
SQL Server 70-461 04-02a
What does the external form of column aliasing for Derived Tables look like?
FROM (DERIVED TABLE) AS D(col1, col2, col3)
SQL Server 70-461 04-02a
What is a CTE?
One of the four types of table expressions. CTE stands for Common Table Expression.
SQL Server 70-461 04-02a
How does the layout of a CTE differ from a derived table?
- The entire definition of the CTE happens from start to finish before the outer query executes.
- A Derived Table is in the middle of the outer query.
SQL Server 70-461 04-02a
How is a CTE laid out?
It goes before main select statement
~~~
WITH <CTE_NAME> AS
(</CTE_NAME>
<inner_query>
)
<outer_query>
~~~
In the code above, this is the CTE:
WITH <CTE_NAME> AS
(
<inner\_query\>
)
## Footnote
SQL Server 70-461 04-02a
</CTE_NAME></outer_query></inner_query>
How long do CTEs last?
- As soon as the outer query runs that was below the CTEs, they are no longer available.
- They are only available to the statement that defined them, not all statements in the session. So from WITH to the outer query end.
SQL Server 70-461 04-02a
What would the layout be if using more than one CTE?
Separate by commas
~~~
WITH <CTE_NAME> AS
(</CTE_NAME>
<inner_query>
), --First CTE
<CTE_NAME> AS
(
<inner_query2>
) --Second CTE
<outer_query>
~~~
Just writing a bunch of extra words so the code will left justify :)
## Footnote
SQL Server 70-461 04-02a
</outer_query></inner_query2></CTE_NAME></inner_query>
Example of the same Derived Table that is referred to multiple times.
FROM ( Derived Table ) AS D1 LEFT OUTER JOIN ( same exact Derived Table as above, code fully written out ) AS D2 --Give it a different name, such as D2
Same exact Derived Table used twice. Has to be fully written out each time.
SQL Server 70-461 04-02a
How many queries does a recursive CTE have?
- At least two (more are possible)
- At least one query known as the anchor member
- At least one query known as the recursive member
SQL Server 70-461 04-02a
How many times is an anchor member invoked in a recursive CTE?
Once
SQL Server 70-461 04-02a
What does the reference to the recursive CTE name represent the first time the recursive member is invoked?
The result set that the anchor member returned.
SQL Server 70-461 04-02a
What does the reference to the recursive CTE name represent each time the recursive member is invoked after the first time?
The result set returned by the previous invocation of the recursive member.
SQL Server 70-461 04-02a
What are the different forms of results a subquery can return?
- Scalar
- Multi-Valued
- Table-Valued
SQL Server 70-461 04-02a
How does EXISTS work with a correlated subquery?
It accepts a subquery as an input and returns:
- true when the subquery returns at least one row
- false otherwise.
EXISTS only returns True or False. It doesn’t return results.
SQL Server 70-461 04-02a