diff --git a/.ipynb_checkpoints/Lab-Sql-9-checkpoint.ipynb b/.ipynb_checkpoints/Lab-Sql-9-checkpoint.ipynb new file mode 100644 index 0000000..989fcbd --- /dev/null +++ b/.ipynb_checkpoints/Lab-Sql-9-checkpoint.ipynb @@ -0,0 +1,768 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 7, + "id": "2509c211", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "········\n" + ] + } + ], + "source": [ + "# Importing libraries\n", + "import pymysql\n", + "from sqlalchemy import create_engine\n", + "import pandas as pd\n", + "import getpass # To get the password without showing the input\n", + "password = getpass.getpass()" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "0e421dc4", + "metadata": {}, + "outputs": [], + "source": [ + "# Create a Python connection with SQL database\n", + "connection_string = 'mysql+pymysql://root:' + password + '@localhost/bank'\n", + "engine = create_engine(connection_string)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "769411ec", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
rental_idrental_dateinventory_idcustomer_idreturn_datestaff_idlast_update
012005-05-24 22:53:303671302005-05-26 22:04:3012006-02-15 21:30:53
122005-05-24 22:54:3315254592005-05-28 19:40:3312006-02-15 21:30:53
232005-05-24 23:03:3917114082005-06-01 22:12:3912006-02-15 21:30:53
342005-05-24 23:04:4124523332005-06-03 01:43:4122006-02-15 21:30:53
452005-05-24 23:05:2120792222005-06-02 04:33:2112006-02-15 21:30:53
........................
115111532005-05-31 21:36:4427255062005-06-10 01:26:4422006-02-15 21:30:53
115211542005-05-31 21:42:092732592005-06-08 16:40:0912006-02-15 21:30:53
115311552005-05-31 22:17:1120482512005-06-04 20:27:1122006-02-15 21:30:53
115411562005-05-31 22:37:344601062005-06-01 23:02:3422006-02-15 21:30:53
115511572005-05-31 22:47:451449612005-06-02 18:01:4512006-02-15 21:30:53
\n", + "

1156 rows × 7 columns

\n", + "
" + ], + "text/plain": [ + " rental_id rental_date inventory_id customer_id \\\n", + "0 1 2005-05-24 22:53:30 367 130 \n", + "1 2 2005-05-24 22:54:33 1525 459 \n", + "2 3 2005-05-24 23:03:39 1711 408 \n", + "3 4 2005-05-24 23:04:41 2452 333 \n", + "4 5 2005-05-24 23:05:21 2079 222 \n", + "... ... ... ... ... \n", + "1151 1153 2005-05-31 21:36:44 2725 506 \n", + "1152 1154 2005-05-31 21:42:09 2732 59 \n", + "1153 1155 2005-05-31 22:17:11 2048 251 \n", + "1154 1156 2005-05-31 22:37:34 460 106 \n", + "1155 1157 2005-05-31 22:47:45 1449 61 \n", + "\n", + " return_date staff_id last_update \n", + "0 2005-05-26 22:04:30 1 2006-02-15 21:30:53 \n", + "1 2005-05-28 19:40:33 1 2006-02-15 21:30:53 \n", + "2 2005-06-01 22:12:39 1 2006-02-15 21:30:53 \n", + "3 2005-06-03 01:43:41 2 2006-02-15 21:30:53 \n", + "4 2005-06-02 04:33:21 1 2006-02-15 21:30:53 \n", + "... ... ... ... \n", + "1151 2005-06-10 01:26:44 2 2006-02-15 21:30:53 \n", + "1152 2005-06-08 16:40:09 1 2006-02-15 21:30:53 \n", + "1153 2005-06-04 20:27:11 2 2006-02-15 21:30:53 \n", + "1154 2005-06-01 23:02:34 2 2006-02-15 21:30:53 \n", + "1155 2005-06-02 18:01:45 1 2006-02-15 21:30:53 \n", + "\n", + "[1156 rows x 7 columns]" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Importing rentals_may table from SQL\n", + "rentals_may = pd.read_sql_query(\"SELECT * FROM SAKILA.RENTALS_MAY\", engine)\n", + "rentals_may" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "df241aa3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
rental_idrental_dateinventory_idcustomer_idreturn_datestaff_idlast_update
011582005-06-14 22:53:3316324162005-06-18 21:37:3322006-02-15 21:30:53
111592005-06-14 22:55:1343955162005-06-17 02:11:1312006-02-15 21:30:53
211602005-06-14 23:00:3427952392005-06-18 01:58:3422006-02-15 21:30:53
311612005-06-14 23:07:0816902852005-06-21 17:12:0812006-02-15 21:30:53
411622005-06-14 23:09:389873102005-06-23 22:00:3812006-02-15 21:30:53
........................
230634652005-06-21 22:10:0114885102005-06-30 21:35:0112006-02-15 21:30:53
230734662005-06-21 22:13:333712262005-06-25 21:01:3322006-02-15 21:30:53
230834672005-06-21 22:19:257295432005-06-27 00:03:2522006-02-15 21:30:53
230934682005-06-21 22:43:4528991002005-06-30 01:49:4512006-02-15 21:30:53
231034692005-06-21 22:48:5940871812005-06-28 19:32:5912006-02-15 21:30:53
\n", + "

2311 rows × 7 columns

\n", + "
" + ], + "text/plain": [ + " rental_id rental_date inventory_id customer_id \\\n", + "0 1158 2005-06-14 22:53:33 1632 416 \n", + "1 1159 2005-06-14 22:55:13 4395 516 \n", + "2 1160 2005-06-14 23:00:34 2795 239 \n", + "3 1161 2005-06-14 23:07:08 1690 285 \n", + "4 1162 2005-06-14 23:09:38 987 310 \n", + "... ... ... ... ... \n", + "2306 3465 2005-06-21 22:10:01 1488 510 \n", + "2307 3466 2005-06-21 22:13:33 371 226 \n", + "2308 3467 2005-06-21 22:19:25 729 543 \n", + "2309 3468 2005-06-21 22:43:45 2899 100 \n", + "2310 3469 2005-06-21 22:48:59 4087 181 \n", + "\n", + " return_date staff_id last_update \n", + "0 2005-06-18 21:37:33 2 2006-02-15 21:30:53 \n", + "1 2005-06-17 02:11:13 1 2006-02-15 21:30:53 \n", + "2 2005-06-18 01:58:34 2 2006-02-15 21:30:53 \n", + "3 2005-06-21 17:12:08 1 2006-02-15 21:30:53 \n", + "4 2005-06-23 22:00:38 1 2006-02-15 21:30:53 \n", + "... ... ... ... \n", + "2306 2005-06-30 21:35:01 1 2006-02-15 21:30:53 \n", + "2307 2005-06-25 21:01:33 2 2006-02-15 21:30:53 \n", + "2308 2005-06-27 00:03:25 2 2006-02-15 21:30:53 \n", + "2309 2005-06-30 01:49:45 1 2006-02-15 21:30:53 \n", + "2310 2005-06-28 19:32:59 1 2006-02-15 21:30:53 \n", + "\n", + "[2311 rows x 7 columns]" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Importing rentals_june table from SQL\n", + "rentals_june = pd.read_sql_query(\"SELECT * FROM SAKILA.RENTALS_JUNE\", engine)\n", + "rentals_june" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "3e0acf5c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
customer_idnumber_of_rentals
01302
14591
24083
33331
42225
.........
5151912
5163511
517101
5181361
519611
\n", + "

520 rows × 2 columns

\n", + "
" + ], + "text/plain": [ + " customer_id number_of_rentals\n", + "0 130 2\n", + "1 459 1\n", + "2 408 3\n", + "3 333 1\n", + "4 222 5\n", + ".. ... ...\n", + "515 191 2\n", + "516 351 1\n", + "517 10 1\n", + "518 136 1\n", + "519 61 1\n", + "\n", + "[520 rows x 2 columns]" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Checking the number of rentals for each customer for May\n", + "query_may = \"select customer_id, count(*) as number_of_rentals \\\n", + "from sakila.rentals_may \\\n", + "group by customer_id\"\n", + "\n", + "number_of_rentals_may = pd.read_sql_query(query_may, engine)\n", + "number_of_rentals_may" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "60dd7178", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
customer_idnumber_of_rentals
04165
15166
22395
32853
43106
.........
5854121
5863351
5872262
588221
5891261
\n", + "

590 rows × 2 columns

\n", + "
" + ], + "text/plain": [ + " customer_id number_of_rentals\n", + "0 416 5\n", + "1 516 6\n", + "2 239 5\n", + "3 285 3\n", + "4 310 6\n", + ".. ... ...\n", + "585 412 1\n", + "586 335 1\n", + "587 226 2\n", + "588 22 1\n", + "589 126 1\n", + "\n", + "[590 rows x 2 columns]" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Checking the number of rentals for each customer for June\n", + "query_june = \"select customer_id, count(*) as number_of_rentals \\\n", + "from sakila.rentals_june \\\n", + "group by customer_id\"\n", + "\n", + "number_of_rentals_june = pd.read_sql_query(query_june, engine)\n", + "number_of_rentals_june" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "475f2018", + "metadata": {}, + "outputs": [], + "source": [ + "# Writing a function that checks if customer borrowed more or less films in the month of June as compared to May\n", + "def check_customer(customer_id):\n", + " if number_of_rentals_june[number_of_rentals_june[\"customer_id\"] == customer_id].iloc[0,1] > number_of_rentals_may[number_of_rentals_may[\"customer_id\"] == customer_id].iloc[0,1]:\n", + " print(\"Customer borrowed more films in June than May\")\n", + " elif number_of_rentals_june[number_of_rentals_june[\"customer_id\"] == customer_id].iloc[0,1] < number_of_rentals_may[number_of_rentals_may[\"customer_id\"] == customer_id].iloc[0,1]:\n", + " print(\"Customer borrowed more films in May than June\")\n", + " else:\n", + " print(\"Customer borrowed the same amount of films in May and June\")" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "id": "c27ef760", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer borrowed more films in June than May\n" + ] + } + ], + "source": [ + "# Applying the function to the customer_id == 100\n", + "check_customer(100)" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "id": "59221aff", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer borrowed more films in May than June\n" + ] + } + ], + "source": [ + "# Applying the function to the customer_id == 150\n", + "check_customer(150)" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "id": "2ce8e568", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer borrowed the same amount of films in May and June\n" + ] + } + ], + "source": [ + "# Applying the function to the customer_id == 70\n", + "check_customer(70)" + ] + } + ], + "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.11.4" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Lab-Sql-9.ipynb b/Lab-Sql-9.ipynb new file mode 100644 index 0000000..989fcbd --- /dev/null +++ b/Lab-Sql-9.ipynb @@ -0,0 +1,768 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 7, + "id": "2509c211", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "········\n" + ] + } + ], + "source": [ + "# Importing libraries\n", + "import pymysql\n", + "from sqlalchemy import create_engine\n", + "import pandas as pd\n", + "import getpass # To get the password without showing the input\n", + "password = getpass.getpass()" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "0e421dc4", + "metadata": {}, + "outputs": [], + "source": [ + "# Create a Python connection with SQL database\n", + "connection_string = 'mysql+pymysql://root:' + password + '@localhost/bank'\n", + "engine = create_engine(connection_string)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "769411ec", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
rental_idrental_dateinventory_idcustomer_idreturn_datestaff_idlast_update
012005-05-24 22:53:303671302005-05-26 22:04:3012006-02-15 21:30:53
122005-05-24 22:54:3315254592005-05-28 19:40:3312006-02-15 21:30:53
232005-05-24 23:03:3917114082005-06-01 22:12:3912006-02-15 21:30:53
342005-05-24 23:04:4124523332005-06-03 01:43:4122006-02-15 21:30:53
452005-05-24 23:05:2120792222005-06-02 04:33:2112006-02-15 21:30:53
........................
115111532005-05-31 21:36:4427255062005-06-10 01:26:4422006-02-15 21:30:53
115211542005-05-31 21:42:092732592005-06-08 16:40:0912006-02-15 21:30:53
115311552005-05-31 22:17:1120482512005-06-04 20:27:1122006-02-15 21:30:53
115411562005-05-31 22:37:344601062005-06-01 23:02:3422006-02-15 21:30:53
115511572005-05-31 22:47:451449612005-06-02 18:01:4512006-02-15 21:30:53
\n", + "

1156 rows × 7 columns

\n", + "
" + ], + "text/plain": [ + " rental_id rental_date inventory_id customer_id \\\n", + "0 1 2005-05-24 22:53:30 367 130 \n", + "1 2 2005-05-24 22:54:33 1525 459 \n", + "2 3 2005-05-24 23:03:39 1711 408 \n", + "3 4 2005-05-24 23:04:41 2452 333 \n", + "4 5 2005-05-24 23:05:21 2079 222 \n", + "... ... ... ... ... \n", + "1151 1153 2005-05-31 21:36:44 2725 506 \n", + "1152 1154 2005-05-31 21:42:09 2732 59 \n", + "1153 1155 2005-05-31 22:17:11 2048 251 \n", + "1154 1156 2005-05-31 22:37:34 460 106 \n", + "1155 1157 2005-05-31 22:47:45 1449 61 \n", + "\n", + " return_date staff_id last_update \n", + "0 2005-05-26 22:04:30 1 2006-02-15 21:30:53 \n", + "1 2005-05-28 19:40:33 1 2006-02-15 21:30:53 \n", + "2 2005-06-01 22:12:39 1 2006-02-15 21:30:53 \n", + "3 2005-06-03 01:43:41 2 2006-02-15 21:30:53 \n", + "4 2005-06-02 04:33:21 1 2006-02-15 21:30:53 \n", + "... ... ... ... \n", + "1151 2005-06-10 01:26:44 2 2006-02-15 21:30:53 \n", + "1152 2005-06-08 16:40:09 1 2006-02-15 21:30:53 \n", + "1153 2005-06-04 20:27:11 2 2006-02-15 21:30:53 \n", + "1154 2005-06-01 23:02:34 2 2006-02-15 21:30:53 \n", + "1155 2005-06-02 18:01:45 1 2006-02-15 21:30:53 \n", + "\n", + "[1156 rows x 7 columns]" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Importing rentals_may table from SQL\n", + "rentals_may = pd.read_sql_query(\"SELECT * FROM SAKILA.RENTALS_MAY\", engine)\n", + "rentals_may" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "df241aa3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
rental_idrental_dateinventory_idcustomer_idreturn_datestaff_idlast_update
011582005-06-14 22:53:3316324162005-06-18 21:37:3322006-02-15 21:30:53
111592005-06-14 22:55:1343955162005-06-17 02:11:1312006-02-15 21:30:53
211602005-06-14 23:00:3427952392005-06-18 01:58:3422006-02-15 21:30:53
311612005-06-14 23:07:0816902852005-06-21 17:12:0812006-02-15 21:30:53
411622005-06-14 23:09:389873102005-06-23 22:00:3812006-02-15 21:30:53
........................
230634652005-06-21 22:10:0114885102005-06-30 21:35:0112006-02-15 21:30:53
230734662005-06-21 22:13:333712262005-06-25 21:01:3322006-02-15 21:30:53
230834672005-06-21 22:19:257295432005-06-27 00:03:2522006-02-15 21:30:53
230934682005-06-21 22:43:4528991002005-06-30 01:49:4512006-02-15 21:30:53
231034692005-06-21 22:48:5940871812005-06-28 19:32:5912006-02-15 21:30:53
\n", + "

2311 rows × 7 columns

\n", + "
" + ], + "text/plain": [ + " rental_id rental_date inventory_id customer_id \\\n", + "0 1158 2005-06-14 22:53:33 1632 416 \n", + "1 1159 2005-06-14 22:55:13 4395 516 \n", + "2 1160 2005-06-14 23:00:34 2795 239 \n", + "3 1161 2005-06-14 23:07:08 1690 285 \n", + "4 1162 2005-06-14 23:09:38 987 310 \n", + "... ... ... ... ... \n", + "2306 3465 2005-06-21 22:10:01 1488 510 \n", + "2307 3466 2005-06-21 22:13:33 371 226 \n", + "2308 3467 2005-06-21 22:19:25 729 543 \n", + "2309 3468 2005-06-21 22:43:45 2899 100 \n", + "2310 3469 2005-06-21 22:48:59 4087 181 \n", + "\n", + " return_date staff_id last_update \n", + "0 2005-06-18 21:37:33 2 2006-02-15 21:30:53 \n", + "1 2005-06-17 02:11:13 1 2006-02-15 21:30:53 \n", + "2 2005-06-18 01:58:34 2 2006-02-15 21:30:53 \n", + "3 2005-06-21 17:12:08 1 2006-02-15 21:30:53 \n", + "4 2005-06-23 22:00:38 1 2006-02-15 21:30:53 \n", + "... ... ... ... \n", + "2306 2005-06-30 21:35:01 1 2006-02-15 21:30:53 \n", + "2307 2005-06-25 21:01:33 2 2006-02-15 21:30:53 \n", + "2308 2005-06-27 00:03:25 2 2006-02-15 21:30:53 \n", + "2309 2005-06-30 01:49:45 1 2006-02-15 21:30:53 \n", + "2310 2005-06-28 19:32:59 1 2006-02-15 21:30:53 \n", + "\n", + "[2311 rows x 7 columns]" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Importing rentals_june table from SQL\n", + "rentals_june = pd.read_sql_query(\"SELECT * FROM SAKILA.RENTALS_JUNE\", engine)\n", + "rentals_june" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "3e0acf5c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
customer_idnumber_of_rentals
01302
14591
24083
33331
42225
.........
5151912
5163511
517101
5181361
519611
\n", + "

520 rows × 2 columns

\n", + "
" + ], + "text/plain": [ + " customer_id number_of_rentals\n", + "0 130 2\n", + "1 459 1\n", + "2 408 3\n", + "3 333 1\n", + "4 222 5\n", + ".. ... ...\n", + "515 191 2\n", + "516 351 1\n", + "517 10 1\n", + "518 136 1\n", + "519 61 1\n", + "\n", + "[520 rows x 2 columns]" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Checking the number of rentals for each customer for May\n", + "query_may = \"select customer_id, count(*) as number_of_rentals \\\n", + "from sakila.rentals_may \\\n", + "group by customer_id\"\n", + "\n", + "number_of_rentals_may = pd.read_sql_query(query_may, engine)\n", + "number_of_rentals_may" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "60dd7178", + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
customer_idnumber_of_rentals
04165
15166
22395
32853
43106
.........
5854121
5863351
5872262
588221
5891261
\n", + "

590 rows × 2 columns

\n", + "
" + ], + "text/plain": [ + " customer_id number_of_rentals\n", + "0 416 5\n", + "1 516 6\n", + "2 239 5\n", + "3 285 3\n", + "4 310 6\n", + ".. ... ...\n", + "585 412 1\n", + "586 335 1\n", + "587 226 2\n", + "588 22 1\n", + "589 126 1\n", + "\n", + "[590 rows x 2 columns]" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Checking the number of rentals for each customer for June\n", + "query_june = \"select customer_id, count(*) as number_of_rentals \\\n", + "from sakila.rentals_june \\\n", + "group by customer_id\"\n", + "\n", + "number_of_rentals_june = pd.read_sql_query(query_june, engine)\n", + "number_of_rentals_june" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "475f2018", + "metadata": {}, + "outputs": [], + "source": [ + "# Writing a function that checks if customer borrowed more or less films in the month of June as compared to May\n", + "def check_customer(customer_id):\n", + " if number_of_rentals_june[number_of_rentals_june[\"customer_id\"] == customer_id].iloc[0,1] > number_of_rentals_may[number_of_rentals_may[\"customer_id\"] == customer_id].iloc[0,1]:\n", + " print(\"Customer borrowed more films in June than May\")\n", + " elif number_of_rentals_june[number_of_rentals_june[\"customer_id\"] == customer_id].iloc[0,1] < number_of_rentals_may[number_of_rentals_may[\"customer_id\"] == customer_id].iloc[0,1]:\n", + " print(\"Customer borrowed more films in May than June\")\n", + " else:\n", + " print(\"Customer borrowed the same amount of films in May and June\")" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "id": "c27ef760", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer borrowed more films in June than May\n" + ] + } + ], + "source": [ + "# Applying the function to the customer_id == 100\n", + "check_customer(100)" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "id": "59221aff", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer borrowed more films in May than June\n" + ] + } + ], + "source": [ + "# Applying the function to the customer_id == 150\n", + "check_customer(150)" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "id": "2ce8e568", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer borrowed the same amount of films in May and June\n" + ] + } + ], + "source": [ + "# Applying the function to the customer_id == 70\n", + "check_customer(70)" + ] + } + ], + "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.11.4" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Lab-Sql-9.sql b/Lab-Sql-9.sql new file mode 100644 index 0000000..8f71316 --- /dev/null +++ b/Lab-Sql-9.sql @@ -0,0 +1,23 @@ +-- 1) Creating a table rentals_may to store the data from rental table with information for the month of May +-- 2) Inserting values in the table rentals_may using the table rental, filtering values only for the month of May +create table sakila.rentals_may as +select * +from sakila.rental +where date_format(rental_date, "%M") = "May"; + +-- 3) Creating a table rentals_june to store the data from rental table with information for the month of June +-- 4) Inserting values in the table rentals_june using the table rental, filtering values only for the month of June +create table sakila.rentals_june as +select * +from sakila.rental +where date_format(rental_date, "%M") = "June"; + +-- 5) Checking the number of rentals for each customer for May +select customer_id, count(*) as number_of_rentals +from sakila.rentals_may +group by customer_id; + +-- 6) Checking the number of rentals for each customer for June +select customer_id, count(*) as number_of_rentals +from sakila.rentals_june +group by customer_id; \ No newline at end of file