Interesting ideas but some chapters are too general.
SQL behind this page6 statements
This page was built with 6 statements
SELECT b.*, c.name AS category, p.name AS publisher
FROM book b
JOIN category c ON c.category_id = b.category_id
JOIN publisher p ON p.publisher_id = b.publisher_id
WHERE b.book_id = %s
Builds: the book itself: cover, title, publisher, price, stock, the highlights and the details grid.
The page as it is now. Red: what this statement builds.
Step by step
SELECT b.*, c.name AS category, p.name AS publisher
FROM book b
JOIN category c ON c.category_id = b.category_id
JOIN publisher p ON p.publisher_id = b.publisher_id
WHERE b.book_id = %s
Step 1 of 5
SELECT
Chooses the columns (or computed values) that the result shows.
Back
SELECT b.*, c.name AS category, p.name AS publisher
FROM book b
JOIN category c ON c.category_id = b.category_id
JOIN publisher p ON p.publisher_id = b.publisher_id
WHERE b.book_id = %s
Step 2 of 5
FROM
The table the rows come from.
SELECT b.*, c.name AS category, p.name AS publisher
FROM book b
JOIN category c ON c.category_id = b.category_id
JOIN publisher p ON p.publisher_id = b.publisher_id
WHERE b.book_id = %s
Step 3 of 5
JOIN
Adds the matching row of another table; the ON part says which columns must be equal.
SELECT b.*, c.name AS category, p.name AS publisher
FROM book b
JOIN category c ON c.category_id = b.category_id
JOIN publisher p ON p.publisher_id = b.publisher_id
WHERE b.book_id = %s
Step 4 of 5
JOIN
Adds the matching row of another table; the ON part says which columns must be equal.
SELECT b.*, c.name AS category, p.name AS publisher
FROM book b
JOIN category c ON c.category_id = b.category_id
JOIN publisher p ON p.publisher_id = b.publisher_id
WHERE b.book_id = %s
Step 5 of 5
WHERE
Keeps only the rows for which the condition is true; AND and OR combine several conditions. 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 a.first_name, a.last_name, a.nationality
FROM author a
JOIN book_author ba ON ba.author_id = a.author_id
WHERE ba.book_id = %sORDERBY a.last_name
Builds: the author names: the Author line under the title and the Author row of the details.
The page as it is now. Red: what this statement builds.
Step by step
SELECT a.first_name, a.last_name, a.nationality
FROM author a
JOIN book_author ba ON ba.author_id = a.author_id
WHERE ba.book_id = %sORDERBY a.last_name
Step 1 of 5
SELECT
Chooses the columns (or computed values) that the result shows.
Back
SELECT a.first_name, a.last_name, a.nationality
FROM author a
JOIN book_author ba ON ba.author_id = a.author_id
WHERE ba.book_id = %sORDERBY a.last_name
Step 2 of 5
FROM
The table the rows come from.
SELECT a.first_name, a.last_name, a.nationality
FROM author a
JOIN book_author ba ON ba.author_id = a.author_id
WHERE ba.book_id = %sORDERBY a.last_name
Step 3 of 5
JOIN
Adds the matching row of another table; the ON part says which columns must be equal.
SELECT a.first_name, a.last_name, a.nationality
FROM author a
JOIN book_author ba ON ba.author_id = a.author_id
WHERE ba.book_id = %sORDERBY a.last_name
Step 4 of 5
WHERE
Keeps only the rows for which the condition is true; AND and OR combine several conditions. 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 a.first_name, a.last_name, a.nationality
FROM author a
JOIN book_author ba ON ba.author_id = a.author_id
WHERE ba.book_id = %sORDERBY a.last_name
Step 5 of 5
ORDER BY
Sorts the result by these columns, from the smallest to the largest (A to Z) unless DESC is written.
SELECT r.review_id, r.user_id, r.rating, r.comment, r.review_date, u.full_name
FROM review r
JOIN user_account u ON u.user_id = r.user_id
WHERE r.book_id = %sORDERBY r.review_date DESC
Builds: the reviews with their rating summary, the stars and count under the title, and the number on the Reviews tab.
The page as it is now. Red: what this statement builds.
Step by step
SELECT r.review_id, r.user_id, r.rating, r.comment, r.review_date, u.full_name
FROM review r
JOIN user_account u ON u.user_id = r.user_id
WHERE r.book_id = %sORDERBY r.review_date DESC
Step 1 of 5
SELECT
Chooses the columns (or computed values) that the result shows.
Back
SELECT r.review_id, r.user_id, r.rating, r.comment, r.review_date, u.full_name
FROM review r
JOIN user_account u ON u.user_id = r.user_id
WHERE r.book_id = %sORDERBY r.review_date DESC
Step 2 of 5
FROM
The table the rows come from.
SELECT r.review_id, r.user_id, r.rating, r.comment, r.review_date, u.full_name
FROM review r
JOIN user_account u ON u.user_id = r.user_id
WHERE r.book_id = %sORDERBY r.review_date DESC
Step 3 of 5
JOIN
Adds the matching row of another table; the ON part says which columns must be equal.
SELECT r.review_id, r.user_id, r.rating, r.comment, r.review_date, u.full_name
FROM review r
JOIN user_account u ON u.user_id = r.user_id
WHERE r.book_id = %sORDERBY r.review_date DESC
Step 4 of 5
WHERE
Keeps only the rows for which the condition is true; AND and OR combine several conditions. 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 r.review_id, r.user_id, r.rating, r.comment, r.review_date, u.full_name
FROM review r
JOIN user_account u ON u.user_id = r.user_id
WHERE r.book_id = %sORDERBY r.review_date DESC
Step 5 of 5
ORDER BY
Sorts the result by these columns, from the smallest to the largest (A to Z) unless DESC is written.
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.category_id, b.cover_image,
p.name AS publisher,
STRING_AGG(a.first_name || ' ' || a.last_name, ', ') AS authors
FROM book b
JOIN publisher p ON p.publisher_id = b.publisher_id
LEFTJOIN book_author ba ON ba.book_id = b.book_id
LEFTJOIN author a ON a.author_id = ba.author_id
WHERE b.category_id = %sAND b.book_id != %sGROUPBY b.book_id, b.title, b.price, b.stock_quantity, b.category_id, b.cover_image, p.name
ORDERBY b.title
LIMIT5
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.
Back
SELECT b.book_id, b.title, b.price, b.stock_quantity, b.category_id, b.cover_image,
p.name AS publisher,
STRING_AGG(a.first_name || ' ' || a.last_name, ', ') AS authors
FROM book b
JOIN publisher p ON p.publisher_id = b.publisher_id
LEFTJOIN book_author ba ON ba.book_id = b.book_id
LEFTJOIN author a ON a.author_id = ba.author_id
WHERE b.category_id = %sAND b.book_id != %sGROUPBY b.book_id, b.title, b.price, b.stock_quantity, b.category_id, b.cover_image, p.name
ORDERBY b.title
LIMIT5
Step 2 of 9
FROM
The table the rows come from.
SELECT b.book_id, b.title, b.price, b.stock_quantity, b.category_id, b.cover_image,
p.name AS publisher,
STRING_AGG(a.first_name || ' ' || a.last_name, ', ') AS authors
FROM book b
JOIN publisher p ON p.publisher_id = b.publisher_id
LEFTJOIN book_author ba ON ba.book_id = b.book_id
LEFTJOIN author a ON a.author_id = ba.author_id
WHERE b.category_id = %sAND b.book_id != %sGROUPBY b.book_id, b.title, b.price, b.stock_quantity, b.category_id, b.cover_image, p.name
ORDERBY b.title
LIMIT5
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.category_id, b.cover_image,
p.name AS publisher,
STRING_AGG(a.first_name || ' ' || a.last_name, ', ') AS authors
FROM book b
JOIN publisher p ON p.publisher_id = b.publisher_id
LEFTJOIN book_author ba ON ba.book_id = b.book_id
LEFTJOIN author a ON a.author_id = ba.author_id
WHERE b.category_id = %sAND b.book_id != %sGROUPBY b.book_id, b.title, b.price, b.stock_quantity, b.category_id, b.cover_image, p.name
ORDERBY b.title
LIMIT5
Step 4 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.category_id, b.cover_image,
p.name AS publisher,
STRING_AGG(a.first_name || ' ' || a.last_name, ', ') AS authors
FROM book b
JOIN publisher p ON p.publisher_id = b.publisher_id
LEFTJOIN book_author ba ON ba.book_id = b.book_id
LEFTJOIN author a ON a.author_id = ba.author_id
WHERE b.category_id = %sAND b.book_id != %sGROUPBY b.book_id, b.title, b.price, b.stock_quantity, b.category_id, b.cover_image, p.name
ORDERBY b.title
LIMIT5
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.category_id, b.cover_image,
p.name AS publisher,
STRING_AGG(a.first_name || ' ' || a.last_name, ', ') AS authors
FROM book b
JOIN publisher p ON p.publisher_id = b.publisher_id
LEFTJOIN book_author ba ON ba.book_id = b.book_id
LEFTJOIN author a ON a.author_id = ba.author_id
WHERE b.category_id = %sAND b.book_id != %sGROUPBY b.book_id, b.title, b.price, b.stock_quantity, b.category_id, b.cover_image, p.name
ORDERBY b.title
LIMIT5
Step 6 of 9
WHERE
Keeps only the rows for which the condition is true; AND and OR combine several conditions. 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.category_id, b.cover_image,
p.name AS publisher,
STRING_AGG(a.first_name || ' ' || a.last_name, ', ') AS authors
FROM book b
JOIN publisher p ON p.publisher_id = b.publisher_id
LEFTJOIN book_author ba ON ba.book_id = b.book_id
LEFTJOIN author a ON a.author_id = ba.author_id
WHERE b.category_id = %sAND b.book_id != %sGROUPBY b.book_id, b.title, b.price, b.stock_quantity, b.category_id, b.cover_image, p.name
ORDERBY b.title
LIMIT5
Step 7 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.category_id, b.cover_image,
p.name AS publisher,
STRING_AGG(a.first_name || ' ' || a.last_name, ', ') AS authors
FROM book b
JOIN publisher p ON p.publisher_id = b.publisher_id
LEFTJOIN book_author ba ON ba.book_id = b.book_id
LEFTJOIN author a ON a.author_id = ba.author_id
WHERE b.category_id = %sAND b.book_id != %sGROUPBY b.book_id, b.title, b.price, b.stock_quantity, b.category_id, b.cover_image, p.name
ORDERBY b.title
LIMIT5
Step 8 of 9
ORDER BY
Sorts the result by these columns, from the smallest to the largest (A to Z) unless DESC is written.
SELECT b.book_id, b.title, b.price, b.stock_quantity, b.category_id, b.cover_image,
p.name AS publisher,
STRING_AGG(a.first_name || ' ' || a.last_name, ', ') AS authors
FROM book b
JOIN publisher p ON p.publisher_id = b.publisher_id
LEFTJOIN book_author ba ON ba.book_id = b.book_id
LEFTJOIN author a ON a.author_id = ba.author_id
WHERE b.category_id = %sAND b.book_id != %sGROUPBY b.book_id, b.title, b.price, b.stock_quantity, b.category_id, b.cover_image, p.name
ORDERBY b.title
LIMIT5
Step 9 of 9
LIMIT
Keeps only the first rows of the result, at most this many.
SELECT c.category_id, c.name, COUNT(b.book_id) AS book_count
FROM category c
LEFTJOIN book b ON b.category_id = c.category_id
GROUPBY c.category_id, c.name
ORDERBY c.name
Builds: the category links: in the menu row and the footer, and on a book list the category band or the side list with the number of books.
The page as it is now. Red: what this statement builds.
Step by step
SELECT c.category_id, c.name, COUNT(b.book_id) AS book_count
FROM category c
LEFTJOIN book b ON b.category_id = c.category_id
GROUPBY c.category_id, c.name
ORDERBY c.name
Step 1 of 5
SELECT
Chooses the columns (or computed values) that the result shows. Here COUNT counts rows.
Back
SELECT c.category_id, c.name, COUNT(b.book_id) AS book_count
FROM category c
LEFTJOIN book b ON b.category_id = c.category_id
GROUPBY c.category_id, c.name
ORDERBY c.name
Step 2 of 5
FROM
The table the rows come from.
SELECT c.category_id, c.name, COUNT(b.book_id) AS book_count
FROM category c
LEFTJOIN book b ON b.category_id = c.category_id
GROUPBY c.category_id, c.name
ORDERBY c.name
Step 3 of 5
LEFT JOIN
Like JOIN, but a row is kept even when the other table has no match (then its columns are empty).
SELECT c.category_id, c.name, COUNT(b.book_id) AS book_count
FROM category c
LEFTJOIN book b ON b.category_id = c.category_id
GROUPBY c.category_id, c.name
ORDERBY c.name
Step 4 of 5
GROUP BY
Puts the rows with the same values into one group, so COUNT, SUM or AVG give one result per group.
SELECT c.category_id, c.name, COUNT(b.book_id) AS book_count
FROM category c
LEFTJOIN book b ON b.category_id = c.category_id
GROUPBY c.category_id, c.name
ORDERBY c.name
Step 5 of 5
ORDER BY
Sorts the result by these columns, from the smallest to the largest (A to Z) unless DESC is written.