Browse by category
All booksBest sellers
See allNew arrivals
See all
All books
17 books listed, title A-ZSQL behind this page 5 statements
This page was built with 5 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)) 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:
'%%''''%%'Picture
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)) 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)) 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)) 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)) 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)) 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)) 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)) 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)) 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)) 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.
-
SELECT oi.book_id, SUM(oi.quantity) AS copies_sold FROM order_item oi JOIN orders o ON o.order_id = oi.order_id WHERE o.status != 'cancelled' GROUP BY oi.book_id ORDER BY copies_sold DESC, oi.book_id LIMIT 5
Back to this SQL statementShow on the pageBuilds: the podium at the top and the Best sellers shelf: which books, in which order, and how many copies were ordered.
Picture
The page as it is now. Red: what this statement builds.
Step by step
SELECT oi.book_id, SUM(oi.quantity) AS copies_sold FROM order_item oi JOIN orders o ON o.order_id = oi.order_id WHERE o.status != 'cancelled' GROUP BY oi.book_id ORDER BY copies_sold DESC, oi.book_id LIMIT 5
Step 1 of 7
SELECT
Chooses the columns (or computed values) that the result shows. Here SUM adds the values up.
BackSELECT oi.book_id, SUM(oi.quantity) AS copies_sold FROM order_item oi JOIN orders o ON o.order_id = oi.order_id WHERE o.status != 'cancelled' GROUP BY oi.book_id ORDER BY copies_sold DESC, oi.book_id LIMIT 5
Step 2 of 7
FROM
The table the rows come from.
SELECT oi.book_id, SUM(oi.quantity) AS copies_sold FROM order_item oi JOIN orders o ON o.order_id = oi.order_id WHERE o.status != 'cancelled' GROUP BY oi.book_id ORDER BY copies_sold DESC, oi.book_id LIMIT 5
Step 3 of 7
JOIN
Adds the matching row of another table; the ON part says which columns must be equal.
SELECT oi.book_id, SUM(oi.quantity) AS copies_sold FROM order_item oi JOIN orders o ON o.order_id = oi.order_id WHERE o.status != 'cancelled' GROUP BY oi.book_id ORDER BY copies_sold DESC, oi.book_id LIMIT 5
Step 4 of 7
WHERE
Keeps only the rows for which the condition is true; AND and OR combine several conditions.
SELECT oi.book_id, SUM(oi.quantity) AS copies_sold FROM order_item oi JOIN orders o ON o.order_id = oi.order_id WHERE o.status != 'cancelled' GROUP BY oi.book_id ORDER BY copies_sold DESC, oi.book_id LIMIT 5
Step 5 of 7
GROUP BY
Puts the rows with the same values into one group, so COUNT, SUM or AVG give one result per group.
SELECT oi.book_id, SUM(oi.quantity) AS copies_sold FROM order_item oi JOIN orders o ON o.order_id = oi.order_id WHERE o.status != 'cancelled' GROUP BY oi.book_id ORDER BY copies_sold DESC, oi.book_id LIMIT 5
Step 6 of 7
ORDER BY
Sorts the result by these columns, from the smallest to the largest (A to Z) unless DESC is written.
SELECT oi.book_id, SUM(oi.quantity) AS copies_sold FROM order_item oi JOIN orders o ON o.order_id = oi.order_id WHERE o.status != 'cancelled' GROUP BY oi.book_id ORDER BY copies_sold DESC, oi.book_id LIMIT 5
Step 7 of 7
LIMIT
Keeps only the first rows of the result, at most this many.
-
SELECT book_id FROM book ORDER BY book_id DESC LIMIT 5
Back to this SQL statementShow on the pageBuilds: the four books standing in the banner and the New arrivals shelf.
Picture
The page as it is now. Red: what this statement builds.
Step by step
SELECT book_id FROM book ORDER BY book_id DESC LIMIT 5
Step 1 of 4
SELECT
Chooses the columns (or computed values) that the result shows.
BackSELECT book_id FROM book ORDER BY book_id DESC LIMIT 5
Step 2 of 4
FROM
The table the rows come from.
SELECT book_id FROM book ORDER BY book_id DESC LIMIT 5
Step 3 of 4
ORDER BY
Sorts the result by these columns, from the smallest to the largest (A to Z) unless DESC is written.
SELECT book_id FROM book ORDER BY book_id DESC LIMIT 5
Step 4 of 4
LIMIT
Keeps only the first rows of the result, at most this many.