Q1
True / FalseMySQL supports the PIVOT operator natively.
MySQL does not have a native PIVOT operator. Instead, users often use conditional aggregation or dynamic SQL to achieve similar results.
Q2
True / FalseTo simulate a PIVOT in MySQL, you can use the CASE statement within an aggregate function.
MySQL users can simulate a PIVOT table by using CASE statements within aggregate functions like SUM() or COUNT() to manually transform rows into columns.
Q3
True / FalseYou can use GROUP_CONCAT() to create a PIVOT-like output in MySQL.
GROUP_CONCAT() is used to concatenate values from multiple rows into a single string, but it doesn't perform PIVOT operations. It is not a substitute for PIVOT functionality.
Q4
True / FalseMySQL’s GROUP BY clause can be used in conjunction with conditional aggregation to achieve a PIVOT-like result.
By using GROUP BY along with conditional aggregation (using CASE or IF), you can simulate PIVOT functionality to rearrange data from rows into columns.
Q5
True / FalseThe UNION operator in MySQL can be used to simulate a PIVOT by stacking rows vertically.
UNION is used to combine result sets vertically, not to transform row data into columns. It does not simulate PIVOT operations.
Q6
True / FalseYou can use dynamic SQL in MySQL to create a PIVOT table with varying numbers of columns.
Dynamic SQL allows for creating queries on-the-fly with varying columns, which can be used to generate PIVOT-like tables where the number of columns may change.
Q7
True / FalseMySQL’s INSERT INTO ... SELECT statement can be used to prepare data for a PIVOT table by first creating a staging table.
By using INSERT INTO ... SELECT, you can prepare and transform data in a staging table before performing PIVOT operations through other queries.
Q8
True / FalseUsing the CROSS JOIN clause in MySQL is a recommended method for achieving PIVOT operations.
CROSS JOIN produces a Cartesian product and is not typically used for PIVOT operations. Instead, CROSS JOIN might be used for generating combinations of data.
Q9
True / FalseMySQL’s PREPARE and EXECUTE statements can be used to create a dynamic PIVOT query where column names are generated at runtime.
PREPARE and EXECUTE allow for dynamic SQL execution, which can be used to create PIVOT queries with columns generated dynamically based on runtime data.
Q10
True / FalseMySQL Workbench provides a built-in PIVOT feature for transforming data in a graphical interface.
MySQL Workbench does not provide a built-in PIVOT feature. Users need to manually write SQL queries to simulate PIVOT functionality or use other tools or methods.
Q14
Single ChoiceWhich SQL query would you use to PIVOT this data to show SalesAmount by Product and Quarter?
SQL Code
Given the following table Sales:
Product Quarter SalesAmount
A Q1 1000
A Q2 1500
B Q1 2000
B Q2 2500
This query correctly uses CASE and SUM to simulate a PIVOT table, showing SalesAmount for each Product by Quarter.
Q17
Single ChoiceWrite a SQL query to PIVOT this data to show the total Amount for each Status by Customer.
SQL Code
You have the following Orders table:
OrderID Customer Status Amount
1 Alice Pending 100
2 Bob Shipped 200
3 Alice Shipped 150
4 Bob Pending 250
This query uses CASE and SUM to calculate the total Amount for each Status by Customer, simulating a PIVOT operation.
Q21
Multiple ChoiceWhich SQL query uses PIVOT to transform rows into columns, aggregating sales data by product and quarter?
SQL Code
SELECT product_name,
SUM(CASE WHEN quarter = 'Q1' THEN sales ELSE 0 END) AS Q1_sales,
SUM(CASE WHEN quarter = 'Q2' THEN sales ELSE 0 END) AS Q2_sales,
SUM(CASE WHEN quarter = 'Q3' THEN sales ELSE 0 END) AS Q3_sales,
SUM(CASE WHEN quarter = 'Q4' THEN sales ELSE 0 END) AS Q4_sales
FROM sales_data
GROUP BY product_name;
This query uses conditional aggregation to pivot sales data by quarter, transforming rows into columns for each quarter's sales.
Q22
Multiple ChoiceIdentify the SQL query that uses PIVOT to summarize employee attendance data, showing the total days attended per month.
SQL Code
SELECT employee_name,
SUM(CASE WHEN month = 'January' THEN days_attended ELSE 0 END) AS Jan_days,
SUM(CASE WHEN month = 'February' THEN days_attended ELSE 0 END) AS Feb_days,
SUM(CASE WHEN month = 'March' THEN days_attended ELSE 0 END) AS Mar_days
FROM attendance_data
GROUP BY employee_name;
This query pivots the attendance data by month, aggregating the total days attended for each employee across different months.
Q23
Multiple ChoiceWhich SQL query uses PIVOT to show the total sales for each product across different regions?
SQL Code
SELECT product_name,
SUM(CASE WHEN region = 'North' THEN sales ELSE 0 END) AS North_sales,
SUM(CASE WHEN region = 'South' THEN sales ELSE 0 END) AS South_sales,
SUM(CASE WHEN region = 'East' THEN sales ELSE 0 END) AS East_sales,
SUM(CASE WHEN region = 'West' THEN sales ELSE 0 END) AS West_sales
FROM sales_data
GROUP BY product_name;
This query pivots the sales data by region, showing total sales for each product in different regions.
Q24
Multiple ChoiceDetermine the SQL query that uses PIVOT to display the total number of orders for each product across different years.
SQL Code
SELECT product_name,
SUM(CASE WHEN year = 2021 THEN order_count ELSE 0 END) AS Orders_2021,
SUM(CASE WHEN year = 2022 THEN order_count ELSE 0 END) AS Orders_2022,
SUM(CASE WHEN year = 2023 THEN order_count ELSE 0 END) AS Orders_2023
FROM order_data
GROUP BY product_name;
This query pivots the order data by year, showing the total number of orders for each product across different years.
Q25
Multiple ChoiceWhich SQL query uses PIVOT to aggregate sales data, showing the total revenue generated by each salesperson across different months?
SQL Code
SELECT salesperson,
SUM(CASE WHEN month = 'January' THEN revenue ELSE 0 END) AS Jan_revenue,
SUM(CASE WHEN month = 'February' THEN revenue ELSE 0 END) AS Feb_revenue,
SUM(CASE WHEN month = 'March' THEN revenue ELSE 0 END) AS Mar_revenue
FROM sales_data
GROUP BY salesperson;
This query pivots the sales data by month, showing the total revenue generated by each salesperson across different months.
Q26
Multiple ChoiceWhich SQL query uses PIVOT to display the total hours worked by employees in different departments across various projects?
SQL Code
SELECT department,
SUM(CASE WHEN project = 'Project A' THEN hours_worked ELSE 0 END) AS ProjectA_hours,
SUM(CASE WHEN project = 'Project B' THEN hours_worked ELSE 0 END) AS ProjectB_hours,
SUM(CASE WHEN project = 'Project C' THEN hours_worked ELSE 0 END) AS ProjectC_hours
FROM work_data
GROUP BY department;
This query pivots the work data by project, showing the total hours worked by employees in different departments across various projects.
Q27
Multiple ChoiceIdentify the SQL query that uses PIVOT to show the total number of units sold per product across different store locations.
SQL Code
SELECT product_name,
SUM(CASE WHEN location = 'Store A' THEN units_sold ELSE 0 END) AS StoreA_units,
SUM(CASE WHEN location = 'Store B' THEN units_sold ELSE 0 END) AS StoreB_units,
SUM(CASE WHEN location = 'Store C' THEN units_sold ELSE 0 END) AS StoreC_units
FROM sales_data
GROUP BY product_name;
This query pivots the sales data by store location, showing the total number of units sold for each product across different stores.
Q28
Multiple ChoiceWhich SQL query uses PIVOT to aggregate the number of hours logged by employees on different tasks?
SQL Code
SELECT employee_name,
SUM(CASE WHEN task = 'Task 1' THEN hours_logged ELSE 0 END) AS Task1_hours,
SUM(CASE WHEN task = 'Task 2' THEN hours_logged ELSE 0 END) AS Task2_hours,
SUM(CASE WHEN task = 'Task 3' THEN hours_logged ELSE 0 END) AS Task3_hours
FROM time_log
GROUP BY employee_name;
This query pivots the time log data by task, aggregating the total number of hours logged by employees on different tasks.
Q29
Multiple ChoiceDetermine the SQL query that uses PIVOT to display the average test scores for students across different subjects.
SQL Code
SELECT student_name,
AVG(CASE WHEN subject = 'Math' THEN score ELSE 0 END) AS Math_avg,
AVG(CASE WHEN subject = 'Science' THEN score ELSE 0 END) AS Science_avg,
AVG(CASE WHEN subject = 'History' THEN score ELSE 0 END) AS History_avg
FROM test_scores
GROUP BY student_name;
This query pivots the test scores data by subject, calculating the average score for each student across different subjects.
Q30
Multiple ChoiceWhich SQL query uses PIVOT to summarize sales data, showing the total quantity sold per product across different months?
SQL Code
SELECT product_name,
SUM(CASE WHEN month = 'January' THEN quantity ELSE 0 END) AS Jan_quantity,
SUM(CASE WHEN month = 'February' THEN quantity ELSE 0 END) AS Feb_quantity,
SUM(CASE WHEN month = 'March' THEN quantity ELSE 0 END) AS Mar_quantity
FROM sales_data
GROUP BY product_name;
This query pivots the sales data by month, showing the total quantity sold for each product across different months.