category: filtering

prompt: Return the titles of every Book published in 2015 or later, ordered by title ascending, as a list of 1-tuples.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)

expected_output: {"rows": [["Harbour Math"], ["Lantern Grammar"], ["Nine Quiet Engines"], ["Salt and Signal"], ["Small Machines"], ["The Cartographer"], ["The Fifth Harvest"], ["The Long Ledger"]]}


category: filtering

prompt: Return (title, year) for every Book that is NOT in stock, ordered by year ascending.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)

expected_output: {"rows": [["Iron Meridian", 2012], ["Harbour Math", 2016], ["Salt and Signal", 2019]]}


category: filtering

prompt: Return the titles of Books whose title contains the word 'The' (case-sensitive), ordered by title.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)

expected_output: {"rows": [["The Cartographer"], ["The Fifth Harvest"], ["The Long Ledger"]]}


category: filtering

prompt: Return (title, pages) for Books with more than 400 pages, ordered by pages descending.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)

expected_output: {"rows": [["The Cartographer", 640], ["Lantern Grammar", 590], ["Salt and Signal", 512], ["The Long Ledger", 460], ["The Fifth Harvest", 428], ["Iron Meridian", 402]]}


category: filtering

prompt: Using a Q object, return the titles of Books that are either published before 2012 OR have more than 600 pages, ordered by title.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)

expected_output: {"rows": [["Static Tides"], ["The Cartographer"], ["Winter Protocols"]]}


category: filtering

prompt: Return the titles of Books that are in stock AND published before 2015, ordered by title.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)

expected_output: {"rows": [["Paper Orbits"], ["Static Tides"], ["Winter Protocols"]]}


category: filtering

prompt: Return the titles of Books whose year is one of 2011, 2016 or 2021, ordered by year ascending.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)

expected_output: {"rows": [["Static Tides"], ["Harbour Math"], ["Lantern Grammar"]]}


category: filtering

prompt: Return (title, year) for Books published between 2013 and 2018 inclusive, ordered by year then title.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)

expected_output: {"rows": [["Paper Orbits", 2013], ["The Long Ledger", 2015], ["Harbour Math", 2016], ["Nine Quiet Engines", 2018]]}


category: filtering

prompt: Return the titles of Books EXCLUDING those by the author named 'Cleo Rivas', ordered by title.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)

expected_output: {"rows": [["Iron Meridian"], ["Nine Quiet Engines"], ["Paper Orbits"], ["Salt and Signal"], ["Small Machines"], ["Static Tides"], ["The Fifth Harvest"], ["The Long Ledger"], ["Winter Protocols"]]}


category: relations

prompt: Return (book title, author name) for every Book, ordered by book title.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)

expected_output: {"rows": [["Harbour Math", "Cleo Rivas"], ["Iron Meridian", "Dan Okafor"], ["Lantern Grammar", "Cleo Rivas"], ["Nine Quiet Engines", "Bo Nakamura"], ["Paper Orbits", "Bo Nakamura"], ["Salt and Signal", "Ada Ellis"], ["Small Machines", "Eve Lindqvist"], ["Static Tides", "Ada Ellis"], ["The Cartographer", "Cleo Rivas"], ["The Fifth Harvest", "Eve Lindqvist"], ["The Long Ledger", "Ada Ellis"], ["Winter Protocols", "Dan Okafor"]]}


category: relations

prompt: Return (book title, publisher name) for Books whose publisher is in the US, ordered by title.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)

expected_output: {"rows": [["Nine Quiet Engines", "Cedar House"], ["Paper Orbits", "Cedar House"], ["Salt and Signal", "Aurora Press"], ["Small Machines", "Cedar House"], ["Static Tides", "Aurora Press"], ["Winter Protocols", "Aurora Press"]]}


category: relations

prompt: Return the names of Authors who have at least one Book that is out of stock, with no duplicates, ordered by name.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)

expected_output: {"rows": [["Ada Ellis"], ["Cleo Rivas"], ["Dan Okafor"]]}


category: relations

prompt: Return the names of Publishers that have published at least one Book with more than 500 pages, deduplicated and ordered by name.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)

expected_output: {"rows": [["Aurora Press"], ["Delta Books"]]}


category: relations

prompt: Return (author name, book title) for Books published by 'Delta Books', ordered by book title.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)

expected_output: {"rows": [["Cleo Rivas", "Lantern Grammar"], ["Cleo Rivas", "The Cartographer"], ["Eve Lindqvist", "The Fifth Harvest"]]}


category: relations

prompt: Return the names of Authors born before 1980 who have published with a publisher in GB, deduplicated and ordered by name.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)

expected_output: {"rows": [["Ada Ellis"], ["Dan Okafor"]]}


category: aggregation

prompt: Return a single 1-tuple containing the total number of Books.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)


category: aggregation

prompt: Return (author name, number of books) for every Author, ordered by author name.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)

expected_output: {"rows": [["Ada Ellis", 3], ["Bo Nakamura", 2], ["Cleo Rivas", 3], ["Dan Okafor", 2], ["Eve Lindqvist", 2]]}


category: aggregation

prompt: Return (publisher name, number of books) for publishers with 3 or more books, ordered by publisher name.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)

expected_output: {"rows": [["Aurora Press", 3], ["Blue Heron", 3], ["Cedar House", 3], ["Delta Books", 3]]}


category: aggregation

prompt: Return (author name, total pages across their books) for every Author, ordered by author name.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)


category: aggregation

prompt: Return (author name, max book year) for every Author, ordered by author name.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)


category: aggregation

prompt: Return (publisher name, average book pages rounded to 2 decimals as a float) for every Publisher, ordered by publisher name.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)


category: aggregation

prompt: Return (publisher country, number of books published from that country) for every country with at least 2 books, ordered by country.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)


category: aggregation

prompt: Return a single 1-tuple with the number of DISTINCT authors that have at least one book in stock.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)


category: expressions

prompt: Using an F expression, return the titles of Books whose pages are greater than 20 times their year's last two digits — concretely, compare pages to (year - 2000) * 20 — ordered by title. Assume every year is 2000 or later.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)

expected_output: {"rows": [["Iron Meridian"], ["Lantern Grammar"], ["Nine Quiet Engines"], ["Salt and Signal"], ["Static Tides"], ["The Cartographer"], ["The Long Ledger"]]}


category: expressions

prompt: Return (title, price as a float) for the 3 most expensive Books, ordered by price descending.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)

expected_output: {"rows": [["The Cartographer", 42.0], ["Lantern Grammar", 38.1], ["The Fifth Harvest", 34.5]]}


category: expressions

prompt: Return (title, pages) for the 5 Books with the fewest pages, ordered by pages ascending then title ascending.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)

expected_output: {"rows": [["Winter Protocols", 168], ["Paper Orbits", 210], ["Harbour Math", 244], ["Static Tides", 320], ["Small Machines", 356]]}


category: expressions

prompt: Return the titles of Books ordered by author name ascending then year descending.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)

expected_output: {"rows": [["Salt and Signal"], ["The Long Ledger"], ["Static Tides"], ["Nine Quiet Engines"], ["Paper Orbits"], ["Lantern Grammar"], ["The Cartographer"], ["Harbour Math"], ["Iron Meridian"], ["Winter Protocols"], ["The Fifth Harvest"], ["Small Machines"]]}


category: expressions

prompt: Return (author name, birth year) for Authors, ordered by birth year descending, limited to the 3 youngest.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)


category: expressions

prompt: Return a single 1-tuple with the sum of all book prices as a float rounded to 2 decimals.

schema:
Models (app label `bookshop`):

    class Publisher(models.Model):
        name = CharField(max_length=64)
        country = CharField(max_length=2)

    class Author(models.Model):
        name = CharField(max_length=64)
        birth_year = IntegerField()

    class Book(models.Model):
        title = CharField(max_length=128)
        author = ForeignKey(Author, related_name="books")
        publisher = ForeignKey(Publisher, related_name="books")
        year = IntegerField()
        price = DecimalField(max_digits=6, decimal_places=2)
        pages = IntegerField()
        in_stock = BooleanField(default=True)