Skip to content

Instantly share code, notes, and snippets.

@krokrob
Created October 24, 2019 16:02
Show Gist options
  • Save krokrob/07bac4a286ad94e0d24c53933e3b6c53 to your computer and use it in GitHub Desktop.
Save krokrob/07bac4a286ad94e0d24c53933e3b6c53 to your computer and use it in GitHub Desktop.
# List all customers (name + email), ordered alphabetically (no extra information)
SELECT first_name, last_name, email
FROM customers
ORDER BY last_name ASC;
# List tracks (Name + Composer) of the Classical playlist
SELECT t.name, t.composer
FROM tracks t
JOIN playlist_tracks pt ON t.id = pt.track_id
JOIN playlists p ON p.id = pt.playlist_id
WHERE p.name = 'Classical';
# List the 10 artists mostly listed in all playlists
SELECT ar.id, ar.name, COUNT(pt.track_id) AS c
FROM artists ar
JOIN albums al ON ar.id = al.artist_id
JOIN tracks t ON al.id = t.album_id
JOIN playlist_tracks pt ON t.id = pt.track_id
GROUP BY ar.id
ORDER BY c DESC
LIMIT 10;
# List the tracks which have been purchased at least twice, ordered by number of purchases
SELECT t.id, t.name, COUNT(il.id) AS c
FROM tracks t
JOIN invoice_lines il ON t.id = il.track_id
GROUP BY t.id
HAVING c >= 2
ORDER BY c DESC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment