Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions your-code/solutions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
-- Challenge 1

SELECT au.au_id, au.au_lname, au.au_fname, t.title, p.pub_name
FROM publications.authors au
LEFT JOIN publications.titleauthor ta ON au.au_id = ta.au_id
LEFT JOIN publications.titles t ON ta.title_id = t.title_id
LEFT JOIN publications.publishers p ON t.pub_id = p.pub_id
WHERE t.title IS NOT NULL
ORDER BY au.au_id ASC;

SELECT *
FROM publications.titleauthor;

-- Challenge 2
SELECT au.au_id, au.au_lname, au.au_fname, p.pub_name, COUNT(p.pub_name) AS 'title_count'
FROM publications.authors au
LEFT JOIN publications.titleauthor ta ON au.au_id = ta.au_id
LEFT JOIN publications.titles t ON ta.title_id = t.title_id
LEFT JOIN publications.publishers p ON t.pub_id = p.pub_id
WHERE t.title IS NOT NULL
GROUP BY au.au_lname, au.au_fname, p.pub_name
ORDER BY au.au_id DESC;

-- Challenge 3
SELECT au.au_id, au.au_lname, au.au_fname, SUM(t.ytd_sales) AS 'Total_Sales'
FROM publications.authors au
LEFT JOIN publications.titleauthor ta ON au.au_id = ta.au_id
LEFT JOIN publications.titles t ON ta.title_id = t.title_id
LEFT JOIN publications.publishers p ON t.pub_id = p.pub_id
WHERE t.title IS NOT NULL
GROUP BY au.au_id
ORDER BY Total_Sales DESC
LIMIT 3;

-- Challenge 4
SELECT au.au_id, au.au_lname, au.au_fname, SUM(t.ytd_sales) AS 'Total_Sales'
FROM publications.authors au
LEFT JOIN publications.titleauthor ta ON au.au_id = ta.au_id
LEFT JOIN publications.titles t ON ta.title_id = t.title_id
LEFT JOIN publications.publishers p ON t.pub_id = p.pub_id
WHERE t.title IS NOT NULL
GROUP BY au.au_id
ORDER BY Total_Sales DESC;

-- Bonus Challenge
SELECT au.au_id, au.au_lname, au.au_fname, SUM(t.royalty*(ta.royaltyper/100)) AS 'Royalty per Sale', (SUM('Royalty per Sale') + SUM(t.advance)) AS 'Profit'
FROM publications.authors au
LEFT JOIN publications.titleauthor ta ON au.au_id = ta.au_id
LEFT JOIN publications.titles t ON ta.title_id = t.title_id
LEFT JOIN publications.publishers p ON t.pub_id = p.pub_id
WHERE t.title IS NOT NULL
GROUP BY au.au_id
ORDER BY Profit DESC
LIMIT 3;