Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions my_code.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@

##################### 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;

SELECT COUNT(*)
FROM titleauthor;

##########################################################

##################### 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) 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 ########################

SELECT
authors.au_id AS "Author ID",
authors.au_lname AS "Last Name",
authors.au_fname AS "First Name",
SUM(sales.qty) AS "Total Copies Sold"
FROM
authors
JOIN
titleauthor ON authors.au_id = titleauthor.au_id
JOIN
titles ON titleauthor.title_id = titles.title_id
JOIN
sales ON titles.title_id = sales.title_id
GROUP BY
authors.au_id, authors.au_lname, authors.au_fname
ORDER BY
SUM(sales.qty) DESC
LIMIT 3;

##########################################################

##################### Challenge 4 ########################

SELECT
authors.au_id AS "Author ID",
authors.au_lname AS "Last Name",
authors.au_fname AS "First Name",
SUM(sales.qty) AS "Total Copies Sold"
FROM
authors
JOIN
titleauthor ON authors.au_id = titleauthor.au_id
JOIN
titles ON titleauthor.title_id = titles.title_id
JOIN
sales ON titles.title_id = sales.title_id
GROUP BY
authors.au_id, authors.au_lname, authors.au_fname
ORDER BY
SUM(sales.qty) DESC;

##########################################################
78 changes: 78 additions & 0 deletions x/README-project 1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# lab-mysql-select

<img src="https://bit.ly/2VnXWr2" alt="Ironhack Logo" width="100"/>

# Lab | MySQL Select

## Introduction

In this lab you will practice how to use the MySQL `SELECT` statement which will be extremely useful in your future work as a data analyst/scientist/engineer. **You will use the `publications` database**. You can find it in "Your code" folder. **If you have any trouble importing it to Workbench/Sequel Pro, select latin1 encoding**

You will create a `solutions.sql` file in the `your-code` directory to record your solutions to all challenges.

## Challenge 1 - Who Have Published What At Where?

In this challenge you will write a MySQL `SELECT` query that joins various tables to figure out what titles each author has published at which publishers. Your output should have at least the following columns:

* `AUTHOR ID` - the ID of the author
* `LAST NAME` - author last name
* `FIRST NAME` - author first name
* `TITLE` - name of the published title
* `PUBLISHER` - name of the publisher where the title was published

Your output will look something like below:

![Challenge 1 output](./images/challenge-1.png)

*Note: the screenshot above is not the complete output.*

If your query is correct, the total rows in your output should be the same as the total number of records in Table `titleauthor`.

## Challenge 2 - Who Have Published How Many At Where?

Elevating from your solution in Challenge 1, query how many titles each author has published at each publisher. Your output should look something like below:

![Challenge 2 output](./images/challenge-2.png)

*Note: the screenshot above is not the complete output.*

To check if your output is correct, sum up the `TITLE COUNT` column. The sum number should be the same as the total number of records in Table `titleauthor`.

*Hint: In order to count the number of titles published by an author, you need to use [MySQL COUNT](https://dev.mysql.com/doc/refman/8.0/en/counting-rows.html). Also check out [MySQL Group By](https://dev.mysql.com/doc/refman/8.0/en/group-by-modifiers.html) because you will count the rows of different groups of data. Refer to the references and learn by yourself. These features will be formally discussed in the Temp Tables and Subqueries lesson.*

## Challenge 3 - Best Selling Authors

Who are the top 3 authors who have sold the highest number of titles? Write a query to find out.

Requirements:

* Your output should have the following columns:
* `AUTHOR ID` - the ID of the author
* `LAST NAME` - author last name
* `FIRST NAME` - author first name
* `TOTAL` - total number of titles sold from this author
* Your output should be ordered based on `TOTAL` from high to low.
* Only output the top 3 best selling authors.

## Challenge 4 - Best Selling Authors Ranking

Now modify your solution in Challenge 3 so that the output will display all 23 authors instead of the top 3. Note that the authors who have sold 0 titles should also appear in your output (ideally display `0` instead of `NULL` as the `TOTAL`). Also order your results based on `TOTAL` from high to low.

## Deliverables

* `solution.sql` that contains all your MySQL queries.

## Submission

* Add `solutions.sql` to git.
* Commit your code.
* Push to your fork.
* Create a pull request to the class repo.

## References

[MySQL Reference: Counting Rows](https://dev.mysql.com/doc/refman/8.0/en/counting-rows.html)

[MySQL Reference: Group By](https://dev.mysql.com/doc/refman/8.0/en/group-by-modifiers.html)

[MySQL Reference: SUM Function](https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html#function_sum)
Binary file added x/project 1 - challenge-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added x/project 1-challenge-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
348 changes: 348 additions & 0 deletions x/project1.sql

Large diffs are not rendered by default.