SQL Advanced (Analytics)
🎯 Free Guest Mode: You are learning for free. Sign in to save your completion progress and quiz answers.
Loading progress...

Key moments

  1. Relational Operators Overview — The instructor defines PIVOT and UNPIVOT as relational operators for converting rows to columns and columns to rows.
  2. Sample Dataset Setup — The instructor presents the employee table schema containing employee_id, department, and salary columns.
  3. Writing PIVOT Queries — The instructor constructs a PIVOT query using SUM(salary) to generate sales and accounting columns.
  4. Analyzing PIVOT Output — The instructor examines how pivoted columns display aggregated salaries and populate missing intersections with NULL.
  5. Writing UNPIVOT Queries — The instructor writes an UNPIVOT query on the derived pivoted table to restore the original row-based department structure.
PDF notes

Frequently asked questions

Why does PIVOT require an aggregate function while UNPIVOT does not?

PIVOT maps multiple rows to single cells, requiring aggregation like SUM to consolidate values. UNPIVOT unpacks distinct column cells into separate rows without combining data.

What happens to employee records that lack values for a pivoted column?

The PIVOT operation automatically fills missing category intersections with NULL values in the corresponding columns.

Must the pivoted or unpivoted subquery always have an alias?

Yes, SQL relational syntax requires derived tables produced by PIVOT and UNPIVOT operators to have an explicit table alias.

How was this lesson?

Your feedback helps us refine explanations and catch bugs.