-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chinook2-7.sql
73 lines (59 loc) · 1.43 KB
/
Chinook2-7.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
-- 2. Ile mamy rekordow w tabelach Artist i Album?
USE Chinook
GO
SELECT COUNT(*)
FROM Artist
SELECT COUNT(*)
FROM Album
--3. Polacz tabele Artist i Album – wyswietl rowniez Artystow bez albumow. Ile jest Artystow bez albumow?
USE Chinook
GO
SELECT COUNT(*) AS "ALL ARTISTS"
FROM Album AL
RIGHT JOIN Artist AR
ON AL.ArtistId = AR.ArtistId
SELECT COUNT(*) AS "ARTISTS WITHOUT ALBUM"
FROM Album AL
RIGHT JOIN Artist AR
ON AL.ArtistId = AR.ArtistId
WHERE AL.ArtistId IS NULL
-- 4. Wyswietl wszystkich artystow ktorzy maja dokladnie 4 albumy
USE Chinook
GO
SELECT AR.Name, COUNT(AL.AlbumId)
FROM Album AL
JOIN Artist AR
ON AL.ArtistId = AR.ArtistId
GROUP BY AR.Name
HAVING COUNT(AL.AlbumId)=4
-- 5. Ilu Customerów pochodzi z Germany?
USE Chinook
GO
SELECT COUNT(*) AS 'CUSTOMERS FROM GERMANY'
FROM Customer
WHERE Country = 'Germany'
-- 6. ktorzy customerzy wydaja najwiecej pieniedzy?
USE Chinook
GO
SELECT C.LastName, C.FirstName, SUM(I.Total)
FROM Customer C
JOIN Invoice I
ON I.CustomerId = C.CustomerId
GROUP BY C.LastName, C.FirstName
ORDER BY SUM(I.Total) DESC
-- 7. Ktore Track sa najczesciej kupowane?
USE Chinook
GO
SELECT T.Name, T.TrackId, SUM(I.Quantity)
FROM InvoiceLine I
JOIN Track T
ON T.TrackId = I.TrackId
GROUP BY T.Name, T.TrackId
ORDER BY SUM(I.Quantity) DESC
-- 8. (optional) jakiego Artysty utwory sa najczesciej kupowane w danym kraju?
USE Chinook
GO
SELECT AR.Name
FROM Artist AR
JOIN Album AL
ON AL.ArtistId = AR.ArtistId