# Magnatune catalog: SQL



`GET https://magnatune.com/api/sql?q=SELECT...` (or `POST` the SQL as the body, as `sql=` form data, or as JSON `{"sql": "..."}`) runs one **read-only** SQLite `SELECT` on the whole catalog and returns `{columns, rows, row_count, truncated, ms}`. No key needed. Limits: one statement, at most 1000 rows (add `LIMIT`/`OFFSET` to page), 3 seconds per query, 20 queries and 10 seconds of query time per minute per IP. `GET https://magnatune.com/api/sql` with no query returns this schema as JSON.

The same database is downloadable: <https://magnatune.com/info/sqlite_normalized.db> (SQLite file), <https://magnatune.com/info/sqlite_normalized.db.gz> (gzipped), <https://magnatune.com/info/sqlite_normalized.sql.gz> (gzipped SQL dump: `gunzip` it and run it into an empty SQLite database). It is rebuilt with every new release; <https://magnatune.com/info/changed.txt> changes when it does.

### Ready-made views (start here)

- **`album_details`**: one row per album with everything you usually filter on: `album_id, sku, album, artist_id, artist, artist_page, city, state, country, release_date` (YYYY-MM-DD), `popularity` (higher = more listened to), `pro` (collecting society short name), `pro_description`, `genres` (comma-separated), `web_url`, `api_url`.
- **`song_vocals`**: `song_id, vocals`, and **`album_vocals`**: `album_id, vocals`. `vocals` is `n` (instrumental: never had vocals), `y` (has singing) or `r` (vocals removed: the "(vocals removed)" albums, whose sku is the original's sku + `nv`, same track numbers), or NULL (not checked yet). For instrumental music use `coalesce(v.vocals,'') <> 'y'`.
- **`album_ai`**: `album_id, moods, uses, energy, instrumentation, sounds_like, summary`: each album described by an AI model for people choosing music for videos. `moods` and `uses` are comma-separated words from fixed lists (`GET https://magnatune.com/api/vocabulary`); match one with `(', ' || moods || ',') LIKE '%, uplifting,%'`. `energy` is 1 (very calm) to 5 (very intense).
- The same data is in the `albums` columns `moods, uses, energy, instrumentation, sounds_like, ai_summary` and the `songs` column `bpm` (in catalogs exported since 2026-10-20).
- **`song_bpm`**: `song_id, bpm`: approximate tempo from automatic detection, folded into 80-160 (fair for rock, electronic and dance; unreliable for ambient and classical).
- **`song_audio`**: `song_id, tempo, tempo_conf, beats_conf, musical_key, key_strength, danceability, loudness_db, energy, audio_moods, audio_instruments`: each song measured from its audio (Essentia). `tempo` is only meaningful when `beats_conf >= 1.5` (a steady beat; ambient, rubato and free-time pieces score lower); `musical_key` like `D minor`; `energy` 1-5 (fifths of the catalog); `audio_moods` (MTG-Jamendo moods/themes such as film, relaxing, energetic, corporate, epic) and `audio_instruments` (piano, cello, electricguitar, synthesizer, drums, voice...) are comma-separated. Also in the `songs` columns of catalogs exported since 2026-10-21.
- **`album_subgenres`**: `album_id, subgenre_id, subgenre`. Use this rather than joining `subgenres_albums` yourself (its ids are off by one in current exports; the view corrects it).

### How the tables join

```text
  pro ◄──── albums.pro              (each album: one collecting society)
             │
  artists ◄─ albums.artist_id       (artist 1 : N albums)
             │ album_id
             ├──► songs.album_id                        (album 1 : N songs, ordered by track_no)
             │       └─ song_id ──► playlist_songs ──► playlists   (N : M, ordered by sort_order)
             ├──► genres_albums      ──► genres          (N : M)
             ├──► subgenres_albums   ──► subgenres       (N : M; ids off by one: use the album_subgenres view)
             ├──► collections_albums ──► collections     (N : M)
             └──► recommendations    ──► albums          (album -> recommended album, rank 1 = best)
```

| Foreign key (child) | Points to (parent) | Relationship |
|---|---|---|
| `albums.artist_id` | `artists.artists_id` | each album has one artist; an artist has many albums |
| `albums.pro` | `pro.pro_id` | the album's collecting society (0 = Unknown, 25 = NONE) |
| `songs.album_id` | `albums.album_id` | an album's tracks; `songs.track_no` gives their order |
| `genres_albums.album_id`, `genres_albums.genre_id` | `albums.album_id`, `genres.genre_id` | many-to-many: an album has 1+ genres |
| `subgenres_albums.album_id`, `subgenres_albums.subgenre_id` | `albums.album_id`, `subgenres.subgenre_id` **- 1** | many-to-many, but the link ids are one LOW in current exports: join `subgenres.subgenre_id = subgenres_albums.subgenre_id + 1`, or simply use the `album_subgenres` view |
| `collections_albums.album_id`, `collections_albums.collection_id` | `albums.album_id`, `collections.collections_id` | many-to-many: moods, instruments, uses, composers |
| `playlist_songs.playlist_id`, `playlist_songs.song_id` | `playlists.playlist_id`, `songs.song_id` | many-to-many, ordered by `playlist_songs.sort_order` |
| `recommendations.album_id`, `recommendations.recommended_album_id` | `albums.album_id` (both) | album-to-album suggestions, `rank` 1 = best match |

Watch the key names: the artist key is `artists.artists_id` (plural) but `albums.artist_id`; the collection key is `collections.collections_id` but `collections_albums.collection_id`. SQLite doesn't declare these foreign keys in the file (`PRAGMA foreign_key_list` is empty); the table above is the authority. Indexes exist on every `album_id`, `artist_id`, `genre_id`, `subgenre_id` and `collection_id` join column.

**Ids are permanent**: `artists_id`, `album_id` and `song_id` stay the same from one release to the next and are never reused, so they are safe to store. `song_id` = `album_id` * 100 + `track_no` (albums have at most 99 tracks), so `song_id / 100` is the album and `song_id % 100` the track. Ids from exports before October 2026 were renumbered on every release and don't match these.

### Tables and columns

| Table (rows) | Column | Type | Meaning (example) |
|---|---|---|---|
| `artists` (723) | `artists_id` | integer, key | permanent: never changes or gets reused |
| | `name` | text, unique | display name (`Paolo Pavan`) |
| | `page` | text | URL slug: https://magnatune.com/artists/{page}/ (`paolo_pavan`) |
| | `homepage` | text | same as `page` (legacy) |
| | `description` | text | one-line style summary (`Urban funky jazz piano...`) |
| | `bio` | text | full biography |
| | `photo` | text | site path of the photo (`/artists/img/paolo_pavan.jpg`) |
| | `city`, `state`, `country` | text | where they're from; `state` only for the USA (`Rome`, empty, `Italy`) |
| `albums` (1,927) | `album_id` | integer, key | permanent: one per sku, never changes or gets reused |
| | `artist_id` | integer | -> `artists.artists_id` |
| | `name` | text, unique | album title (`French Kiss`) |
| | `sku` | text, unique | the album's id everywhere in this API (`1kub-frenchkiss`) |
| | `description` | text | album notes |
| | `upc` | text | barcode |
| | `release_date` | integer | Unix timestamp; `date(release_date, 'unixepoch')` gives YYYY-MM-DD |
| | `popularity` | integer | higher = more listened to |
| | `pro` | integer | -> `pro.pro_id` |
| | `itunes_buy_url` | text | legacy, usually empty |
| `pro` (51) | `pro_id`, `name`, `description` | integer, text, text | collecting society: `NONE` (25) = in none, `Unknown` (0) = not recorded, `ASCAP`, `BMI`, `PRS`, `SACEM`, `GEMA`... (`description` adds the country) |
| `songs` (23,771) | `song_id` | integer, unique | `album_id` * 100 + `track_no`, so permanent too (`56406` = track 6 of album 564) |
| | `album_id` | integer | -> `albums.album_id` |
| | `name` | text | song title |
| | `track_no` | integer | 1, 2, ...; the audio: `GET /api/albums/{sku}/tracks/{track_no}/file` |
| | `duration` | integer | seconds |
| | `mp3` | text | the file name on the server (`01-I'm Monster-1kub.mp3`) |
| `genres` (10) | `genre_id`, `name` | integer, text | Alt Rock, Ambient, Classical, Electro Rock, Electronica, Hard Rock, Hip Hop, Jazz, New Age, World |
| `subgenres` (80) | `subgenre_id`, `name` | integer, text | finer styles: Baroque, Blues, Chamber Music... |
| `collections` (472) | `collections_id`, `name` | integer, text | moods, instruments, uses (`Background Mix`) and one per classical composer |
| `genres_albums` (2,478), `subgenres_albums` (5,221), `collections_albums` (3,723) | | integer pairs | the many-to-many links above |
| `playlists` (24) | `playlist_id`, `sort_order`, `owner_id`, `name` | | Magnatune-curated playlists (`80s Rock`) |
| `playlist_songs` (5,867) | `playlist_id`, `sort_order`, `song_id` | integers | the songs of each playlist, in order |
| `recommendations` (35,492) | `album_id`, `recommended_album_id`, `rank` | integers | "if you like this, try..." |

### Join recipes

- An album with its artist and collecting society:
  `SELECT al.sku, al.name AS album, ar.name AS artist, ar.country, p.name AS pro FROM albums al JOIN artists ar ON ar.artists_id = al.artist_id JOIN pro p ON p.pro_id = al.pro WHERE al.sku = '1kub-frenchkiss'`
- An album's tracks, in order:
  `SELECT s.track_no, s.name, s.duration FROM songs s JOIN albums al ON al.album_id = s.album_id WHERE al.sku = '1kub-frenchkiss' ORDER BY s.track_no`
- Albums in a genre:
  `SELECT al.sku, al.name FROM albums al JOIN genres_albums ga ON ga.album_id = al.album_id JOIN genres g ON g.genre_id = ga.genre_id WHERE g.name = 'Jazz'`
- Albums in a subgenre (the raw ids are one low, hence + 1):
  `SELECT al.sku, al.name FROM albums al JOIN subgenres_albums sa ON sa.album_id = al.album_id JOIN subgenres sg ON sg.subgenre_id = sa.subgenre_id + 1 WHERE sg.name = 'Baroque'`
- Albums in a collection:
  `SELECT al.sku, al.name FROM albums al JOIN collections_albums ca ON ca.album_id = al.album_id JOIN collections c ON c.collections_id = ca.collection_id WHERE c.name = 'Background Mix'`
- A playlist's songs, in order, with their albums:
  `SELECT ps.sort_order, s.name AS song, al.sku, s.track_no FROM playlists pl JOIN playlist_songs ps ON ps.playlist_id = pl.playlist_id JOIN songs s ON s.song_id = ps.song_id JOIN albums al ON al.album_id = s.album_id WHERE pl.name = '80s Rock' ORDER BY ps.sort_order`
- Albums recommended for an album:
  `SELECT r.rank, rec.sku, rec.name FROM recommendations r JOIN albums src ON src.album_id = r.album_id JOIN albums rec ON rec.album_id = r.recommended_album_id WHERE src.sku = '1kub-frenchkiss' ORDER BY r.rank`

Text matching: `LIKE` is case-insensitive for ASCII; genres in `album_details.genres` are comma-separated, so match with `genres LIKE '%Jazz%'`. Every album row is a released, visible album.

### Example queries

- Jazz artists from outside the USA whose albums are in no collecting society (11 artists: Argentina, Brazil, Czech Republic, England, Germany, Italy, Japan, Russia, Serbia):
  `SELECT DISTINCT artist, city, country, album FROM album_details WHERE genres LIKE '%Jazz%' AND country <> 'USA' AND pro = 'NONE' ORDER BY country, artist`
- French jazz artists, with the collecting society each album is in (none are society-free, so this shows how to relax a filter):
  `SELECT DISTINCT artist, city, album, pro_description FROM album_details WHERE country = 'France' AND genres LIKE '%Jazz%'`
- Short ambient tracks (2-3 minutes) from albums in no collecting society, most popular first:
  `SELECT d.artist, d.album, s.track_no, s.name AS song, s.duration, d.api_url || '/tracks/' || s.track_no AS track_api FROM songs s JOIN album_details d ON d.album_id = s.album_id WHERE d.genres LIKE '%Ambient%' AND d.pro = 'NONE' AND s.duration BETWEEN 120 AND 180 ORDER BY d.popularity DESC LIMIT 50`
- Albums in a subgenre (use the album_subgenres view, not subgenres_albums):
  `SELECT d.artist, d.album, d.release_date FROM album_subgenres x JOIN album_details d ON d.album_id = x.album_id WHERE x.subgenre = 'Baroque' ORDER BY d.release_date DESC`
- Instrumental songs (no singing) of 2-4 minutes in the Cinematic collection:
  `SELECT d.artist, d.album, s.track_no, s.name AS song, s.duration FROM songs s JOIN song_vocals v ON v.song_id = s.song_id JOIN album_details d ON d.album_id = s.album_id JOIN collections_albums ca ON ca.album_id = s.album_id JOIN collections c ON c.collections_id = ca.collection_id WHERE c.name = 'Cinematic' AND coalesce(v.vocals, '') <> 'y' AND s.duration BETWEEN 120 AND 240 ORDER BY d.popularity DESC LIMIT 50`
- Calm, uplifting instrumental songs for a corporate video (moods and uses from album_ai), 2-4 minutes:
  `SELECT d.artist, d.album, s.track_no, s.name AS song, s.duration, ai.energy, ai.summary FROM songs s JOIN album_ai ai ON ai.album_id = s.album_id JOIN song_vocals v ON v.song_id = s.song_id JOIN album_details d ON d.album_id = s.album_id WHERE (', ' || ai.uses || ',') LIKE '%, corporate,%' AND (', ' || ai.moods || ',') LIKE '%, uplifting,%' AND v.vocals IN ('n','r') AND s.duration BETWEEN 120 AND 240 ORDER BY d.popularity DESC LIMIT 50`
- Fast electronic tracks around 128 BPM with high energy, for a workout video:
  `SELECT d.artist, d.album, s.track_no, s.name AS song, b.bpm FROM songs s JOIN song_bpm b ON b.song_id = s.song_id JOIN album_ai ai ON ai.album_id = s.album_id JOIN album_details d ON d.album_id = s.album_id WHERE d.genres LIKE '%Electronica%' AND b.bpm BETWEEN 124 AND 132 AND ai.energy >= 4 ORDER BY d.popularity DESC LIMIT 50`
- Steady-beat songs around 90 BPM in a minor key with piano, under 3 minutes:
  `SELECT d.artist, d.album, s.track_no, s.name AS song, x.tempo, x.musical_key, x.audio_instruments FROM songs s JOIN song_audio x ON x.song_id = s.song_id JOIN album_details d ON d.album_id = s.album_id WHERE x.beats_conf >= 1.5 AND x.tempo BETWEEN 85 AND 95 AND x.musical_key LIKE '% minor' AND (', ' || x.audio_instruments || ',') LIKE '%, piano,%' AND s.duration < 180 ORDER BY d.popularity DESC LIMIT 50`
- Albums per country:
  `SELECT country, count(*) AS albums FROM album_details GROUP BY country ORDER BY albums DESC`
- Albums in a collection (moods, instruments, uses, composers):
  `SELECT d.artist, d.album FROM collections c JOIN collections_albums ca ON ca.collection_id = c.collections_id JOIN album_details d ON d.album_id = ca.album_id WHERE c.name = 'Background Mix'`

To use what you find: each `sku` works with `/api/albums/{sku}` (tracks, covers, credit lines); clean audio needs a membership (see "Paying from an agent").
