Session 6 - Sub queries, with, CTE, pivot, functions, GUIDS, XML, JSON, Temporal Tables Flashcards
What are the keywords you can use in a subquery comparison?
ANY/SOME
ALL
What is the equivalent word for ANY/SOME
OR
What is the equivalent word for ALL
AND
How would you return all values from table1 where that each contain COL1 using a subquery and COL1 doesn’t match any value from table2?
SELECT * FROM TABLE1 WHERE COL1 <> ALL (SELECT COL1 FROM TABLE2)
What is a correlated subquery?
A subquery that relies on the original query and is used in the SELECT portion of the query:
SELECT EMPNAME, EMPNUMBER, (SELECT ID FROM TABLE2 T2 WHERE T2.EMPNAME = T1.EMPNAME) AS ID FROM TABLE1 T1
How do you run a recursive query?
Start with Anchor then union to the original table with a join to the anchor (Using CTE):
with MyTable ( Select EmployeeNumber, 0 as BossLevel from EmployeeTable where ManagerNumber = 0 UNION ALL Select EmployeeNumber, BossLevel + 1 from EmployeeTable e join MyTable o on e.ManagerNumber = o.EmployeeNumber )
How can you join a derived table to another table in a query?
SELECT a.COL1, a.COL2, b.COL2
FROM TABLE1 a
JOIN (SELECT COL1, COL2 FROM TABLE 2) b
ON a.COL1 = b.COL2
You HAVE to alias the derived table, otherwise the system doesn’t know what it’s named
What are the different functions?
Multi-Statement table Function
Inline Table Function
Scalar Function
What Function statement uses multiple statements to return a table?
Multi-Statement table function
What function statement uses a single statement to return a table?
Inline Table Function
What Function statement returns a single value?
Scalar Function
What is the syntax for creating a Multi-Statement Table Function?
CREATE FUNCTION [dbo].[FunctionName] ( @param1 int, @param2 char(5) ) RETURNS @returntable TABLE ( [c1] int, [c2] char(5) ) AS BEGIN INSERT @returntable SELECT @param1, @param2 RETURN END
What is the syntax for creating a Inline Table Function?
CREATE FUNCTION [dbo].[FunctionName] ( @param1 int, @param2 char(5) ) RETURNS TABLE AS RETURN ( SELECT @param1 AS c1, @param2 AS c2 )
What is the syntax for creating a Scalar Function?
CREATE FUNCTION [dbo].[FunctionName] ( @param1 int, @param2 int ) RETURNS INT AS BEGIN
RETURN @param1 + @param2 END
How can you join a table with a table function?
Use Apply ( select * from table1 a cross apply ( select * from tablefunction(a.col1))
What are the two different ways to use apply? What is the difference?
CROSS APPLY - Like INNER JOIN
OUTER APPLY - Like LEFT JOIN
What is a synonym?
It takes the place of the table name. If table changes name, then query could still find it
What is the syntax for creating a synonym?
CREATE SYNONYM [SYNONYMNAME]
FOR [TABLENAME]
What are the two commands used to run Dynamic SQL?
EXECUTE(SQL STATEMENT IN STRING)
SYS.SP_EXECUTESQL @STATEMENT, @PARAMS
What is the syntax needed to run SYS.SP_EXECUTESQL?
EXECUTE SYS.SP_EXECUTESQL @STATEMENT, @PARAMS, @VAR1, @VAR2
@STATEMENT & @PARAMS HAVE TO BE NVARCHAR
EX:
DECLARE @COMMAND NVARCHAR(MAX) = N’SELECT * FROM JDE_PRODUCTION.PRODDTA.F0101 WHERE ABAN8 = @AB’
DECLARE @PARAMS NVARCHAR(MAX) = N’@AB INT’
DECLARE @AB INT = 41302
EXECUTE SYS.SP_EXECUTESQL @COMMAND, @PARAMS, @AB
Where is it beneficial to use a correlated subquery?
When the select statement will return a small number of rows.
What is the name of a sub-query used in the FROM clause?
Derived Table.
What is the name of a sub-query used in the SELECT clause?
Correlated Subquery.
How would you get the top 5 values from a window function?
Use a WHERE clause on a derived table or WITH :
SELECT * FROM ( SELECT EMPLOYEENUMBER, RANK() OVER (PARTITION BY DEPARTMENT ORDER BY EMPLOYEENUMBER) AS 'RANK' FROM DBO.MYTABLE ) AS RANKEDTABLE WHERE RANK <= 5
What is the syntax of a pivot table?
With PIVOTTABLE AS (SELECT STATEMENT) PIVOT (AGGREGATE(VALUES)) FOR ROWSTOMAKECOLUMNS IN (COLUMN NAMES IN HARD BRACKETS) AS ALIAL
What is the syntax of an UNPIVOT table?
SELECT *
FROM PIVOTTABLE
UNPIVOT (ALIASFORVALUES FOR SPREADEDCOLUMNS IN ([COL1], [COL2])) AS ALIAS
What are some drawbacks of identity?
- You can’t insert it into an existing column
* Transactions made with identity makes identity not able to be rolled back
What does GUID stand for?
Globally Unique Identifier
What is a GUID?
a 128 bit resource for making a unique number
What is the datatype for a GUID?
uniqueidentifier
How do you create a GUID?
Use NEWID()
DECLARE @NEWGUID AS uniqueidentifier
SET @NEWGUID = NEWID()
How is a GUID returned?
As a Hexadecimal
What is the function used to create a GUID used in an Indexed column? What is the limitation?
NEWSEQUENTIALID()
It can only be used in a column containing a DEFAULT constraint.
What is the major problem with using GUIDS?
They are VERY large, 16 Bytes.
What is the best way to store a unique number?
SEQUENCE?
What is the syntax for creating a new SEQUENCE?
CREATE SEQUENCE sequenceName AS INT --OR Any numerical datatype START WITH beginningNumber INCREMENT BY number MINVALUE number MAXVALUE number CYCLE --MEANING THAT IT RESTARTS
What table stores sequences?
sys.sequences
What is the syntax for getting the next value of a SEQUENCE?
SELECT NEXT VALUE FOR sequenceName AS Alias