Use CASE within aggregation functions to count items by category in a single query. Count tracks by price tier without using GROUP BY on the tier.
Write a query that counts how many Standard and Premium tracks exist using CASE inside COUNT/SUM.
| Column | Type |
|---|---|
| TrackId | INTEGER (Primary Key) |
| Name | TEXT |
| AlbumId | INTEGER (Foreign Key → Album.AlbumId) |
| MediaTypeId | INTEGER (Foreign Key) |
| GenreId | INTEGER (Foreign Key → Genre.GenreId) |
| Composer | TEXT |
| Milliseconds | INTEGER |
| Bytes | INTEGER |
| UnitPrice | NUMERIC(10,2) |
SELECT
SUM(CASE WHEN condition THEN 1 ELSE 0 END) AS ConditionCount
FROM table;
Ready to take your skills to the next level? Enroll in our comprehensive Data Analyst Career Track to master SQL, Python, Excel, and Power BI.
Run your code to see the result here.