From e93890ffc4827c1814f061eeaf888203bf97b0b2 Mon Sep 17 00:00:00 2001 From: whitz22 Date: Sat, 25 Nov 2023 17:11:38 +0100 Subject: [PATCH] *updated* --- your-code/solutions.sql | 75 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 your-code/solutions.sql diff --git a/your-code/solutions.sql b/your-code/solutions.sql new file mode 100644 index 0000000..5737d7c --- /dev/null +++ b/your-code/solutions.sql @@ -0,0 +1,75 @@ +USE publications; +SHOW TABLES; +SELECT * FROM authors; +SELECT * FROM publishers; + +## Challenge 1 - Who Have Published What At Where? +SELECT authors.au_id AS 'AUTHOR ID', +authors.au_lname AS 'LAST NAME', +authors.au_fname AS 'FIRST NAME', +titles.title AS TITLE, +publishers.pub_name AS PUBLISHER +FROM authors +JOIN + titleauthor ON authors.au_id = titleauthor.au_id +JOIN + titles ON titleauthor.title_id = titles.title_id +JOIN + publishers ON titles.pub_id = publishers.pub_id; + + +#Challenge 2 -Who Have Published How Many At Where? +SELECT authors.au_id AS 'AUTHOR ID', + authors.au_lname AS 'LAST NAME', + authors.au_fname AS 'FIRST NAME', + publishers.pub_name AS PUBLISHER, +COUNT(titles.title) AS 'TITLE COUNT' +FROM authors +JOIN + titleauthor ON authors.au_id = titleauthor.au_id +JOIN + titles ON titleauthor.title_id = titles.title_id +JOIN + publishers ON titles.pub_id = publishers.pub_id +GROUP BY + authors.au_id, authors.au_lname, authors.au_fname, publishers.pub_name; + +#Challenge 3 Who are the top 3 authors who have sold the highest number of titles? +SELECT + authors.au_id AS 'AUTHOR ID', + authors.au_lname AS 'LAST NAME', + authors.au_fname AS 'FIRST NAME', + SUM(sales.qty) AS 'TOTAL' +FROM + authors +JOIN + titleauthor ON authors.au_id = titleauthor.au_id +JOIN + titles ON titleauthor.title_id = titles.title_id +LEFT JOIN + sales ON titles.title_id = sales.title_id +GROUP BY + authors.au_id, authors.au_lname, authors.au_fname +ORDER BY + TOTAL DESC +LIMIT 3; + +#Challenge 4 - Best Selling Authors Ranking +SELECT + authors.au_id AS 'AUTHOR ID', + authors.au_lname AS 'LAST NAME', + authors.au_fname AS 'FIRST NAME', + COALESCE(SUM(sales.qty), 0) AS 'TOTAL' +FROM + authors +LEFT JOIN + titleauthor ON authors.au_id = titleauthor.au_id +LEFT JOIN + titles ON titleauthor.title_id = titles.title_id +LEFT JOIN + sales ON titles.title_id = sales.title_id +GROUP BY + authors.au_id, authors.au_lname, authors.au_fname +ORDER BY + TOTAL DESC; +