Books / Search

Results for "Martin Kleppmann"

1 book listed. Clear

SQL behind this page 3 statements

This page was built with 3 statements

  1. 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

    Builds: the book cards of the list (title, authors, publisher, price, stock) and the number of books listed.

    %s values, in order: '%Martin Kleppmann%''Martin Kleppmann''%Martin Kleppmann%'

    Show on the page
    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.

    Back
    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 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.

    Back to this SQL statement
  2. SELECT book_id, ROUND(AVG(rating), 1) AS avg_rating, COUNT(*) AS review_count
    FROM review
    GROUP BY book_id

    Builds: the stars and review counts on the cards.

    Show on the page
    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.

    Back
    SELECT 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.

    Back to this SQL statement
  3. SELECT c.category_id, c.name, COUNT(b.book_id) AS book_count
    FROM category c
    LEFT JOIN book b ON b.category_id = c.category_id
    GROUP BY c.category_id, c.name
    ORDER BY 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.

    Show on the page
    Picture

    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
    LEFT JOIN book b ON b.category_id = c.category_id
    GROUP BY c.category_id, c.name
    ORDER BY 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
    LEFT JOIN book b ON b.category_id = c.category_id
    GROUP BY c.category_id, c.name
    ORDER BY 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
    LEFT JOIN book b ON b.category_id = c.category_id
    GROUP BY c.category_id, c.name
    ORDER BY 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
    LEFT JOIN book b ON b.category_id = c.category_id
    GROUP BY c.category_id, c.name
    ORDER BY 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
    LEFT JOIN book b ON b.category_id = c.category_id
    GROUP BY c.category_id, c.name
    ORDER BY 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.

    Back to this SQL statement