🧚 Hör zu! Beekeeper Studio ist eine schnelle, moderne und Open-Source-Datenbank-GUI Herunterladen
January 7, 2026 Von Matthew Rathbone

To create a pivot table in SQL, you will need to use the GROUP BY and CASE statements. Here is an example of how you might go about creating a pivot table.

Throughout this guide, you’ll find interactive SQL exercises where you can practice creating pivot tables directly in your browser. These hands-on examples will help you master using CASE statements with aggregate functions to transform rows into columns—essential for creating summary reports and dashboards.

Using a demo sales table

First, let’s say we have a table called sales that contains information about sales transactions. The table has the following columns: date, product, quantity, and price.

Simple pivot table example

To create a pivot table that shows the total quantity and total price for each product, we would use the following query:

SELECT
  product,
  SUM(CASE WHEN quantity THEN quantity ELSE 0 END) AS total_quantity,
  SUM(CASE WHEN price THEN price ELSE 0 END) AS total_price
FROM sales
GROUP BY product

This query uses a CASE statement to specify which values should be included in the pivot table, and then uses the SUM function to calculate the total quantity and total price for each product. The GROUP BY statement groups the rows by product, which allows us to see the totals for each product separately.

Pivot table results

Here is an example of what the resulting pivot table might look like:

product total_quantity total_price
ProductA 50 500
ProductB 100 1000
ProductC 75 750

Try it yourself:

[[ expanded ? '▼' : '▶' ]]

[[ testData.title ]]

Query the weather_data table to create a pivot table showing average temperature for each city by day of week. Transform the day rows into separate columns (Mon, Tue, Wed).

Interaktives Beispiel ✓ Abgeschlossen
Erwartete Spalten: [[ col ]]
ℹ️ Diese Übung setzt die Datenbank bei jedem Durchlauf zurück. Schreiben Sie Ihre vollständige Lösung in einer einzigen Eingabe.
[[ i18n.correct ]] [[ validationResult ? i18n.all_checks_passed : i18n.query_success ]]
[[ detail.message ]]
[[ col ]]
[[ formatCell(cell) ]]
[[ i18n.not_quite_right ]] [[ patternError ]] [[ validationResult.error ]] [[ i18n.results_dont_match ]]
[[ detail.passed ? '✓' : '✗' ]] [[ detail.message ]]
[[ i18n.your_results ]]
[[ col ]]
[[ formatCell(cell) ]]
[[ i18n.expected_results ]]
[[ col ]]
[[ formatCell(cell) ]]
[[ i18n.sql_error ]]
[[ error ]]
💡 [[ i18n.hint_label ]] [[ testData.hint ]]
📊 [[ i18n.expected_result_label ]]
[[ col ]]
[[ formatCell(cell) ]]
✨ [[ i18n.solution_label ]]
[[ testData.solution ]]
Verfügbare Tabellen
[[ table.name ]]
[[ col ]]
[[ formatCell(cell) ]]

This example shows the classic pivot pattern: transforming rows (months) into columns using CASE statements with SUM aggregation.

SUM, AVG, or other aggregate functions

You can also use the GROUP BY and CASE statements to create pivot tables that show other types of data, such as the average price for each product. For example, the following query would create a pivot table that shows the average price for each product:

SELECT
  product,
  AVG(CASE WHEN price THEN price ELSE NULL END) AS avg_price
FROM sales
GROUP BY product

The resulting pivot table would look something like this:

product avg_price
ProductA 10
ProductB 20
ProductC 15

Try it yourself:

[[ expanded ? '▼' : '▶' ]]

[[ testData.title ]]

Query the website_traffic table to create a pivot table showing both sessions and pageviews for each traffic source. Show Desktop and Mobile as separate column groups.

Interaktives Beispiel ✓ Abgeschlossen
Erwartete Spalten: [[ col ]]
ℹ️ Diese Übung setzt die Datenbank bei jedem Durchlauf zurück. Schreiben Sie Ihre vollständige Lösung in einer einzigen Eingabe.
[[ i18n.correct ]] [[ validationResult ? i18n.all_checks_passed : i18n.query_success ]]
[[ detail.message ]]
[[ col ]]
[[ formatCell(cell) ]]
[[ i18n.not_quite_right ]] [[ patternError ]] [[ validationResult.error ]] [[ i18n.results_dont_match ]]
[[ detail.passed ? '✓' : '✗' ]] [[ detail.message ]]
[[ i18n.your_results ]]
[[ col ]]
[[ formatCell(cell) ]]
[[ i18n.expected_results ]]
[[ col ]]
[[ formatCell(cell) ]]
[[ i18n.sql_error ]]
[[ error ]]
💡 [[ i18n.hint_label ]] [[ testData.hint ]]
📊 [[ i18n.expected_result_label ]]
[[ col ]]
[[ formatCell(cell) ]]
✨ [[ i18n.solution_label ]]
[[ testData.solution ]]
Verfügbare Tabellen
[[ table.name ]]
[[ col ]]
[[ formatCell(cell) ]]

This advanced example demonstrates pivoting with multiple aggregations—combining different metrics into column groups for comprehensive analysis.

Summary

In summary, to create a pivot table in SQL, you can use the GROUP BY and CASE statements along with aggregation functions like SUM and AVG to calculate and display the data you want to see in the pivot table. This approach allows you to easily summarize and analyze large amounts of data, and can be very useful for making business decisions.

Beekeeper Studio Ist Eine Kostenlose & Open-Source-Datenbank-GUI

Das beste SQL-Abfrage- und Editor-Tool, das ich je benutzt habe. Es bietet alles, was ich zur Verwaltung meiner Datenbank brauche. - ⭐⭐⭐⭐⭐ Mit

Beekeeper Studio ist schnell, intuitiv und einfach zu bedienen. Beekeeper unterstützt viele Datenbanken und funktioniert hervorragend unter Windows, Mac und Linux.

Die Linux-Version von Beekeeper ist zu 100% vollständig ausgestattet, ohne Abstriche und ohne Funktionskompromisse.

Was Benutzer Über Beekeeper Studio Sagen

★★★★★
"Beekeeper Studio hat meinen alten SQL-Workflow komplett ersetzt. Es ist schnell, intuitiv und macht die Datenbankarbeit wieder angenehm."
— Alex K., Datenbankentwickler
★★★★★
"Ich habe viele Datenbank-GUIs ausprobiert, aber Beekeeper findet die perfekte Balance zwischen Funktionen und Einfachheit. Es funktioniert einfach."
— Sarah M., Full-Stack-Entwicklerin

Bereit, Ihren SQL-Workflow zu Verbessern?

download Kostenlos Herunterladen