Microsoft SQL Server Database Quiz Questions

Chapter Name:Chapter 7 - DQL (Data Query Language)
Lesson Content Link:PIVOT
Current Quiz Count:45
Progress
0%
Q1
True / False

The PIVOT operator is used to rotate rows into columns in SQL Server.

Q2
True / False

The PIVOT operator can be used to aggregate data from columns into rows.

Q3
True / False

The PIVOT operation requires at least one aggregate function.

Q4
True / False

You can use the PIVOT operator with multiple aggregate functions in a single query.

Q5
True / False

The PIVOT operator can only be used on numeric data types.

Q6
True / False

PIVOT can be used to convert data from columns into rows.

Q7
True / False

The PIVOT operator can be used to aggregate data without using GROUP BY.

Q8
True / False

When using PIVOT, you need to specify the columns that will be transformed into rows.

Q9
True / False

The PIVOT operator requires the use of the UNPIVOT operator in the same query.

Q10
True / False

You can only pivot on columns with aggregate values, not individual values.

Q11
True / False

PIVOT operation can be applied to a subquery.

Q12
True / False

The PIVOT operator can be used with static column names only.

Q13
True / False

The PIVOT operator requires that the columns being pivoted have distinct values.

Q14
True / False

PIVOT operations cannot be nested within other PIVOT operations.

Q15
True / False

The PIVOT operator can be used in conjunction with the ORDER BY clause.

Q16
Single Choice

Which keyword is used to rotate rows into columns in SQL Server?

Q17
Single Choice

What must you specify in a PIVOT operation?

Q18
Single Choice

Which aggregate function is commonly used with the PIVOT operator?

Q19
Single Choice

Which clause is necessary to specify in a PIVOT operation to define the new columns?

Q20
Single Choice

Which of the following is a required component of a PIVOT query?

Q21
Single Choice

What is the primary purpose of the PIVOT operator?

Q22
Single Choice

Which SQL clause is typically used with PIVOT to group the aggregated data?

Q23
Single Choice

What does the PIVOT operation do with non-aggregated values?

Q24
Single Choice

In a PIVOT query, what does the following snippet represent? PIVOT(SUM(Sales) FOR Year IN ([2019], [2020], [2021]))

SQL Code
PIVOT(SUM(Sales) FOR Year IN ([2019], [2020], [2021]))
Q25
Single Choice

Which SQL function is used to pivot data in Microsoft SQL Server?

Q26
Single Choice

Which clause specifies the column values that become the new columns in a PIVOT operation?

Q27
Single Choice

The PIVOT operator can be used with which types of aggregate functions?

Q28
Single Choice

What will happen if you use the PIVOT operator without an aggregate function?

Q29
Single Choice

Which of the following is not required for a PIVOT query?

Q30
Single Choice

Can you use PIVOT on multiple columns in a single query?

Q31
Multiple Choice

Which of the following SQL code snippets correctly uses the PIVOT operator? Select all that apply.

SQL Code
SELECT * FROM (
 SELECT Year, Sales
 FROM SalesData
) AS SourceTable
PIVOT (
 SUM(Sales)
 FOR Year IN ([2019], [2020], [2021])
) AS PivotTable;
Q32
Multiple Choice

Select the correct usage of PIVOT with different aggregate functions. Select all that apply.

SQL Code
SELECT * FROM (
 SELECT Year, Sales, Quantity
 FROM SalesData
) AS SourceTable
PIVOT (
 SUM(Sales) FOR Year IN ([2019], [2020], [2021])
) AS SalesPivot

SELECT * FROM (
 SELECT Year, Quantity
 FROM SalesData
) AS SourceTable
PIVOT (
 COUNT(Quantity) FOR Year IN ([2019], [2020], [2021])
) AS QuantityPivot;
Q33
Multiple Choice

Which of the following are correct ways to handle dynamic columns in a PIVOT operation? Select all that apply.

SQL Code
DECLARE @cols AS NVARCHAR(MAX),
 @query AS NVARCHAR(MAX);

SET @cols = STUFF((SELECT DISTINCT ',' + QUOTENAME(Year)
 FROM SalesData
 FOR XML PATH(''), TYPE
 ).value('.', 'NVARCHAR(MAX)')
 ,1,1,'')

SET @query = 'SELECT * FROM (
 SELECT Year, Sales
 FROM SalesData
 ) AS SourceTable
 PIVOT (
 SUM(Sales) FOR Year IN (' + @cols + ')
 ) AS PivotTable;'

EXEC sp_executesql @query;
Q34
Multiple Choice

Which SQL keyword must be used to specify the column values in a PIVOT operation? Select all that apply.

Q35
Multiple Choice

Which scenarios are suitable for using the PIVOT operator? Select all that apply.

Q36
Multiple Choice

Which SQL code snippet shows the correct use of PIVOT to aggregate and transform data? Select all that apply.

SQL Code
SELECT * FROM (
 SELECT Department, Sales, Quarter
 FROM SalesData
) AS SourceTable
PIVOT (
 SUM(Sales) FOR Quarter IN ([Q1], [Q2], [Q3], [Q4])
) AS PivotTable;

SELECT * FROM (
 SELECT Department, Sales, Year
 FROM SalesData
) AS SourceTable
PIVOT (
 AVG(Sales) FOR Year IN ([2019], [2020], [2021])
) AS PivotTable;
Q37
Multiple Choice

Select the correct use of PIVOT when working with dynamic data. Select all that apply.

SQL Code
DECLARE @cols AS NVARCHAR(MAX),
 @query AS NVARCHAR(MAX);

SET @cols = STUFF((SELECT DISTINCT ',' + QUOTENAME(ColumnName)
 FROM DynamicTable
 FOR XML PATH(''), TYPE
 ).value('.', 'NVARCHAR(MAX)')
 ,1,1,'')

SET @query = 'SELECT * FROM (
 SELECT ColumnName, Value
 FROM DynamicTable
 ) AS SourceTable
 PIVOT (
 SUM(Value) FOR ColumnName IN (' + @cols + ')
 ) AS PivotTable;'

EXEC sp_executesql @query;
Q38
Multiple Choice

Which of the following SQL code snippets demonstrates the correct use of PIVOT to summarize sales data by region and year? Select all that apply.

SQL Code
SELECT * FROM (
 SELECT Region, Sales, Year
 FROM SalesData
) AS SourceTable
PIVOT (
 SUM(Sales) FOR Year IN ([2019], [2020], [2021])
) AS PivotTable;

SELECT * FROM (
 SELECT Region, Sales, Quarter
 FROM SalesData
) AS SourceTable
PIVOT (
 AVG(Sales) FOR Quarter IN ([Q1], [Q2], [Q3], [Q4])
) AS PivotTable;
Q39
Multiple Choice

Which of the following SQL code snippets correctly pivots data from columns to rows? Select all that apply.

SQL Code
SELECT * FROM (
 SELECT Department, Sales, Month
 FROM SalesData
) AS SourceTable
PIVOT (
 SUM(Sales) FOR Month IN ([January], [February], [March], [April])
) AS PivotTable;

SELECT * FROM (
 SELECT Product, Quantity, Category
 FROM Inventory
) AS SourceTable
PIVOT (
 COUNT(Quantity) FOR Category IN ([Electronics], [Furniture], [Clothing])
) AS PivotTable;
Q40
Multiple Choice

Select the correct usage of PIVOT for aggregating data over different time periods. Select all that apply.

SQL Code
SELECT * FROM (
 SELECT Year, Sales, Month
 FROM SalesData
) AS SourceTable
PIVOT (
 SUM(Sales) FOR Month IN ([January], [February], [March], [April])
) AS PivotTable;

SELECT * FROM (
 SELECT Year, Sales, Quarter
 FROM SalesData
) AS SourceTable
PIVOT (
 AVG(Sales) FOR Quarter IN ([Q1], [Q2], [Q3], [Q4])
) AS PivotTable;
Q41
Multiple Choice

Which SQL code snippets show correct PIVOT usage for multiple aggregate functions? Select all that apply.

SQL Code
SELECT * FROM (
 SELECT Region, Sales, Year
 FROM SalesData
) AS SourceTable
PIVOT (
 SUM(Sales) FOR Year IN ([2019], [2020], [2021])
) AS TotalSalesPivot;

SELECT * FROM (
 SELECT Region, Quantity, Year
 FROM SalesData
) AS SourceTable
PIVOT (
 AVG(Quantity) FOR Year IN ([2019], [2020], [2021])
) AS AvgQuantityPivot;
Q42
Multiple Choice

What are the necessary components for a valid PIVOT operation in SQL? Select all that apply.

Q43
Multiple Choice

Which of the following SQL code snippets correctly pivots data using multiple columns in the aggregate function? Select all that apply.

SQL Code
SELECT * FROM (
 SELECT Department, Sales, Quarter, Region
 FROM SalesData
) AS SourceTable
PIVOT (
 SUM(Sales) FOR Quarter IN ([Q1], [Q2], [Q3], [Q4])
) AS PivotTable;

SELECT * FROM (
 SELECT Department, Sales, Quarter, Region
 FROM SalesData
) AS SourceTable
PIVOT (
 MAX(Sales) FOR Quarter IN ([Q1], [Q2], [Q3], [Q4])
) AS PivotTable;
Q44
Multiple Choice

Which scenarios correctly illustrate the use of the PIVOT operator? Select all that apply.

Q45
Multiple Choice

Identify the correct use of the PIVOT operator to transform sales data across multiple years. Select all that apply.

SQL Code
SELECT * FROM (
 SELECT Year, Sales, Region
 FROM SalesData
) AS SourceTable
PIVOT (
 SUM(Sales) FOR Year IN ([2019], [2020], [2021])
) AS PivotTable;

SELECT * FROM (
 SELECT Year, Sales, Region
 FROM SalesData
) AS SourceTable
PIVOT (
 AVG(Sales) FOR Year IN ([2019], [2020], [2021])
) AS PivotTable;