-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathlab-sql-select.sql
49 lines (45 loc) · 1.39 KB
/
lab-sql-select.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
SELECT
a.au_id AS "AUTHOR ID",
a.au_lname AS "LAST NAME",
a.au_fname AS "FIRST NAME",
t.title AS "TITLE",
p.pub_name AS "PUBLISHER"
FROM authors a
JOIN titleauthor ta ON a.au_id = ta.au_id
JOIN titles t ON ta.title_id = t.title_id
JOIN publishers p ON t.pub_id = p.pub_id;
SELECT
a.au_id AS "AUTHOR ID",
a.au_lname AS "LAST NAME",
a.au_fname AS "FIRST NAME",
p.pub_name AS "PUBLISHER",
COUNT(t.title_id) AS "TITLE COUNT"
FROM authors a
JOIN titleauthor ta ON a.au_id = ta.au_id
JOIN titles t ON ta.title_id = t.title_id
JOIN publishers p ON t.pub_id = p.pub_id
GROUP BY a.au_id, a.au_lname, a.au_fname, p.pub_name
ORDER BY a.au_id, p.pub_name;
SELECT
a.au_id AS "AUTHOR ID",
a.au_lname AS "LAST NAME",
a.au_fname AS "FIRST NAME",
SUM(s.qty) AS "TOTAL"
FROM authors a
JOIN titleauthor ta ON a.au_id = ta.au_id
JOIN titles t ON ta.title_id = t.title_id
JOIN sales s ON t.title_id = s.title_id
GROUP BY a.au_id, a.au_lname, a.au_fname
ORDER BY TOTAL DESC
LIMIT 3;
SELECT
a.au_id AS "AUTHOR ID",
a.au_lname AS "LAST NAME",
a.au_fname AS "FIRST NAME",
COALESCE(SUM(s.qty), 0) AS "TOTAL"
FROM authors a
LEFT JOIN titleauthor ta ON a.au_id = ta.au_id
LEFT JOIN titles t ON ta.title_id = t.title_id
LEFT JOIN sales s ON t.title_id = s.title_id
GROUP BY a.au_id, a.au_lname, a.au_fname
ORDER BY TOTAL DESC;