From 640336bdb10d0a345a9ec551b636049b5fa6b576 Mon Sep 17 00:00:00 2001 From: Alex Husiev Date: Tue, 17 Sep 2024 01:20:54 +0200 Subject: [PATCH 1/2] Husiev --- your-code/solutions.sql | 102 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 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..dc4ba31 --- /dev/null +++ b/your-code/solutions.sql @@ -0,0 +1,102 @@ +/* Challenge 1 - Who Have Published What At Where? */ +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 + titleauthor ta +JOIN + authors a ON ta.au_id = a.au_id -- Corrected to use ta.au_id +JOIN + titles t ON ta.title_id = t.title_id +JOIN + publishers p ON t.pub_id = p.pub_id; -- Joining on pub_id + +/*Compare nuymber of rows in titleauthor vs my output */ +SELECT COUNT(*) AS total_records FROM titleauthor; + +/*My oputput */ +SELECT COUNT(*) AS total_output_rows +FROM + titleauthor ta +JOIN + authors a ON ta.au_id = a.au_id +JOIN + titles t ON ta.title_id = t.title_id +JOIN + publishers p ON t.pub_id = p.pub_id; + + +/*Challenge 2 - Who Have Published How Many At Where? */ +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) AS 'TOTAL TITLES' +FROM + titleauthor ta +JOIN + authors a ON ta.au_id = a.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, p.pub_name +ORDER BY + a.au_lname, p.pub_name; + +SELECT SUM(total_titles) AS 'TOTAL TITLE COUNT' +FROM ( + SELECT COUNT(t.title) AS total_titles + FROM titleauthor ta + JOIN authors a ON ta.au_id = a.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, p.pub_name +) AS title_counts; + +SELECT COUNT(*) AS total_records FROM titleauthor; + +/*Challenge 3 - Best Selling Authors */ +SELECT + a.au_id AS 'AUTHOR ID', + a.au_lname AS 'LAST NAME', + a.au_fname AS 'FIRST NAME', + COUNT(t.title) AS 'TOTAL' +FROM + titleauthor ta +JOIN + authors a ON ta.au_id = a.au_id +JOIN + titles t ON ta.title_id = t.title_id +GROUP BY + a.au_id +ORDER BY + COUNT(t.title) DESC +LIMIT 3; + +/*Challenge 4 - Best Selling Authors Ranking */ +SELECT + a.au_id AS 'AUTHOR ID', + a.au_lname AS 'LAST NAME', + a.au_fname AS 'FIRST NAME', + COALESCE(COUNT(t.title), 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 +GROUP BY + a.au_id +ORDER BY + TOTAL DESC; + + + + + From f6305fe01dc87ec85ff20f75f722574346d980db Mon Sep 17 00:00:00 2001 From: Alex Husiev Date: Wed, 18 Sep 2024 01:53:13 +0200 Subject: [PATCH 2/2] Husiev --- your-code/solutions.sql | 46 ++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 30 deletions(-) diff --git a/your-code/solutions.sql b/your-code/solutions.sql index dc4ba31..a262b63 100644 --- a/your-code/solutions.sql +++ b/your-code/solutions.sql @@ -62,39 +62,25 @@ FROM ( SELECT COUNT(*) AS total_records FROM titleauthor; /*Challenge 3 - Best Selling Authors */ -SELECT - a.au_id AS 'AUTHOR ID', - a.au_lname AS 'LAST NAME', - a.au_fname AS 'FIRST NAME', - COUNT(t.title) AS 'TOTAL' -FROM - titleauthor ta -JOIN - authors a ON ta.au_id = a.au_id -JOIN - titles t ON ta.title_id = t.title_id -GROUP BY - a.au_id -ORDER BY - COUNT(t.title) DESC +SELECT a.au_id 'AUTHOR ID', a.au_lname 'LAST NAME', a.au_fname 'FIRST NAME', +SUM(s.qty) 'TOTAL' +FROM authors a +JOIN titleauthor ta USING (au_id) +JOIN titles t USING (title_id) +JOIN sales s USING (title_id) +GROUP BY a.au_id +ORDER BY SUM(s.qty) DESC LIMIT 3; /*Challenge 4 - Best Selling Authors Ranking */ -SELECT - a.au_id AS 'AUTHOR ID', - a.au_lname AS 'LAST NAME', - a.au_fname AS 'FIRST NAME', - COALESCE(COUNT(t.title), 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 -GROUP BY - a.au_id -ORDER BY - TOTAL DESC; +SELECT a.au_id 'AUTHOR ID', a.au_lname 'LAST NAME', a.au_fname 'FIRST NAME', +IFNULL(SUM(s.qty),0) AS 'TOTAL' +FROM authors a +LEFT JOIN titleauthor ta USING (au_id) +LEFT JOIN titles t USING (title_id) +LEFT JOIN sales s USING (title_id) +GROUP BY a.au_id +ORDER BY TOTAL DESC;