Bird
0
0

You have a table EmployeeSales with columns Employee, Jan, Feb, Mar representing sales per month. How would you write a query to list each employee's sales by month as rows instead of columns?

hard📝 Application Q8 of 15
SQL - Advanced Query Patterns
You have a table EmployeeSales with columns Employee, Jan, Feb, Mar representing sales per month. How would you write a query to list each employee's sales by month as rows instead of columns?
AUse GROUP BY Employee and SUM sales columns.
BUse UNPIVOT to convert Jan, Feb, Mar columns into rows with month and sales columns.
CUse PIVOT to create new columns for each month.
DUse JOIN to combine monthly sales into one column.
Step-by-Step Solution
Solution:
  1. Step 1: Identify goal

    We want to convert monthly columns into rows to list sales by month per employee.
  2. Step 2: Choose correct operation

    UNPIVOT converts columns into rows, which fits the requirement.
  3. Final Answer:

    Use UNPIVOT to convert Jan, Feb, Mar columns into rows with month and sales columns. -> Option B
  4. Quick Check:

    Columns to rows = UNPIVOT [OK]
Quick Trick: UNPIVOT columns to rows for monthly data [OK]
Common Mistakes:
  • Using PIVOT instead of UNPIVOT
  • Trying to sum columns
  • Using JOIN unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes