From 97db94aa33d3bdfbf6173c37fcb0a166f3e73bb2 Mon Sep 17 00:00:00 2001 From: Ekraj Pokhrel Date: Sun, 23 Apr 2023 20:42:05 +0200 Subject: [PATCH 1/2] challenge 1 and 3 are done using temporary table, challenge 2 need to do with derived table which is quiet messy for now --- your-code/solutions.sql | 64 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/your-code/solutions.sql b/your-code/solutions.sql index e69de29..431847a 100644 --- a/your-code/solutions.sql +++ b/your-code/solutions.sql @@ -0,0 +1,64 @@ +# Use database publication: +USE publication; +# List all tables in publication database: +SHOW TABLES; + + +# Challenge 1 - Most Profiting Authors +#This challenge is solved using temorary talble: +# Calculate the royalty of each sale for each author +CREATE TEMPORARY TABLE publication.sales_royalty +SELECT titles.title_id, + authors.au_id, + sales.ord_num, + sales.qty, + (titles.price * sales.qty * titles.royalty / 100 *titleauthor.royaltyper / 100) AS royalty_per_sales +FROM authors + INNER JOIN titleauthor + ON authors.au_id = titleauthor.au_id + INNER JOIN titles + ON titleauthor.title_id = titles.title_id + INNER JOIN sales + ON sales.title_id = titles.title_id +GROUP BY titles.title_id, authors.au_id, authors.au_fname, sales.ord_num, sales.qty; + +# Using the output from Step 1 as a temp table, aggregate the total royalties for each title for each author. + +CREATE TEMPORARY TABLE total_royalty +SELECT title_id, au_id, SUM(royalty_per_sales) AS royalty_per_title +FROM sales_royalty +GROUP BY title_id, au_id, au_fname, au_lname; + + +# Using the output from Step 2 as a temp table, calculate the total profits of each author +# by aggregating the advances and total royalties of each title. +SELECT au_id, sum(royalty_per_title + titles.advance) AS aggregate_royalty +FROM title_royalty +INNER JOIN titles + ON title_royalty.title_id = titles.title_id +GROUP BY au_id, au_fname, au_lname +ORDER BY aggregate_royalty DESC +LIMIT 3; + + + +## Challenge 2 - Alternative Solution +# Solutions using derived table + + +## Challenge 3 -->> most_profiting_authors + +CREATE TABLE publication.most_profiting_authors AS SELECT +au_id, sum(royalty_per_title + titles.advance) AS aggregate_royalty +FROM title_royalty +INNER JOIN titles + ON title_royalty.title_id = titles.title_id +GROUP BY au_id, au_fname, au_lname +ORDER BY aggregate_royalty DESC; + +# wiew the table most_profiting_authors +SELECT * FROM publication.most_profiting_authors; + +# add foreign key to table to make relations with the database: +ALTER TABLE publication.most_profiting_authors +ADD FOREIGN KEY (au_id) REFERENCEs authors(au_id); From 8155d4720470d3f48635f27ddcdc26c5a78233fc Mon Sep 17 00:00:00 2001 From: Ekraj Pokhrel Date: Sun, 23 Apr 2023 23:27:08 +0200 Subject: [PATCH 2/2] all solved --- your-code/solutions.sql | 45 ++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/your-code/solutions.sql b/your-code/solutions.sql index 431847a..dd8f26f 100644 --- a/your-code/solutions.sql +++ b/your-code/solutions.sql @@ -13,12 +13,12 @@ SELECT titles.title_id, sales.ord_num, sales.qty, (titles.price * sales.qty * titles.royalty / 100 *titleauthor.royaltyper / 100) AS royalty_per_sales -FROM authors - INNER JOIN titleauthor +FROM publication.authors + INNER JOIN publication.titleauthor ON authors.au_id = titleauthor.au_id - INNER JOIN titles + INNER JOIN publication.titles ON titleauthor.title_id = titles.title_id - INNER JOIN sales + INNER JOIN publication.sales ON sales.title_id = titles.title_id GROUP BY titles.title_id, authors.au_id, authors.au_fname, sales.ord_num, sales.qty; @@ -27,16 +27,16 @@ GROUP BY titles.title_id, authors.au_id, authors.au_fname, sales.ord_num, sales. CREATE TEMPORARY TABLE total_royalty SELECT title_id, au_id, SUM(royalty_per_sales) AS royalty_per_title FROM sales_royalty -GROUP BY title_id, au_id, au_fname, au_lname; +GROUP BY title_id, au_id; # Using the output from Step 2 as a temp table, calculate the total profits of each author # by aggregating the advances and total royalties of each title. SELECT au_id, sum(royalty_per_title + titles.advance) AS aggregate_royalty -FROM title_royalty +FROM total_royalty INNER JOIN titles - ON title_royalty.title_id = titles.title_id -GROUP BY au_id, au_fname, au_lname + ON total_royalty.title_id = titles.title_id +GROUP BY au_id ORDER BY aggregate_royalty DESC LIMIT 3; @@ -44,16 +44,37 @@ LIMIT 3; ## Challenge 2 - Alternative Solution # Solutions using derived table +# Here in this solutions one derived table royalty_per_title is created and inner +#join with the titles table to get aggregated royalty per author +SELECT royalty_per_title.au_id, + SUM(per_title_royalty + titles.advance) AS total_royalty +FROM publication.titles +INNER JOIN ( + SELECT titles.title_id, + authors.au_id, + SUM(titles.price * sales.qty * titles.royalty / 100 *titleauthor.royaltyper / 100) AS per_title_royalty + FROM publication.authors + INNER JOIN publication.titleauthor + ON authors.au_id = titleauthor.au_id + INNER JOIN publication.titles + ON titleauthor.title_id = titles.title_id + INNER JOIN publication.sales + ON sales.title_id = titles.title_id + GROUP BY titles.title_id, authors.au_id) AS royalty_per_title +ON titles.title_id = royalty_per_title.title_id +GROUP BY royalty_per_title.au_id +ORDER BY total_royalty DESC +LIMIT 3; ## Challenge 3 -->> most_profiting_authors CREATE TABLE publication.most_profiting_authors AS SELECT au_id, sum(royalty_per_title + titles.advance) AS aggregate_royalty -FROM title_royalty -INNER JOIN titles - ON title_royalty.title_id = titles.title_id -GROUP BY au_id, au_fname, au_lname +FROM total_royalty +INNER JOIN publication.titles + ON total_royalty.title_id = titles.title_id +GROUP BY au_id ORDER BY aggregate_royalty DESC; # wiew the table most_profiting_authors