SELECT *
FROM books;
SELECT *
FROM books
ORDER BY title;
SELECT *
FROM books
WHERE rented=true;
SELECT COUNT(id)
AS Ile_jest_w_tym_momencie_wypożyczonych_książek
FROM books
WHERE rented=true;
SELECT COUNT(id)
AS Ile_jest_książek_danego_autora
FROM authors
WHERE name='dany autor';
Zakładając, że każda dodana książka to unikalna pozycja:
SELECT books.title AS title,
COUNT(rentals.book_id) AS top_rented_b
FROM books
INNER JOIN rentals ON books.id = rentals.book_id
GROUP BY books.title, rentals.book_id
HAVING COUNT(rentals.book_id) = (
SELECT MAX(top_rented_b)
FROM (
SELECT book_id,COUNT(rentals.book_id)
AS top_rented_b
FROM rentals GROUP BY book_id)rentals
);
Zakładając, że dodane książki mogą się powielać:
SELECT books.title AS title,
COUNT(rentals.book_id) AS top_rented_b
FROM books
INNER JOIN rentals ON books.id = rentals.book_id
GROUP BY books.title
HAVING COUNT(rentals.book_id) = (
SELECT MAX(top_rented_b)
FROM (
SELECT books.title AS title,
COUNT(rentals.book_id) AS top_rented_b
FROM books
INNER JOIN rentals ON books.id = rentals.book_id
GROUP BY books.title)books);
Zakładając, że każda dodana książka to unikalna pozycja:
SELECT books.title AS title,
COUNT(rentals.book_id) AS top_rented_b
FROM books
INNER JOIN rentals ON books.id = rentals.book_id
GROUP BY books.id
HAVING COUNT(rentals.book_id) >=2
Zakładając, że dodane książki mogą się powielać:
SELECT books.title AS title,
COUNT(rentals.book_id) AS top_rented_b
FROM books
INNER JOIN rentals ON books.id = rentals.book_id
GROUP BY books.title
HAVING COUNT(rentals.book_id) >=2