SQL behind this page 3 statements
This page was built with 3 statements
-
SELECT b.book_id, b.title, b.price, b.stock_quantity, b.publication_year, b.category_id, b.cover_image, c.name AS category, p.name AS publisher, STRING_AGG(a.first_name || ' ' || a.last_name, ', ') AS authors FROM book b JOIN category c ON c.category_id = b.category_id JOIN publisher p ON p.publisher_id = b.publisher_id LEFT JOIN book_author ba ON ba.book_id = b.book_id LEFT JOIN author a ON a.author_id = ba.author_id WHERE (b.title ILIKE %s OR b.isbn = %s OR b.book_id IN (SELECT ba2.book_id FROM book_author ba2 JOIN author a2 ON a2.author_id = ba2.author_id WHERE a2.first_name || ' ' || a2.last_name ILIKE %s)) AND b.category_id = %s GROUP BY b.book_id, b.title, b.price, b.stock_quantity, b.publication_year, b.category_id, b.cover_image, c.name, p.name ORDER BY b.title
Back to this SQL statementShow on the pageBuilds: the book cards of the list (title, authors, publisher, price, stock) and the number of books listed.
%s values, in order:
'%Terry Pratchett%''Terry Pratchett''%Terry Pratchett%'3Picture
The page as it is now. Red: what this statement builds.
Step by step
SELECT b.book_id, b.title, b.price, b.stock_quantity, b.publication_year, b.category_id, b.cover_image, c.name AS category, p.name AS publisher, STRING_AGG(a.first_name || ' ' || a.last_name, ', ') AS authors FROM book b JOIN category c ON c.category_id = b.category_id JOIN publisher p ON p.publisher_id = b.publisher_id LEFT JOIN book_author ba ON ba.book_id = b.book_id LEFT JOIN author a ON a.author_id = ba.author_id WHERE (b.title ILIKE %s OR b.isbn = %s OR b.book_id IN (SELECT ba2.book_id FROM book_author ba2 JOIN author a2 ON a2.author_id = ba2.author_id WHERE a2.first_name || ' ' || a2.last_name ILIKE %s)) AND b.category_id = %s GROUP BY b.book_id, b.title, b.price, b.stock_quantity, b.publication_year, b.category_id, b.cover_image, c.name, p.name ORDER BY b.title
Step 1 of 9
SELECT
Chooses the columns (or computed values) that the result shows. Here STRING_AGG joins the texts of several rows into one text.
BackSELECT b.book_id, b.title, b.price, b.stock_quantity, b.publication_year, b.category_id, b.cover_image, c.name AS category, p.name AS publisher, STRING_AGG(a.first_name || ' ' || a.last_name, ', ') AS authors FROM book b JOIN category c ON c.category_id = b.category_id JOIN publisher p ON p.publisher_id = b.publisher_id LEFT JOIN book_author ba ON ba.book_id = b.book_id LEFT JOIN author a ON a.author_id = ba.author_id WHERE (b.title ILIKE %s OR b.isbn = %s OR b.book_id IN (SELECT ba2.book_id FROM book_author ba2 JOIN author a2 ON a2.author_id = ba2.author_id WHERE a2.first_name || ' ' || a2.last_name ILIKE %s)) AND b.category_id = %s GROUP BY b.book_id, b.title, b.price, b.stock_quantity, b.publication_year, b.category_id, b.cover_image, c.name, p.name ORDER BY b.title
Step 2 of 9
FROM
The table the rows come from.
SELECT b.book_id, b.title, b.price, b.stock_quantity, b.publication_year, b.category_id, b.cover_image, c.name AS category, p.name AS publisher, STRING_AGG(a.first_name || ' ' || a.last_name, ', ') AS authors FROM book b JOIN category c ON c.category_id = b.category_id JOIN publisher p ON p.publisher_id = b.publisher_id LEFT JOIN book_author ba ON ba.book_id = b.book_id LEFT JOIN author a ON a.author_id = ba.author_id WHERE (b.title ILIKE %s OR b.isbn = %s OR b.book_id IN (SELECT ba2.book_id FROM book_author ba2 JOIN author a2 ON a2.author_id = ba2.author_id WHERE a2.first_name || ' ' || a2.last_name ILIKE %s)) AND b.category_id = %s GROUP BY b.book_id, b.title, b.price, b.stock_quantity, b.publication_year, b.category_id, b.cover_image, c.name, p.name ORDER BY b.title
Step 3 of 9
JOIN
Adds the matching row of another table; the ON part says which columns must be equal.
SELECT b.book_id, b.title, b.price, b.stock_quantity, b.publication_year, b.category_id, b.cover_image, c.name AS category, p.name AS publisher, STRING_AGG(a.first_name || ' ' || a.last_name, ', ') AS authors FROM book b JOIN category c ON c.category_id = b.category_id JOIN publisher p ON p.publisher_id = b.publisher_id LEFT JOIN book_author ba ON ba.book_id = b.book_id LEFT JOIN author a ON a.author_id = ba.author_id WHERE (b.title ILIKE %s OR b.isbn = %s OR b.book_id IN (SELECT ba2.book_id FROM book_author ba2 JOIN author a2 ON a2.author_id = ba2.author_id WHERE a2.first_name || ' ' || a2.last_name ILIKE %s)) AND b.category_id = %s GROUP BY b.book_id, b.title, b.price, b.stock_quantity, b.publication_year, b.category_id, b.cover_image, c.name, p.name ORDER BY b.title
Step 4 of 9
JOIN
Adds the matching row of another table; the ON part says which columns must be equal.
SELECT b.book_id, b.title, b.price, b.stock_quantity, b.publication_year, b.category_id, b.cover_image, c.name AS category, p.name AS publisher, STRING_AGG(a.first_name || ' ' || a.last_name, ', ') AS authors FROM book b JOIN category c ON c.category_id = b.category_id JOIN publisher p ON p.publisher_id = b.publisher_id LEFT JOIN book_author ba ON ba.book_id = b.book_id LEFT JOIN author a ON a.author_id = ba.author_id WHERE (b.title ILIKE %s OR b.isbn = %s OR b.book_id IN (SELECT ba2.book_id FROM book_author ba2 JOIN author a2 ON a2.author_id = ba2.author_id WHERE a2.first_name || ' ' || a2.last_name ILIKE %s)) AND b.category_id = %s GROUP BY b.book_id, b.title, b.price, b.stock_quantity, b.publication_year, b.category_id, b.cover_image, c.name, p.name ORDER BY b.title
Step 5 of 9
LEFT JOIN
Like JOIN, but a row is kept even when the other table has no match (then its columns are empty).
SELECT b.book_id, b.title, b.price, b.stock_quantity, b.publication_year, b.category_id, b.cover_image, c.name AS category, p.name AS publisher, STRING_AGG(a.first_name || ' ' || a.last_name, ', ') AS authors FROM book b JOIN category c ON c.category_id = b.category_id JOIN publisher p ON p.publisher_id = b.publisher_id LEFT JOIN book_author ba ON ba.book_id = b.book_id LEFT JOIN author a ON a.author_id = ba.author_id WHERE (b.title ILIKE %s OR b.isbn = %s OR b.book_id IN (SELECT ba2.book_id FROM book_author ba2 JOIN author a2 ON a2.author_id = ba2.author_id WHERE a2.first_name || ' ' || a2.last_name ILIKE %s)) AND b.category_id = %s GROUP BY b.book_id, b.title, b.price, b.stock_quantity, b.publication_year, b.category_id, b.cover_image, c.name, p.name ORDER BY b.title
Step 6 of 9
LEFT JOIN
Like JOIN, but a row is kept even when the other table has no match (then its columns are empty).
SELECT b.book_id, b.title, b.price, b.stock_quantity, b.publication_year, b.category_id, b.cover_image, c.name AS category, p.name AS publisher, STRING_AGG(a.first_name || ' ' || a.last_name, ', ') AS authors FROM book b JOIN category c ON c.category_id = b.category_id JOIN publisher p ON p.publisher_id = b.publisher_id LEFT JOIN book_author ba ON ba.book_id = b.book_id LEFT JOIN author a ON a.author_id = ba.author_id WHERE (b.title ILIKE %s OR b.isbn = %s OR b.book_id IN (SELECT ba2.book_id FROM book_author ba2 JOIN author a2 ON a2.author_id = ba2.author_id WHERE a2.first_name || ' ' || a2.last_name ILIKE %s)) AND b.category_id = %s GROUP BY b.book_id, b.title, b.price, b.stock_quantity, b.publication_year, b.category_id, b.cover_image, c.name, p.name ORDER BY b.title
Step 7 of 9
WHERE
Keeps only the rows for which the condition is true; AND and OR combine several conditions. It holds a subquery in brackets: the subquery runs first and gives its result to the outer query. Each %s stands for a value (for example one typed in a form) that is sent separately from the SQL text, which is why text typed in a form cannot change the query.
SELECT b.book_id, b.title, b.price, b.stock_quantity, b.publication_year, b.category_id, b.cover_image, c.name AS category, p.name AS publisher, STRING_AGG(a.first_name || ' ' || a.last_name, ', ') AS authors FROM book b JOIN category c ON c.category_id = b.category_id JOIN publisher p ON p.publisher_id = b.publisher_id LEFT JOIN book_author ba ON ba.book_id = b.book_id LEFT JOIN author a ON a.author_id = ba.author_id WHERE (b.title ILIKE %s OR b.isbn = %s OR b.book_id IN (SELECT ba2.book_id FROM book_author ba2 JOIN author a2 ON a2.author_id = ba2.author_id WHERE a2.first_name || ' ' || a2.last_name ILIKE %s)) AND b.category_id = %s GROUP BY b.book_id, b.title, b.price, b.stock_quantity, b.publication_year, b.category_id, b.cover_image, c.name, p.name ORDER BY b.title
Step 8 of 9
GROUP BY
Puts the rows with the same values into one group, so COUNT, SUM or AVG give one result per group.
SELECT b.book_id, b.title, b.price, b.stock_quantity, b.publication_year, b.category_id, b.cover_image, c.name AS category, p.name AS publisher, STRING_AGG(a.first_name || ' ' || a.last_name, ', ') AS authors FROM book b JOIN category c ON c.category_id = b.category_id JOIN publisher p ON p.publisher_id = b.publisher_id LEFT JOIN book_author ba ON ba.book_id = b.book_id LEFT JOIN author a ON a.author_id = ba.author_id WHERE (b.title ILIKE %s OR b.isbn = %s OR b.book_id IN (SELECT ba2.book_id FROM book_author ba2 JOIN author a2 ON a2.author_id = ba2.author_id WHERE a2.first_name || ' ' || a2.last_name ILIKE %s)) AND b.category_id = %s GROUP BY b.book_id, b.title, b.price, b.stock_quantity, b.publication_year, b.category_id, b.cover_image, c.name, p.name ORDER BY b.title
Step 9 of 9
ORDER BY
Sorts the result by these columns, from the smallest to the largest (A to Z) unless DESC is written.
-
SELECT book_id, ROUND(AVG(rating), 1) AS avg_rating, COUNT(*) AS review_count FROM review GROUP BY book_id
Back to this SQL statementShow on the pageBuilds: the stars and review counts on the cards.
Picture
The page as it is now. Red: what this statement builds.
Step by step
SELECT book_id, ROUND(AVG(rating), 1) AS avg_rating, COUNT(*) AS review_count FROM review GROUP BY book_id
Step 1 of 3
SELECT
Chooses the columns (or computed values) that the result shows. Here COUNT counts rows, AVG gives the average value.
BackSELECT book_id, ROUND(AVG(rating), 1) AS avg_rating, COUNT(*) AS review_count FROM review GROUP BY book_id
Step 2 of 3
FROM
The table the rows come from.
SELECT book_id, ROUND(AVG(rating), 1) AS avg_rating, COUNT(*) AS review_count FROM review GROUP BY book_id
Step 3 of 3
GROUP BY
Puts the rows with the same values into one group, so COUNT, SUM or AVG give one result per group.