From 6b950437280a938880d2b139b44ef946b1c96b5c Mon Sep 17 00:00:00 2001 From: Sanober Khoso Date: Sat, 25 Nov 2023 22:47:46 +0100 Subject: [PATCH] SanoberKhoso --- your-code/solutions.sql | 97 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 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..273f1d3 --- /dev/null +++ b/your-code/solutions.sql @@ -0,0 +1,97 @@ +-- Challenge 1 ------------------------- + +USE publications; + +SELECT + authors.au_id AS `Author Id`, + au_lname AS `Last Name`, + au_fname AS `First Name`, + titles.title AS `Title`, + pub_name AS `Publisher` +FROM + authors + INNER JOIN + titleauthor ON authors.au_id = titleauthor.au_id + INNER JOIN + titles ON titleauthor.title_id = titles.title_id + INNER JOIN + publishers ON titles.pub_id = publishers.pub_id + + +-- Challenge 2 - Solution 1 ---------------------------- +USE publications; + +SELECT + authors.au_id AS `Author Id`, + au_lname AS `Last Name`, + au_fname AS `First Name`, + pub_name AS `Publisher`, + COUNT(titles.title_id) AS `Title Count` +FROM + authors + INNER JOIN + titleauthor ON authors.au_id = titleauthor.au_id + INNER JOIN + titles ON titleauthor.title_id = titles.title_id + INNER JOIN + publishers ON titles.pub_id = publishers.pub_id +GROUP BY authors.au_id , au_lname , au_fname , pub_name + + +-- Challenge 2 - Solution 2 using subquery -------- +USE publications; + +SELECT + `Author Id`, + `Last Name`, + `First Name`, + `Publisher`, + COUNT(`Title`) AS `Title Count` +FROM + (SELECT + authors.au_id AS `Author Id`, + au_lname AS `Last Name`, + au_fname AS `First Name`, + titles.title AS `Title`, + pub_name AS `Publisher` + FROM + authors + INNER JOIN titleauthor ON authors.au_id = titleauthor.au_id + INNER JOIN titles ON titleauthor.title_id = titles.title_id + INNER JOIN publishers ON titles.pub_id = publishers.pub_id) t +GROUP BY `Author Id` , `Last Name` , `First Name` , `Publisher` + +-- Challenge 3 ----------- +USE publications; + +SELECT + authors.au_id AS `Author Id`, + au_lname AS `Last Name`, + au_fname AS `First Name`, + SUM(qty) AS `Total` +FROM + authors + INNER JOIN + titleauthor ON authors.au_id = titleauthor.au_id + INNER JOIN + sales ON titleauthor.title_id = sales.title_id +GROUP BY authors.au_id , au_lname , au_fname +ORDER BY `Total` DESC +LIMIT 3 + +-- Challenge 4 ----------------- +USE publications; + +SELECT + authors.au_id AS `Author Id`, + au_lname AS `Last Name`, + au_fname AS `First Name`, + IFNULL(SUM(qty), 0) AS `Total` +FROM + authors + LEFT JOIN + titleauthor ON authors.au_id = titleauthor.au_id + LEFT JOIN + sales ON titleauthor.title_id = sales.title_id +GROUP BY authors.au_id , au_lname , au_fname +ORDER BY `Total` DESC