{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "720d481b-b6d4-4fe7-bd4c-e1421dd21363",
   "metadata": {},
   "outputs": [],
   "source": [
    "from data201 import make_connection, dataframe_query"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2fd3716e-f1e2-418b-9d00-d118220d9022",
   "metadata": {},
   "outputs": [],
   "source": [
    "conn = make_connection('building.ini')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18e16b4f-eab8-4b15-8a3e-c4ada9016516",
   "metadata": {},
   "source": [
    "# Which buildings have managers living in them?\n",
    "### Buildings with managers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6ca9d7c0-96c3-4d2b-88fd-2a27089dd9e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "_, df = dataframe_query(conn,\n",
    "    \"\"\"\n",
    "    SELECT *\n",
    "    FROM building b, manager m\n",
    "    WHERE b.buildingid = m.mresbuildingid\n",
    "    \"\"\"\n",
    ")\n",
    "\n",
    "display(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e39f2a2-bef1-4738-86c0-2e64af21520f",
   "metadata": {},
   "source": [
    "### Data about buildings with managers. Uses a **correlated subquery**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22fae266-3491-4b5b-8fa5-8860e6487674",
   "metadata": {},
   "outputs": [],
   "source": [
    "_, df = dataframe_query(conn, \n",
    "    \"\"\"\n",
    "    SELECT *\n",
    "    FROM building b\n",
    "    WHERE EXISTS (\n",
    "        SELECT *\n",
    "        FROM manager m\n",
    "        WHERE b.buildingid = m.mresbuildingid\n",
    "    )\n",
    "    \"\"\"\n",
    ")\n",
    "\n",
    "display(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6e23c65-0c93-432d-b950-129015d06a6f",
   "metadata": {},
   "source": [
    "# Which buildings do **not** have managers living in them?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac85bcb5-9da7-42bc-b48f-e6d7f67e52f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "_, df = dataframe_query(conn, \n",
    "    \"\"\"\n",
    "    SELECT *\n",
    "    FROM building b\n",
    "    WHERE NOT EXISTS (\n",
    "        SELECT *\n",
    "        FROM manager m\n",
    "        WHERE b.buildingid = m.mresbuildingid\n",
    "    )\n",
    "    \"\"\"\n",
    ")\n",
    "\n",
    "display(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45d81089-8b6c-47e2-a0a3-bbd4960eeffc",
   "metadata": {},
   "outputs": [],
   "source": [
    "conn.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b97a23f9-1db1-48fb-a987-e42e2901c9a7",
   "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
}
