Created
May 10, 2016 17:42
-
-
Save joshuastr/0e9ef40e4a5d8a133bfccdee8d69a0a4 to your computer and use it in GitHub Desktop.
SQL Exercise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT isbn FROM editions | |
where publisher_id = 59 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT authors.id, (authors.first_name || ' ' || authors.last_name) AS "Name", COUNT(authors.id) FROM authors | |
join books on books.author_id = authors.id | |
GROUP BY authors.id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT books.title, editions.isbn from books join editions | |
on books.id = editions.book_id | |
where editions.publisher_id = 59; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT books.title, editions.isbn, stock.retail, stock.stock FROM books | |
JOIN editions on books.id = editions.book_id | |
JOIN stock on stock.isbn = editions.isbn | |
WHERE editions.publisher_id = 59; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT books.title, editions.isbn, stock.retail, stock.stock from books | |
join editions on books.id = editions.book_id | |
join stock on stock.isbn = editions.isbn | |
where editions.publisher_id = 59 and stock.stock > 0; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT books.title, editions.isbn, stock.retail, stock.stock, | |
CASE WHEN editions.type = 'h' then 'hardcover' | |
WHEN editions.type = 'p' then 'paperback' | |
END AS type | |
FROM books | |
JOIN editions on books.id = editions.book_id | |
JOIN stock on stock.isbn = editions.isbn | |
WHERE editions.publisher_id = 59 and stock.stock > 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT books.title, editions.publication | |
FROM books | |
join editions on books.id = editions.book_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT sum(stock.stock) from stock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT round(avg(stock.retail),2) AS "Average Retail", round(avg(stock.cost),2) AS "Average Cost", round(avg(stock.retail),2) - round(avg(stock.cost),2) as "Average Profit" | |
from stock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT editions.book_id, stock.stock FROM stock | |
join editions on stock.isbn = editions.isbn | |
ORDER BY stock.stock DESC | |
LIMIT 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment