Author SHA1 Message Date
JMR-dev 4c001f1781 saving work on SQL interlude problems, problem 16 complete so far 2022-02-25 14:30:28 -06:00
8 changed files with 88 additions and 0 deletions
+4
View File
@@ -0,0 +1,4 @@
SELECT COUNT(InvoiceLineId)
FROM InvoiceLine
WHERE InvoiceId = 37;
+10
View File
@@ -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;
+3
View File
@@ -0,0 +1,3 @@
SELECT InvoiceLine.*, t.Name, t.Composer
FROM InvoiceLine
JOIN Track t on InvoiceLine.TrackId = t.TrackId;
+10
View File
@@ -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;
+6
View File
@@ -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.
+36
View File
@@ -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` <br/>
`WHERE NOT customer.country = "USA";` <br/>
or <br/>
`WHERE NOT != "USA";` <br/>
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 <br/>
`strftime('%Y', InvoiceDate` <br/>
[strftime documentation](https://www.w3resource.com/sqlite/sqlite-strftime.php)
+8
View File
@@ -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;
+11
View File
@@ -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;