Skip to content

Instantly share code, notes, and snippets.

@ruxandrafed
Created September 14, 2015 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruxandrafed/edc59e17348118a37f5c to your computer and use it in GitHub Desktop.
Save ruxandrafed/edc59e17348118a37f5c to your computer and use it in GitHub Desktop.
SELECT p.isbn FROM editions AS e
INNER JOIN publishers as p ON e.publisher_id=p.id
WHERE p.name = 'Random House'
SELECT a.id AS "ID", CONCAT(a.last_name, ' ', a.first_name) AS "Full Name", COUNT(b.id) AS "Number of Books"
FROM authors AS a
LEFT JOIN books AS b ON b.author_id=a.id
GROUP BY a.id
SELECT a.id AS "ID", CONCAT(a.last_name, ' ', a.first_name) AS "Full Name", COUNT(b.id) AS "Number of Books"
FROM authors AS a
LEFT JOIN books AS b ON b.author_id=a.id
GROUP BY a.id
ORDER BY "Number of Books" DESC
SELECT b.title FROM editions AS e
FULL OUTER JOIN books AS b ON e.book_id = b.id
GROUP BY b.title, e.book_id
HAVING COUNT(e.book_id) = 2 And e.book_id IN
(SELECT e1.book_id
FROM editions e1
JOIN editions e2 ON e1.book_id = e2.book_id
WHERE e1.type <> e2.type)
SELECT p.name AS "Publisher",
ROUND(AVG(retail),2) AS "Average Book Sale Price",
COUNT(edition) AS "Editions Published"
FROM publishers AS p
JOIN editions AS e ON p.id = e.publisher_id
JOIN stock AS s ON s.isbn = e.isbn
GROUP BY p.name
SELECT e.isbn, b.title FROM editions AS e
INNER JOIN publishers as p ON e.publisher_id=p.id
INNER JOIN books as b ON b.id=e.book_id
WHERE p.name = 'Random House'
SELECT e.isbn, b.title, s.stock, s.retail FROM editions AS e
INNER JOIN publishers as p ON e.publisher_id=p.id
INNER JOIN books as b ON b.id=e.book_id
INNER JOIN stock as s ON s.isbn=e.isbn
WHERE p.name = 'Random House'
SELECT e.isbn, b.title, s.stock, s.retail FROM editions AS e
INNER JOIN publishers as p ON e.publisher_id=p.id
INNER JOIN books as b ON b.id=e.book_id
INNER JOIN stock as s ON s.isbn=e.isbn
WHERE p.name = 'Random House' AND s.stock > 0
SELECT e.isbn, b.title, s.stock, s.retail, CASE WHEN e.type = 'h' THEN 'hardcover'
WHEN e.type = 'p' THEN 'paperback'
END AS "type"
FROM editions AS e
INNER JOIN publishers as p ON e.publisher_id=p.id
INNER JOIN books as b ON b.id=e.book_id
INNER JOIN stock as s ON s.isbn=e.isbn
WHERE p.name = 'Random House' AND s.stock > 0
SELECT b.title, e.publication
FROM editions AS e
RIGHT JOIN books as b ON b.id=e.book_id
SELECT SUM(stock)
FROM stock
SELECT ROUND(AVG(cost),2) as "Average Cost", ROUND(AVG(retail),2) as "Average Retail", ROUND(AVG(retail) - AVG(cost),2) as "Average Profit"
FROM stock
SELECT book_id FROM editions
WHERE isbn = (SELECT isbn FROM stock ORDER BY stock DESC LIMIT 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment