Skip to content

Instantly share code, notes, and snippets.

@joshuastr
Created May 10, 2016 17:42
Show Gist options
  • Save joshuastr/0e9ef40e4a5d8a133bfccdee8d69a0a4 to your computer and use it in GitHub Desktop.
Save joshuastr/0e9ef40e4a5d8a133bfccdee8d69a0a4 to your computer and use it in GitHub Desktop.
SQL Exercise
SELECT isbn FROM editions
where publisher_id = 59
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
SELECT books.title, editions.isbn from books join editions
on books.id = editions.book_id
where editions.publisher_id = 59;
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;
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;
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;
SELECT books.title, editions.publication
FROM books
join editions on books.id = editions.book_id
SELECT sum(stock.stock) from stock
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
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