{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f7a8bdc8-d38d-4e7d-b589-7ea8564b794c",
   "metadata": {},
   "source": [
    "## PROBLEM 1 (5 points)\n",
    "### Each store maintains an inventory of films. Which films, if any, are *not* inventoried by either store? Display in a dataframe the film ids and titles, sorted by id."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "032871a1-8141-47bc-a41e-748b72775b20",
   "metadata": {},
   "outputs": [],
   "source": [
    "from data201 import db_connection, df_query\n",
    "conn = db_connection(config_file='sakila.ini')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19a951fd-736f-4264-bc14-6a391cc22b93",
   "metadata": {},
   "source": [
    "### Solution using a `LEFT OUTER JOIN`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5ca19333-bc6c-47a7-933b-93c030c3f3a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_query(conn,\n",
    "    \"\"\"\n",
    "    SELECT film_id, title\n",
    "    FROM film\n",
    "    LEFT OUTER JOIN inventory USING (film_id)\n",
    "    WHERE inventory_id IS NULL\n",
    "    ORDER BY film_id\n",
    "    \"\"\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22508196-8574-4cb4-8ffb-136505fc2751",
   "metadata": {},
   "source": [
    "### Solution using `GROUP BY HAVING` and the `COUNT` function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dbb5d002-6384-4277-842a-a57e3fa171a3",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_query(conn,\n",
    "    \"\"\"\n",
    "    SELECT film_id, title\n",
    "    FROM film\n",
    "    LEFT OUTER JOIN inventory USING (film_id)\n",
    "    GROUP BY film_id\n",
    "    HAVING COUNT(inventory_id) = 0\n",
    "    ORDER BY film_id\n",
    "    \"\"\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "312aa0e3-0c01-4f51-b17f-3b5015f065a5",
   "metadata": {},
   "source": [
    "### Solution using a nested `SELECT`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83e8d7aa-7020-44e2-9bfa-b58d095a631b",
   "metadata": {},
   "outputs": [],
   "source": [
    "df_query(conn,\n",
    "    \"\"\"\n",
    "    SELECT film_id, title\n",
    "    FROM film\n",
    "    WHERE film_id NOT IN (\n",
    "        SELECT DISTINCT film_id\n",
    "        FROM inventory\n",
    "    )\n",
    "    ORDER BY film_id\n",
    "    \"\"\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4f462387-3e16-4fd7-b20b-8a72a7bf2c68",
   "metadata": {},
   "outputs": [],
   "source": [
    "conn.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "663c3566-71ea-4761-803c-e919e3c865a5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}