From 4c001f1781e7c6c67ebc70f131d206a5cec79bd2 Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Fri, 25 Feb 2022 14:30:28 -0600 Subject: [PATCH] saving work on SQL interlude problems, problem 16 complete so far --- invoice_37_line_item_count.sql | 4 ++++ line_item_track.sql | 10 ++++++++++ line_item_track_artist.sql | 3 +++ line_items_per_invoice.sql | 10 ++++++++++ playlists_track_count.sql | 6 ++++++ project_notes.md | 36 ++++++++++++++++++++++++++++++++++ total_sales_{year}.sql | 8 ++++++++ tracks_no_id.sql | 11 +++++++++++ 8 files changed, 88 insertions(+) create mode 100644 invoice_37_line_item_count.sql create mode 100644 line_item_track.sql create mode 100644 line_item_track_artist.sql create mode 100644 line_items_per_invoice.sql create mode 100644 playlists_track_count.sql create mode 100644 project_notes.md create mode 100644 total_sales_{year}.sql create mode 100644 tracks_no_id.sql diff --git a/invoice_37_line_item_count.sql b/invoice_37_line_item_count.sql new file mode 100644 index 0000000..4e1e13c --- /dev/null +++ b/invoice_37_line_item_count.sql @@ -0,0 +1,4 @@ +SELECT COUNT(InvoiceLineId) +FROM InvoiceLine +WHERE InvoiceId = 37; + diff --git a/line_item_track.sql b/line_item_track.sql new file mode 100644 index 0000000..0a63f43 --- /dev/null +++ b/line_item_track.sql @@ -0,0 +1,10 @@ +SELECT InvoiceLine.*, Track.Name FROM InvoiceLine AS Track_Name +INNER JOIN Invoice +ON Invoice.InvoiceId = InvoiceLine.InvoiceId +INNER JOIN Track +ON InvoiceLine.TrackId = Track.TrackId +GROUP BY Invoice.InvoiceId; + +SELECT InvoiceLine.*, t.Name +FROM InvoiceLine +JOIN Track t on InvoiceLine.TrackId = t.TrackId; \ No newline at end of file diff --git a/line_item_track_artist.sql b/line_item_track_artist.sql new file mode 100644 index 0000000..3d7ea80 --- /dev/null +++ b/line_item_track_artist.sql @@ -0,0 +1,3 @@ +SELECT InvoiceLine.*, t.Name, t.Composer +FROM InvoiceLine +JOIN Track t on InvoiceLine.TrackId = t.TrackId; \ No newline at end of file diff --git a/line_items_per_invoice.sql b/line_items_per_invoice.sql new file mode 100644 index 0000000..9f457b7 --- /dev/null +++ b/line_items_per_invoice.sql @@ -0,0 +1,10 @@ +/* My theory */ +SELECT COUNT(InvoiceLineId) +FROM InvoiceLine +GROUP BY InvoiceLine.InvoiceId; + +/* Correct Answer */ +select i.InvoiceId, count(il.invoiceId) line_item_count +from Invoice i +join InvoiceLine il on i.InvoiceId = il.InvoiceId +group by il.InvoiceId; \ No newline at end of file diff --git a/playlists_track_count.sql b/playlists_track_count.sql new file mode 100644 index 0000000..3bc25da --- /dev/null +++ b/playlists_track_count.sql @@ -0,0 +1,6 @@ +SELECT Playlist.Name, COUNT(PlaylistTrack.TrackId) AS Playlist_Track_Count FROM Playlist +INNER JOIN PlaylistTrack +ON Playlist.PlaylistId = PlaylistTrack.PlaylistId +GROUP BY PlaylistTrack.PlaylistId; + +-- WHERE was messing up the results, GROUP BY was the appropriate syntax. diff --git a/project_notes.md b/project_notes.md new file mode 100644 index 0000000..531c124 --- /dev/null +++ b/project_notes.md @@ -0,0 +1,36 @@ +# Project Notes - SQL Interlude - Chinook + +## Questions +### How to use NOT in queries +Why does + +``` +SELECT * FROM CUSTOMER +WHERE COUNTRY NOT LIKE 'USA'; +``` + +work and + +``` +SELECT * FROM CUSTOMER +WHERE COUNTRY NOT 'USA'; +``` +does not work? + +### Answer +To avoid using `LIKE`
+ +`WHERE NOT customer.country = "USA";`
+or
+`WHERE NOT != "USA";`
+ +are valid ways of forming the query. + +## Resources +[How to concatenate columns in SQL - CONCAT_WS()](https://www.w3schools.com/sql/func_mysql_concat_ws.asp) + +## New Concepts/Syntax + +SQLite
+`strftime('%Y', InvoiceDate`
+[strftime documentation](https://www.w3resource.com/sqlite/sqlite-strftime.php) \ No newline at end of file diff --git a/total_sales_{year}.sql b/total_sales_{year}.sql new file mode 100644 index 0000000..e8a0ecc --- /dev/null +++ b/total_sales_{year}.sql @@ -0,0 +1,8 @@ +SELECT strftime('%Y', InvoiceDate) as Year, + SUM(TOTAL) as YearlyTotal +FROM Invoice +WHERE strftime('%Y', InvoiceDate) = '2009' + OR strftime('%Y', InvoiceDate) = '2011' +GROUP BY strftime('%Y', InvoiceDate); + +SELECT * FROM Invoice; \ No newline at end of file diff --git a/tracks_no_id.sql b/tracks_no_id.sql new file mode 100644 index 0000000..cc1641d --- /dev/null +++ b/tracks_no_id.sql @@ -0,0 +1,11 @@ +SELECT Track.Name AS Track_Name, + Album.Title AS Album_Title, + MediaType.Name AS Media_Type, + Genre.Name AS Genre +From Track +INNER JOIN Album +ON Track.AlbumId = Album.AlbumId +INNER JOIN MediaType +ON Track.MediaTypeId = MediaType.MediaTypeId +INNER JOIN Genre +ON Track.GenreId = Genre.GenreId; \ No newline at end of file