Skip to main content
ThunderLang™Programming Language

Gallery · SQL · Sakila sample database

Is this DVD in stock? (Sakila, SQL)

Sakila is MySQL's famous sample database of a DVD rental store, used in countless SQL tutorials. This stored function answers whether one copy of a film is on the shelf. The two queries that do the counting are shown as SQL; Thunder puts the decision around them into words.

What to look for

No rentals ever means in stock; otherwise any rental without a return date means it is out.

How much Thunder could read

5 of 7 steps put into words; the other 2 are shown as code.

Read by the current Thunder compiler when this page was built. Hand-checked against the code: accurate, with some wording that is thin or close to the code.

Flow: How does it go? One action or rule as a path: questions with Yes and No, repeats, the Stops, what happens if something goes wrong, and what always happens at the end.

In this file

Inventory in stock

From codelines 609–636
You give itinventory id
You get backyes or no
  1. #AN ITEM IS IN-STOCK IF THERE ARE EITHER NO ROWS IN THE rental TABLE #FOR THE ITEM OR ALL ROWS HAVE return_date POPULATED SELECT COUNT(*) INTO v_rentals FROM rental WHERE inventory_id = p_inventory_id
  2. Is rentals 0?

    Yes

    1. Gives back yes

    No continue

  3. SELECT COUNT(rental_id) INTO v_out FROM inventory LEFT JOIN rental USING(inventory_id) WHERE inventory.inventory_id = p_inventory_id AND rental.return_date IS NULL
  4. Is out more than 0?

    Yes

    1. Gives back no

    No

    1. Gives back yes
The same reading as Thunder Text, built with this page
Inventory in stock
  You give it: inventory id
  You get back: yes or no
  - `#AN ITEM IS IN-STOCK IF THERE ARE EITHER NO ROWS IN THE rental TABLE #FOR THE ITEM OR ALL ROWS HAVE return_date POPULATED SELECT COUNT(*) INTO v_rentals FROM rental WHERE inventory_id = p_inventory_id` (code, not explained)  (line 615)
  - Ask: Is rentals 0?  (line 622)
    Yes:
      - Gives back yes  (line 623)
    No: continue
  - `SELECT COUNT(rental_id) INTO v_out FROM inventory LEFT JOIN rental USING(inventory_id) WHERE inventory.inventory_id = p_inventory_id AND rental.return_date IS NULL` (code, not explained)  (line 626)
  - Ask: Is out more than 0?  (line 631)
    Yes:
      - Gives back no  (line 632)
    No:
      - Gives back yes  (line 634)

Source and license

From Sakila sample database (jOOQ/sakila), mysql-sakila-db/mysql-sakila-schema.sql, lines 609–636, at commit e089a5b (main).

Copyright (c) 2006, MySQL AB (schema); Copyright (c) 2021, jOOQ Object Oriented Querying (collection). Licensed under the BSD (3-Clause, MySQL AB; 2-Clause, jOOQ). The code is shown unmodified; the Thunder reading is ThunderLang's.

Full license text (LICENSE)
BSD 2-Clause License

Copyright (c) 2021, jOOQ Object Oriented Querying
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.