Content Type Distribution

An easy SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
easy
Seniority
L3

Problem

We want a census of everything in our content library, untitled drafts included, so every row of content_items counts. How many items do we have of each content type, listed alphabetically?

How many of each content type?

Tables

content_items

Columns: content_id INTEGER, title TEXT, content_type TEXT, duration_seconds INTEGER, creator_id INTEGER, publish_date TEXT

content_items: 10 of 200 rows
content_idtitlecontent_typeduration_secondscreator_idpublish_date
243The Ultimate Guide to AI TrendsarticleNULL1972026-02-02
286Top 10 Mobile Appspodcast1542942025-03-03
329Understanding Machine Learning [DRAFT]short2013912026-04-04
372Mastering Tech Startupslivestream2484882025-05-05
415Introduction to Cybersecurityvideo2955852026-06-06
458Deep Dive: Cloud ComputingarticleNULL6822025-07-07
501Behind the Scenes of Product Managementpodcast3897792026-08-08
544Breaking Down DevOpsshort4368762025-09-09
587Exploring Web Developmentlivestream483NULL2026-01-10
630How to Data Sciencevideo53010702025-11-11

Expected output

First 5 of 5 rows
content_typeitem_count
article40
livestream40
podcast40
short40
video40

Worked solution and explanation

This is a row census dressed up as an editorial report. Anyone can produce a count per type; what separates candidates is counting the right thing and handing the result back in the order asked for. content_items holds two drafts whose title is NULL, both of type 'short', so a query that counts titles reports 38 shorts instead of 40 and looks perfectly plausible doing it. Counting unique titles is worse still, because the same title repeats across many items.

Building it

Step 1: Set the grain to one row per content_type

The question is about types, not items, so the 200 rows collapse into one row per value of content_type. A single overall COUNT(*) answers a different question (200), and listing the types alone gives five names with nothing next to them.

Step 2: Count rows, not a column

COUNT(*) counts every row in the bucket. COUNT(title) counts only rows where title is filled, and the two untitled drafts vanish from 'short'. A census of the library includes drafts, so the count is of rows.

Step 3: Sort by the type name

Grouping hands rows back in whatever order the engine's buckets produce. The listing is alphabetical, so ORDER BY content_type is part of the answer, not presentation. Every type has 40 items here, so sorting by item_count would leave the order undetermined.

Count per content type
SELECT content_type, COUNT(*) AS item_count
FROM content_items
GROUP BY content_type
ORDER BY content_type

One row per content_type, alphabetical.

Counting rows
COUNT(*) gives 40 for every type, 'short' included: each item is a row, titled or not.
Counting titles
COUNT(title) skips the two drafts with a NULL title and reports 38 shorts. COUNT(DISTINCT title) drops further, because titles repeat across items.

Common follow-up questions