Last active
July 19, 2016 01:25
-
-
Save shinobcrc/1d682a7f30866d8f8808f814732fdfd8 to your computer and use it in GitHub Desktop.
Bookstore-SQL-Assigment
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.isbn AS "ISBN" | |
FROM editions | |
INNER JOIN books ON (books.id = editions.book_id) | |
INNER JOIN publishers ON (editions.publisher_id = publishers.id) | |
WHERE publishers.name = 'Random House'; |
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 AS "Author ID", | |
concat(authors.first_name, ' ', authors.last_name) AS "Author name", | |
count(books.author_id = authors.id) AS "No. of books" | |
FROM | |
books | |
INNER JOIN | |
authors ON (books.author_id = authors.id) | |
GROUP BY | |
authors.id | |
ORDER BY | |
count(books.author_id = authors.id) DESC | |
; |
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 AS "Author ID", | |
concat(authors.first_name, ' ', authors.last_name) AS "Author name", | |
count(books.author_id = authors.id) AS "No. of books" | |
FROM | |
books | |
INNER JOIN | |
authors ON (books.author_id = authors.id) | |
GROUP BY | |
authors.id | |
ORDER BY | |
count(books.author_id = authors.id) DESC | |
; |
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 | |
hardcopy.title | |
FROM | |
( | |
SELECT | |
books.title | |
FROM | |
editions | |
INNER JOIN | |
books ON (editions.book_id = books.id) | |
WHERE | |
editions.type = 'h' | |
) AS hardcopy | |
INNER JOIN | |
( | |
SELECT | |
books.title | |
FROM | |
editions | |
INNER JOIN | |
books ON (editions.book_id = books.id) | |
WHERE | |
editions.type = 'p' | |
) AS paperback | |
ON (hardcopy.title = paperback.title) | |
; |
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 | |
publishers.name AS "Publisher", | |
avg(stock.retail) AS "Average retail price", | |
count(editions.edition) | |
FROM | |
publishers | |
LEFT OUTER JOIN | |
editions ON (publishers.id = editions.publisher_id) | |
LEFT OUTER JOIN | |
stock ON (editions.isbn = stock.isbn) | |
GROUP BY | |
publishers.name | |
ORDER BY | |
count(editions.edition) DESC | |
; |
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.isbn AS "ISBN", | |
books.title AS "Title" | |
FROM editions | |
INNER JOIN books ON (books.id = editions.book_id) | |
INNER JOIN publishers ON (editions.publisher_id = publishers.id) | |
WHERE publishers.name = 'Random House'; |
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.isbn AS "ISBN", | |
books.title AS "Title", | |
stock.stock AS "Item is in stock", | |
stock.retail AS "Item's retail value" | |
FROM editions | |
INNER JOIN books ON (books.id = editions.book_id) | |
INNER JOIN publishers ON (editions.publisher_id = publishers.id) | |
INNER JOIN stock ON (editions.isbn = stock.isbn) | |
WHERE publishers.name = 'Random House'; |
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.isbn AS "ISBN", | |
books.title AS "Title", | |
stock.stock AS "Item is in stock", | |
stock.retail AS "Item's retail value" | |
FROM editions | |
INNER JOIN books ON (books.id = editions.book_id) | |
INNER JOIN publishers ON (editions.publisher_id = publishers.id) | |
INNER JOIN stock ON (editions.isbn = stock.isbn) | |
WHERE publishers.name = 'Random House' 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 AS "Title", | |
editions.publication AS "Date of Publication" | |
FROM books | |
LEFT OUTER 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 | |
books.title AS "Title", | |
editions.publication AS "Date of publication" | |
FROM books | |
LEFT OUTER 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 | |
avg(stock.cost) AS "Average cost", | |
avg(stock.retail) AS "Average retail price", | |
avg(stock.retail / stock.cost * 100 - 100) 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 | |
books.title, | |
stock.stock | |
FROM | |
stock, | |
editions, | |
books | |
WHERE | |
(stock.isbn = editions.isbn) AND | |
(editions.book_id = books.id) | |
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
Hi Shino, here are some comments on your sql code:
Give me a shout if you have any questions!
-Scott