diff --git a/solution.sql b/solution.sql new file mode 100644 index 0000000..73bc2af --- /dev/null +++ b/solution.sql @@ -0,0 +1,39 @@ +CREATE DATABASE publications; + +USE publications; +/* Challenge 1 */ +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 */ +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_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, publishers.pub_name +ORDER BY TITLE_COUNT DESC; + + /* Challenge 3 */ +SELECT authors.au_id AS AUTHOR_ID, authors.au_lname AS LAST_NAME, authors.au_fname AS FIRST_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 +GROUP BY authors.au_id, authors.au_lname, authors.au_fname +ORDER BY TITLE_COUNT DESC +LIMIT 3; + +/* Challenge 4 */ +SELECT authors.au_id AS AUTHOR_ID, authors.au_lname AS LAST_NAME, authors.au_fname AS FIRST_NAME, +COUNT(titles.title_id) AS TOTAL +FROM authors +LEFT JOIN titleauthor ON authors.au_id = titleauthor.au_id +LEFT JOIN titles ON titleauthor.title_id = titles.title_id +GROUP BY authors.au_id, authors.au_lname, authors.au_fname +ORDER BY TOTAL DESC; +