From ba28c9e7b656fad77381d7bcd2f5f66e63abdec8 Mon Sep 17 00:00:00 2001 From: Rina4M Date: Sat, 25 Nov 2023 17:10:14 +0100 Subject: [PATCH] Irina Markova --- your-code/solution.sql | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 your-code/solution.sql diff --git a/your-code/solution.sql b/your-code/solution.sql new file mode 100644 index 0000000..57eff7d --- /dev/null +++ b/your-code/solution.sql @@ -0,0 +1,37 @@ +CREATE DATABASE publications; +USE publications; + +-- Challenge 1 - Who Have Published What At Where? +SELECT authors.au_id, authors.au_lname, authors.au_fname, titles.title, publishers.pub_name +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, authors.au_lname, authors.au_fname, publishers.pub_name, COUNT(titles.title_id) 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 - Top 3 Best Selling Authors + +SELECT authors.au_id, authors.au_lname, authors.au_fname, COUNT(sales.title_id) AS total +FROM authors +JOIN titleauthor ON authors.au_id = titleauthor.au_id +LEFT JOIN sales ON titleauthor.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, authors.au_lname, authors.au_fname, COALESCE(COUNT(sales.title_id), 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, authors.au_lname, authors.au_fname +ORDER BY total DESC;