Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files via upload #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
48 changes: 48 additions & 0 deletions solution2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
-- Challenge 1 - Author, Title, Publisher Info
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;

-- Challenge 2 - Title Count per Author per Publisher
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;

-- Challenge 3 - Top 3 Best-Selling Authors
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;

-- Challenge 4 - Best Selling Authors Ranking (Including Zero Sales)
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;