From 8f20b5cfdb1f36235a155a39267d4c3275110ee2 Mon Sep 17 00:00:00 2001 From: Sara Date: Tue, 31 Oct 2023 17:02:13 +0100 Subject: [PATCH] lab-mysql-subqueries --- main.ipynb | 1346 ++++++++++++++++++++++++++++++++++++++++++++++++++++ main.sql | 0 2 files changed, 1346 insertions(+) create mode 100644 main.ipynb create mode 100644 main.sql diff --git a/main.ipynb b/main.ipynb new file mode 100644 index 0000000..f373bbf --- /dev/null +++ b/main.ipynb @@ -0,0 +1,1346 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import pymysql\n", + "import sqlalchemy as alch # python -m pip install --upgrade 'sqlalchemy<2.0'\n", + "from getpass import getpass\n", + "import pandas as pd\n", + "from getpass import getpass\n", + "import pandas as pd" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "password = getpass()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "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", + "
actor_idfirst_namelast_namelast_update
01PENELOPEGUINESS2006-02-15 04:34:33
12NICKWAHLBERG2006-02-15 04:34:33
23EDCHASE2006-02-15 04:34:33
34JENNIFERDAVIS2006-02-15 04:34:33
45JOHNNYLOLLOBRIGIDA2006-02-15 04:34:33
...............
195196BELAWALKEN2006-02-15 04:34:33
196197REESEWEST2006-02-15 04:34:33
197198MARYKEITEL2006-02-15 04:34:33
198199JULIAFAWCETT2006-02-15 04:34:33
199200THORATEMPLE2006-02-15 04:34:33
\n", + "

200 rows × 4 columns

\n", + "
" + ], + "text/plain": [ + " actor_id first_name last_name last_update\n", + "0 1 PENELOPE GUINESS 2006-02-15 04:34:33\n", + "1 2 NICK WAHLBERG 2006-02-15 04:34:33\n", + "2 3 ED CHASE 2006-02-15 04:34:33\n", + "3 4 JENNIFER DAVIS 2006-02-15 04:34:33\n", + "4 5 JOHNNY LOLLOBRIGIDA 2006-02-15 04:34:33\n", + ".. ... ... ... ...\n", + "195 196 BELA WALKEN 2006-02-15 04:34:33\n", + "196 197 REESE WEST 2006-02-15 04:34:33\n", + "197 198 MARY KEITEL 2006-02-15 04:34:33\n", + "198 199 JULIA FAWCETT 2006-02-15 04:34:33\n", + "199 200 THORA TEMPLE 2006-02-15 04:34:33\n", + "\n", + "[200 rows x 4 columns]" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Sakila = \"sakila\"\n", + "connectionData=f\"mysql+pymysql://root:{password}@localhost/{Sakila}\"\n", + "engine = alch.create_engine(connectionData)\n", + "\n", + "\n", + "query = \"SELECT * FROM actor;\"\n", + "pd.read_sql_query(query, engine)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "1. How many copies of the film Hunchback Impossible exist in the inventory system?" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
TitleCopies
0HUNCHBACK IMPOSSIBLE6
\n", + "
" + ], + "text/plain": [ + " Title Copies\n", + "0 HUNCHBACK IMPOSSIBLE 6" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Which columns? Film\n", + "\n", + "Q1 = \"\"\"\n", + "SELECT film.title AS Title, COUNT(inventory.inventory_id) AS Copies\n", + "FROM film\n", + " JOIN inventory ON film.film_id = inventory.film_id\n", + "WHERE film.title = \"Hunchback Impossible\"\n", + "\"\"\"\n", + "\n", + "df = pd.read_sql_query(Q1, engine)\n", + "df" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "2. List all films whose length is longer than the average of all the films. " + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "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", + "
titlelength
0AFFAIR PREJUDICE117
1AFRICAN EGG130
2AGENT TRUMAN169
3ALAMO VIDEOTAPE126
4ALASKA PHANTOM136
.........
484WORST BANGER185
485WRATH MILE176
486WRONG BEHAVIOR178
487YOUNG LANGUAGE183
488YOUTH KICK179
\n", + "

489 rows × 2 columns

\n", + "
" + ], + "text/plain": [ + " title length\n", + "0 AFFAIR PREJUDICE 117\n", + "1 AFRICAN EGG 130\n", + "2 AGENT TRUMAN 169\n", + "3 ALAMO VIDEOTAPE 126\n", + "4 ALASKA PHANTOM 136\n", + ".. ... ...\n", + "484 WORST BANGER 185\n", + "485 WRATH MILE 176\n", + "486 WRONG BEHAVIOR 178\n", + "487 YOUNG LANGUAGE 183\n", + "488 YOUTH KICK 179\n", + "\n", + "[489 rows x 2 columns]" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Q2_1 = \"\"\"\n", + "SELECT AVG(length)\n", + "FROM film\n", + "\"\"\"\n", + "\n", + "df1 = pd.read_sql_query(Q2_1, engine)\n", + "\n", + "Q2_2 = \"\"\"\n", + "SELECT title, length\n", + "FROM film \n", + "WHERE film.length > (SELECT AVG(length)\n", + " FROM film)\n", + "\"\"\"\n", + "\n", + "df2 = pd.read_sql_query(Q2_2, engine)\n", + "df2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "3. Use subqueries to display all actors who appear in the film Alone Trip." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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", + "
titlefirst_namelast_name
0ALONE TRIPEDCHASE
1ALONE TRIPKARLBERRY
2ALONE TRIPUMAWOOD
3ALONE TRIPWOODYJOLIE
4ALONE TRIPSPENCERDEPP
5ALONE TRIPCHRISDEPP
6ALONE TRIPLAURENCEBULLOCK
7ALONE TRIPRENEEBALL
\n", + "
" + ], + "text/plain": [ + " title first_name last_name\n", + "0 ALONE TRIP ED CHASE\n", + "1 ALONE TRIP KARL BERRY\n", + "2 ALONE TRIP UMA WOOD\n", + "3 ALONE TRIP WOODY JOLIE\n", + "4 ALONE TRIP SPENCER DEPP\n", + "5 ALONE TRIP CHRIS DEPP\n", + "6 ALONE TRIP LAURENCE BULLOCK\n", + "7 ALONE TRIP RENEE BALL" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Q3_1 = \"\"\"\n", + "# SELECT first_name, last_name\n", + "# FROM actor\n", + "# \"\"\"\n", + "# df3_1 = pd.read_sql_query(Q3_1, engine)\n", + "\n", + "Q3_2joins = \"\"\"\n", + "SELECT film.title, actor.first_name, actor.last_name\n", + "FROM actor\n", + "JOIN film_actor ON actor.actor_id = film_actor.actor_id \n", + "JOIN film ON film_actor.film_id = film.film_id\n", + "WHERE film.title = \"Alone Trip\"\n", + "\"\"\"\n", + "\n", + "df3_2 = pd.read_sql_query(Q3_2joins, engine)\n", + "df3_2" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "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", + "
titlefirst_namelast_name
0ALONE TRIPEDCHASE
1ALONE TRIPKARLBERRY
2ALONE TRIPUMAWOOD
3ALONE TRIPWOODYJOLIE
4ALONE TRIPSPENCERDEPP
5ALONE TRIPCHRISDEPP
6ALONE TRIPLAURENCEBULLOCK
7ALONE TRIPRENEEBALL
\n", + "
" + ], + "text/plain": [ + " title first_name last_name\n", + "0 ALONE TRIP ED CHASE\n", + "1 ALONE TRIP KARL BERRY\n", + "2 ALONE TRIP UMA WOOD\n", + "3 ALONE TRIP WOODY JOLIE\n", + "4 ALONE TRIP SPENCER DEPP\n", + "5 ALONE TRIP CHRIS DEPP\n", + "6 ALONE TRIP LAURENCE BULLOCK\n", + "7 ALONE TRIP RENEE BALL" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Q3_2 = \"\"\"\n", + "SELECT film.title, actor.first_name, actor.last_name\n", + "FROM actor\n", + "JOIN film_actor ON actor.actor_id = film_actor.actor_id\n", + "JOIN film ON film_actor.film_id = film.film_id\n", + "WHERE film.title = 'Alone Trip' AND (actor.first_name, actor.last_name) IN (SELECT first_name, last_name FROM actor)\n", + "\n", + "\"\"\"\n", + "\n", + "df3_2 = pd.read_sql_query(Q3_2, engine)\n", + "df3_2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "4. Sales have been lagging among young families, and you wish to target all family movies for a promotion. Identify all movies categorized as family films." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "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", + "
titlefirst_namelast_name
0ALONE TRIPEDCHASE
1ALONE TRIPKARLBERRY
2ALONE TRIPUMAWOOD
3ALONE TRIPWOODYJOLIE
4ALONE TRIPSPENCERDEPP
5ALONE TRIPCHRISDEPP
6ALONE TRIPLAURENCEBULLOCK
7ALONE TRIPRENEEBALL
\n", + "
" + ], + "text/plain": [ + " title first_name last_name\n", + "0 ALONE TRIP ED CHASE\n", + "1 ALONE TRIP KARL BERRY\n", + "2 ALONE TRIP UMA WOOD\n", + "3 ALONE TRIP WOODY JOLIE\n", + "4 ALONE TRIP SPENCER DEPP\n", + "5 ALONE TRIP CHRIS DEPP\n", + "6 ALONE TRIP LAURENCE BULLOCK\n", + "7 ALONE TRIP RENEE BALL" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Q4 = \"\"\"\n", + "SELECT film.title, actor.first_name, actor.last_name\n", + "FROM actor\n", + "JOIN film_actor ON actor.actor_id = film_actor.actor_id\n", + "JOIN film ON film_actor.film_id = film.film_id\n", + "WHERE film.title = 'Alone Trip' AND (actor.first_name, actor.last_name) IN (SELECT first_name, last_name FROM actor)\n", + "\"\"\"\n", + "\n", + "df4 = pd.read_sql_query(Q4, engine)\n", + "df4" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "5. Get name and email from customers from Canada using subqueries. Do the same with joins. Note that to create a join, you will have to identify the correct tables with their primary keys and foreign keys, that will help you get the relevant information." + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "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", + "
first_namelast_nameemailcountry
0DERRICKBOURQUEDERRICK.BOURQUE@sakilacustomer.orgCanada
1DARRELLPOWERDARRELL.POWER@sakilacustomer.orgCanada
2LORETTACARPENTERLORETTA.CARPENTER@sakilacustomer.orgCanada
3CURTISIRBYCURTIS.IRBY@sakilacustomer.orgCanada
4TROYQUIGLEYTROY.QUIGLEY@sakilacustomer.orgCanada
\n", + "
" + ], + "text/plain": [ + " first_name last_name email country\n", + "0 DERRICK BOURQUE DERRICK.BOURQUE@sakilacustomer.org Canada\n", + "1 DARRELL POWER DARRELL.POWER@sakilacustomer.org Canada\n", + "2 LORETTA CARPENTER LORETTA.CARPENTER@sakilacustomer.org Canada\n", + "3 CURTIS IRBY CURTIS.IRBY@sakilacustomer.org Canada\n", + "4 TROY QUIGLEY TROY.QUIGLEY@sakilacustomer.org Canada" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Q5_joins = \"\"\"\n", + "SELECT customer.first_name, customer.last_name, customer.email, country.country\n", + "FROM customer\n", + "JOIN address ON customer.address_id = address.address_id\n", + "JOIN city ON address.city_id = city.city_id\n", + "JOIN country ON city.country_id = country.country_id\n", + "WHERE country.country = \"Canada\"\n", + "\"\"\"\n", + "\n", + "df5 = pd.read_sql_query(Q5_joins, engine)\n", + "df5" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "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", + "
first_namelast_nameemailcountry
0DERRICKBOURQUEDERRICK.BOURQUE@sakilacustomer.orgCanada
1DARRELLPOWERDARRELL.POWER@sakilacustomer.orgCanada
2LORETTACARPENTERLORETTA.CARPENTER@sakilacustomer.orgCanada
3CURTISIRBYCURTIS.IRBY@sakilacustomer.orgCanada
4TROYQUIGLEYTROY.QUIGLEY@sakilacustomer.orgCanada
\n", + "
" + ], + "text/plain": [ + " first_name last_name email country\n", + "0 DERRICK BOURQUE DERRICK.BOURQUE@sakilacustomer.org Canada\n", + "1 DARRELL POWER DARRELL.POWER@sakilacustomer.org Canada\n", + "2 LORETTA CARPENTER LORETTA.CARPENTER@sakilacustomer.org Canada\n", + "3 CURTIS IRBY CURTIS.IRBY@sakilacustomer.org Canada\n", + "4 TROY QUIGLEY TROY.QUIGLEY@sakilacustomer.org Canada" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Q5_1 = \"\"\"\n", + "# SELECT country\n", + "# FROM country \n", + "# WHERE country = \"Canada\"\n", + "# \"\"\"\n", + "# df5 = pd.read_sql_query(Q5_1, engine)\n", + "# df5\n", + "\n", + "Q5_sub = \"\"\"\n", + "SELECT customer.first_name, customer.last_name, customer.email, country.country\n", + "FROM customer\n", + "JOIN address ON customer.address_id = address.address_id\n", + "JOIN city ON address.city_id = city.city_id\n", + "JOIN country ON city.country_id = country.country_id\n", + "WHERE country.country IN (SELECT country FROM country WHERE country = 'Canada')\n", + "\"\"\"\n", + "\n", + "df5 = pd.read_sql_query(Q5_sub, engine)\n", + "df5" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "6. Which are films starred by the most prolific actor? Most prolific actor is defined as the actor that has acted in the most number of films. First you will have to find the most prolific actor and then use that actor_id to find the different films that he/she starred." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "ename": "OperationalError", + "evalue": "(pymysql.err.OperationalError) (1055, \"Expression #1 of HAVING clause is not in GROUP BY clause and contains nonaggregated column 'sakila.actor.first_name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by\")\n[SQL: \nSELECT film.title, CONCAT(first_name, \" \",last_name) AS full_name\nFROM film \nJOIN film_actor ON film.film_id = film_actor.film_id\nJOIN actor ON film_actor.actor_id = actor.actor_id\nWHERE CONCAT(actor.first_name, ' ', actor.last_name) IN (SELECT CONCAT(first_name, ' ', last_name)\n FROM actor\n GROUP BY CONCAT(first_name, ' ', last_name)\n HAVING COUNT(actor_id) > 1)\n]\n(Background on this error at: https://sqlalche.me/e/14/e3q8)", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mOperationalError\u001b[0m Traceback (most recent call last)", + "File \u001b[1;32mc:\\Users\\Sara\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\sqlalchemy\\engine\\base.py:1910\u001b[0m, in \u001b[0;36mConnection._execute_context\u001b[1;34m(self, dialect, constructor, statement, parameters, execution_options, *args, **kw)\u001b[0m\n\u001b[0;32m 1909\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m evt_handled:\n\u001b[1;32m-> 1910\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mdialect\u001b[39m.\u001b[39mdo_execute(\n\u001b[0;32m 1911\u001b[0m cursor, statement, parameters, context\n\u001b[0;32m 1912\u001b[0m )\n\u001b[0;32m 1914\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_has_events \u001b[39mor\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mengine\u001b[39m.\u001b[39m_has_events:\n", + "File \u001b[1;32mc:\\Users\\Sara\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\sqlalchemy\\engine\\default.py:736\u001b[0m, in \u001b[0;36mDefaultDialect.do_execute\u001b[1;34m(self, cursor, statement, parameters, context)\u001b[0m\n\u001b[0;32m 735\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mdo_execute\u001b[39m(\u001b[39mself\u001b[39m, cursor, statement, parameters, context\u001b[39m=\u001b[39m\u001b[39mNone\u001b[39;00m):\n\u001b[1;32m--> 736\u001b[0m cursor\u001b[39m.\u001b[39mexecute(statement, parameters)\n", + "File \u001b[1;32mc:\\Users\\Sara\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\pymysql\\cursors.py:153\u001b[0m, in \u001b[0;36mCursor.execute\u001b[1;34m(self, query, args)\u001b[0m\n\u001b[0;32m 151\u001b[0m query \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mmogrify(query, args)\n\u001b[1;32m--> 153\u001b[0m result \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_query(query)\n\u001b[0;32m 154\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_executed \u001b[39m=\u001b[39m query\n", + "File \u001b[1;32mc:\\Users\\Sara\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\pymysql\\cursors.py:322\u001b[0m, in \u001b[0;36mCursor._query\u001b[1;34m(self, q)\u001b[0m\n\u001b[0;32m 321\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_clear_result()\n\u001b[1;32m--> 322\u001b[0m conn\u001b[39m.\u001b[39mquery(q)\n\u001b[0;32m 323\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_do_get_result()\n", + "File \u001b[1;32mc:\\Users\\Sara\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\pymysql\\connections.py:558\u001b[0m, in \u001b[0;36mConnection.query\u001b[1;34m(self, sql, unbuffered)\u001b[0m\n\u001b[0;32m 557\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_execute_command(COMMAND\u001b[39m.\u001b[39mCOM_QUERY, sql)\n\u001b[1;32m--> 558\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_affected_rows \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_read_query_result(unbuffered\u001b[39m=\u001b[39munbuffered)\n\u001b[0;32m 559\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_affected_rows\n", + "File \u001b[1;32mc:\\Users\\Sara\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\pymysql\\connections.py:822\u001b[0m, in \u001b[0;36mConnection._read_query_result\u001b[1;34m(self, unbuffered)\u001b[0m\n\u001b[0;32m 821\u001b[0m result \u001b[39m=\u001b[39m MySQLResult(\u001b[39mself\u001b[39m)\n\u001b[1;32m--> 822\u001b[0m result\u001b[39m.\u001b[39mread()\n\u001b[0;32m 823\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_result \u001b[39m=\u001b[39m result\n", + "File \u001b[1;32mc:\\Users\\Sara\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\pymysql\\connections.py:1200\u001b[0m, in \u001b[0;36mMySQLResult.read\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 1199\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[1;32m-> 1200\u001b[0m first_packet \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mconnection\u001b[39m.\u001b[39m_read_packet()\n\u001b[0;32m 1202\u001b[0m \u001b[39mif\u001b[39;00m first_packet\u001b[39m.\u001b[39mis_ok_packet():\n", + "File \u001b[1;32mc:\\Users\\Sara\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\pymysql\\connections.py:772\u001b[0m, in \u001b[0;36mConnection._read_packet\u001b[1;34m(self, packet_type)\u001b[0m\n\u001b[0;32m 771\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_result\u001b[39m.\u001b[39munbuffered_active \u001b[39m=\u001b[39m \u001b[39mFalse\u001b[39;00m\n\u001b[1;32m--> 772\u001b[0m packet\u001b[39m.\u001b[39mraise_for_error()\n\u001b[0;32m 773\u001b[0m \u001b[39mreturn\u001b[39;00m packet\n", + "File \u001b[1;32mc:\\Users\\Sara\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\pymysql\\protocol.py:221\u001b[0m, in \u001b[0;36mMysqlPacket.raise_for_error\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 220\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39merrno =\u001b[39m\u001b[39m\"\u001b[39m, errno)\n\u001b[1;32m--> 221\u001b[0m err\u001b[39m.\u001b[39mraise_mysql_exception(\u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_data)\n", + "File \u001b[1;32mc:\\Users\\Sara\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\pymysql\\err.py:143\u001b[0m, in \u001b[0;36mraise_mysql_exception\u001b[1;34m(data)\u001b[0m\n\u001b[0;32m 142\u001b[0m errorclass \u001b[39m=\u001b[39m InternalError \u001b[39mif\u001b[39;00m errno \u001b[39m<\u001b[39m \u001b[39m1000\u001b[39m \u001b[39melse\u001b[39;00m OperationalError\n\u001b[1;32m--> 143\u001b[0m \u001b[39mraise\u001b[39;00m errorclass(errno, errval)\n", + "\u001b[1;31mOperationalError\u001b[0m: (1055, \"Expression #1 of HAVING clause is not in GROUP BY clause and contains nonaggregated column 'sakila.actor.first_name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by\")", + "\nThe above exception was the direct cause of the following exception:\n", + "\u001b[1;31mOperationalError\u001b[0m Traceback (most recent call last)", + "\u001b[1;32mc:\\Users\\Sara\\Ironhack lectures\\lab-mysql-subqueries\\main.ipynb Cell 17\u001b[0m line \u001b[0;36m1\n\u001b[0;32m 1\u001b[0m \u001b[39m# Q6_1 = \"\"\"\u001b[39;00m\n\u001b[0;32m 2\u001b[0m \u001b[39m# SELECT CONCAT(first_name, \" \",last_name) AS full_name\u001b[39;00m\n\u001b[0;32m 3\u001b[0m \u001b[39m# FROM actor\u001b[39;00m\n\u001b[0;32m 4\u001b[0m \u001b[39m# \"\"\"\u001b[39;00m\n\u001b[0;32m 5\u001b[0m \u001b[39m# df6 = pd.read_sql_query(Q6_1, engine)\u001b[39;00m\n\u001b[0;32m 6\u001b[0m \u001b[39m# df6\u001b[39;00m\n\u001b[0;32m 8\u001b[0m Q6_2 \u001b[39m=\u001b[39m \u001b[39m\"\"\"\u001b[39m\n\u001b[0;32m 9\u001b[0m \u001b[39mSELECT film.title, CONCAT(first_name, \u001b[39m\u001b[39m\"\u001b[39m\u001b[39m \u001b[39m\u001b[39m\"\u001b[39m\u001b[39m,last_name) AS full_name\u001b[39m\n\u001b[0;32m 10\u001b[0m \u001b[39mFROM film \u001b[39m\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 16\u001b[0m \u001b[39m HAVING COUNT(actor_id) > 1)\u001b[39m\n\u001b[0;32m 17\u001b[0m \u001b[39m\"\"\"\u001b[39m\n\u001b[1;32m---> 18\u001b[0m df6_2 \u001b[39m=\u001b[39m pd\u001b[39m.\u001b[39mread_sql_query(Q6_2, engine)\n\u001b[0;32m 19\u001b[0m df6_2\n", + "File \u001b[1;32mc:\\Users\\Sara\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\pandas\\io\\sql.py:486\u001b[0m, in \u001b[0;36mread_sql_query\u001b[1;34m(sql, con, index_col, coerce_float, params, parse_dates, chunksize, dtype, dtype_backend)\u001b[0m\n\u001b[0;32m 483\u001b[0m \u001b[39massert\u001b[39;00m dtype_backend \u001b[39mis\u001b[39;00m \u001b[39mnot\u001b[39;00m lib\u001b[39m.\u001b[39mno_default\n\u001b[0;32m 485\u001b[0m \u001b[39mwith\u001b[39;00m pandasSQL_builder(con) \u001b[39mas\u001b[39;00m pandas_sql:\n\u001b[1;32m--> 486\u001b[0m \u001b[39mreturn\u001b[39;00m pandas_sql\u001b[39m.\u001b[39mread_query(\n\u001b[0;32m 487\u001b[0m sql,\n\u001b[0;32m 488\u001b[0m index_col\u001b[39m=\u001b[39mindex_col,\n\u001b[0;32m 489\u001b[0m params\u001b[39m=\u001b[39mparams,\n\u001b[0;32m 490\u001b[0m coerce_float\u001b[39m=\u001b[39mcoerce_float,\n\u001b[0;32m 491\u001b[0m parse_dates\u001b[39m=\u001b[39mparse_dates,\n\u001b[0;32m 492\u001b[0m chunksize\u001b[39m=\u001b[39mchunksize,\n\u001b[0;32m 493\u001b[0m dtype\u001b[39m=\u001b[39mdtype,\n\u001b[0;32m 494\u001b[0m dtype_backend\u001b[39m=\u001b[39mdtype_backend,\n\u001b[0;32m 495\u001b[0m )\n", + "File \u001b[1;32mc:\\Users\\Sara\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\pandas\\io\\sql.py:1776\u001b[0m, in \u001b[0;36mSQLDatabase.read_query\u001b[1;34m(self, sql, index_col, coerce_float, parse_dates, params, chunksize, dtype, dtype_backend)\u001b[0m\n\u001b[0;32m 1719\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mread_query\u001b[39m(\n\u001b[0;32m 1720\u001b[0m \u001b[39mself\u001b[39m,\n\u001b[0;32m 1721\u001b[0m sql: \u001b[39mstr\u001b[39m,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 1728\u001b[0m dtype_backend: DtypeBackend \u001b[39m|\u001b[39m Literal[\u001b[39m\"\u001b[39m\u001b[39mnumpy\u001b[39m\u001b[39m\"\u001b[39m] \u001b[39m=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39mnumpy\u001b[39m\u001b[39m\"\u001b[39m,\n\u001b[0;32m 1729\u001b[0m ) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m DataFrame \u001b[39m|\u001b[39m Iterator[DataFrame]:\n\u001b[0;32m 1730\u001b[0m \u001b[39m \u001b[39m\u001b[39m\"\"\"\u001b[39;00m\n\u001b[0;32m 1731\u001b[0m \u001b[39m Read SQL query into a DataFrame.\u001b[39;00m\n\u001b[0;32m 1732\u001b[0m \n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 1774\u001b[0m \n\u001b[0;32m 1775\u001b[0m \u001b[39m \"\"\"\u001b[39;00m\n\u001b[1;32m-> 1776\u001b[0m result \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mexecute(sql, params)\n\u001b[0;32m 1777\u001b[0m columns \u001b[39m=\u001b[39m result\u001b[39m.\u001b[39mkeys()\n\u001b[0;32m 1779\u001b[0m \u001b[39mif\u001b[39;00m chunksize \u001b[39mis\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mNone\u001b[39;00m:\n", + "File \u001b[1;32mc:\\Users\\Sara\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\pandas\\io\\sql.py:1599\u001b[0m, in \u001b[0;36mSQLDatabase.execute\u001b[1;34m(self, sql, params)\u001b[0m\n\u001b[0;32m 1597\u001b[0m args \u001b[39m=\u001b[39m [] \u001b[39mif\u001b[39;00m params \u001b[39mis\u001b[39;00m \u001b[39mNone\u001b[39;00m \u001b[39melse\u001b[39;00m [params]\n\u001b[0;32m 1598\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39misinstance\u001b[39m(sql, \u001b[39mstr\u001b[39m):\n\u001b[1;32m-> 1599\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mcon\u001b[39m.\u001b[39mexec_driver_sql(sql, \u001b[39m*\u001b[39margs)\n\u001b[0;32m 1600\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mcon\u001b[39m.\u001b[39mexecute(sql, \u001b[39m*\u001b[39margs)\n", + "File \u001b[1;32mc:\\Users\\Sara\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\sqlalchemy\\engine\\base.py:1770\u001b[0m, in \u001b[0;36mConnection.exec_driver_sql\u001b[1;34m(self, statement, parameters, execution_options)\u001b[0m\n\u001b[0;32m 1715\u001b[0m \u001b[39m\u001b[39m\u001b[39mr\u001b[39m\u001b[39m\"\"\"Executes a string SQL statement on the DBAPI cursor directly,\u001b[39;00m\n\u001b[0;32m 1716\u001b[0m \u001b[39mwithout any SQL compilation steps.\u001b[39;00m\n\u001b[0;32m 1717\u001b[0m \n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 1765\u001b[0m \n\u001b[0;32m 1766\u001b[0m \u001b[39m\"\"\"\u001b[39;00m\n\u001b[0;32m 1768\u001b[0m args_10style, kwargs_10style \u001b[39m=\u001b[39m _distill_params_20(parameters)\n\u001b[1;32m-> 1770\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_exec_driver_sql(\n\u001b[0;32m 1771\u001b[0m statement,\n\u001b[0;32m 1772\u001b[0m args_10style,\n\u001b[0;32m 1773\u001b[0m kwargs_10style,\n\u001b[0;32m 1774\u001b[0m execution_options,\n\u001b[0;32m 1775\u001b[0m future\u001b[39m=\u001b[39m\u001b[39mTrue\u001b[39;00m,\n\u001b[0;32m 1776\u001b[0m )\n", + "File \u001b[1;32mc:\\Users\\Sara\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\sqlalchemy\\engine\\base.py:1674\u001b[0m, in \u001b[0;36mConnection._exec_driver_sql\u001b[1;34m(self, statement, multiparams, params, execution_options, future)\u001b[0m\n\u001b[0;32m 1664\u001b[0m (\n\u001b[0;32m 1665\u001b[0m statement,\n\u001b[0;32m 1666\u001b[0m distilled_params,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 1670\u001b[0m statement, distilled_parameters, execution_options\n\u001b[0;32m 1671\u001b[0m )\n\u001b[0;32m 1673\u001b[0m dialect \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mdialect\n\u001b[1;32m-> 1674\u001b[0m ret \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_execute_context(\n\u001b[0;32m 1675\u001b[0m dialect,\n\u001b[0;32m 1676\u001b[0m dialect\u001b[39m.\u001b[39mexecution_ctx_cls\u001b[39m.\u001b[39m_init_statement,\n\u001b[0;32m 1677\u001b[0m statement,\n\u001b[0;32m 1678\u001b[0m distilled_parameters,\n\u001b[0;32m 1679\u001b[0m execution_options,\n\u001b[0;32m 1680\u001b[0m statement,\n\u001b[0;32m 1681\u001b[0m distilled_parameters,\n\u001b[0;32m 1682\u001b[0m )\n\u001b[0;32m 1684\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m future:\n\u001b[0;32m 1685\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_has_events \u001b[39mor\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mengine\u001b[39m.\u001b[39m_has_events:\n", + "File \u001b[1;32mc:\\Users\\Sara\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\sqlalchemy\\engine\\base.py:1953\u001b[0m, in \u001b[0;36mConnection._execute_context\u001b[1;34m(self, dialect, constructor, statement, parameters, execution_options, *args, **kw)\u001b[0m\n\u001b[0;32m 1950\u001b[0m branched\u001b[39m.\u001b[39mclose()\n\u001b[0;32m 1952\u001b[0m \u001b[39mexcept\u001b[39;00m \u001b[39mBaseException\u001b[39;00m \u001b[39mas\u001b[39;00m e:\n\u001b[1;32m-> 1953\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_handle_dbapi_exception(\n\u001b[0;32m 1954\u001b[0m e, statement, parameters, cursor, context\n\u001b[0;32m 1955\u001b[0m )\n\u001b[0;32m 1957\u001b[0m \u001b[39mreturn\u001b[39;00m result\n", + "File \u001b[1;32mc:\\Users\\Sara\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\sqlalchemy\\engine\\base.py:2134\u001b[0m, in \u001b[0;36mConnection._handle_dbapi_exception\u001b[1;34m(self, e, statement, parameters, cursor, context)\u001b[0m\n\u001b[0;32m 2132\u001b[0m util\u001b[39m.\u001b[39mraise_(newraise, with_traceback\u001b[39m=\u001b[39mexc_info[\u001b[39m2\u001b[39m], from_\u001b[39m=\u001b[39me)\n\u001b[0;32m 2133\u001b[0m \u001b[39melif\u001b[39;00m should_wrap:\n\u001b[1;32m-> 2134\u001b[0m util\u001b[39m.\u001b[39mraise_(\n\u001b[0;32m 2135\u001b[0m sqlalchemy_exception, with_traceback\u001b[39m=\u001b[39mexc_info[\u001b[39m2\u001b[39m], from_\u001b[39m=\u001b[39me\n\u001b[0;32m 2136\u001b[0m )\n\u001b[0;32m 2137\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[0;32m 2138\u001b[0m util\u001b[39m.\u001b[39mraise_(exc_info[\u001b[39m1\u001b[39m], with_traceback\u001b[39m=\u001b[39mexc_info[\u001b[39m2\u001b[39m])\n", + "File \u001b[1;32mc:\\Users\\Sara\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\sqlalchemy\\util\\compat.py:211\u001b[0m, in \u001b[0;36mraise_\u001b[1;34m(***failed resolving arguments***)\u001b[0m\n\u001b[0;32m 208\u001b[0m exception\u001b[39m.\u001b[39m__cause__ \u001b[39m=\u001b[39m replace_context\n\u001b[0;32m 210\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[1;32m--> 211\u001b[0m \u001b[39mraise\u001b[39;00m exception\n\u001b[0;32m 212\u001b[0m \u001b[39mfinally\u001b[39;00m:\n\u001b[0;32m 213\u001b[0m \u001b[39m# credit to\u001b[39;00m\n\u001b[0;32m 214\u001b[0m \u001b[39m# https://cosmicpercolator.com/2016/01/13/exception-leaks-in-python-2-and-3/\u001b[39;00m\n\u001b[0;32m 215\u001b[0m \u001b[39m# as the __traceback__ object creates a cycle\u001b[39;00m\n\u001b[0;32m 216\u001b[0m \u001b[39mdel\u001b[39;00m exception, replace_context, from_, with_traceback\n", + "File \u001b[1;32mc:\\Users\\Sara\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\sqlalchemy\\engine\\base.py:1910\u001b[0m, in \u001b[0;36mConnection._execute_context\u001b[1;34m(self, dialect, constructor, statement, parameters, execution_options, *args, **kw)\u001b[0m\n\u001b[0;32m 1908\u001b[0m \u001b[39mbreak\u001b[39;00m\n\u001b[0;32m 1909\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mnot\u001b[39;00m evt_handled:\n\u001b[1;32m-> 1910\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mdialect\u001b[39m.\u001b[39mdo_execute(\n\u001b[0;32m 1911\u001b[0m cursor, statement, parameters, context\n\u001b[0;32m 1912\u001b[0m )\n\u001b[0;32m 1914\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_has_events \u001b[39mor\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mengine\u001b[39m.\u001b[39m_has_events:\n\u001b[0;32m 1915\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mdispatch\u001b[39m.\u001b[39mafter_cursor_execute(\n\u001b[0;32m 1916\u001b[0m \u001b[39mself\u001b[39m,\n\u001b[0;32m 1917\u001b[0m cursor,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 1921\u001b[0m context\u001b[39m.\u001b[39mexecutemany,\n\u001b[0;32m 1922\u001b[0m )\n", + "File \u001b[1;32mc:\\Users\\Sara\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\sqlalchemy\\engine\\default.py:736\u001b[0m, in \u001b[0;36mDefaultDialect.do_execute\u001b[1;34m(self, cursor, statement, parameters, context)\u001b[0m\n\u001b[0;32m 735\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mdo_execute\u001b[39m(\u001b[39mself\u001b[39m, cursor, statement, parameters, context\u001b[39m=\u001b[39m\u001b[39mNone\u001b[39;00m):\n\u001b[1;32m--> 736\u001b[0m cursor\u001b[39m.\u001b[39mexecute(statement, parameters)\n", + "File \u001b[1;32mc:\\Users\\Sara\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\pymysql\\cursors.py:153\u001b[0m, in \u001b[0;36mCursor.execute\u001b[1;34m(self, query, args)\u001b[0m\n\u001b[0;32m 149\u001b[0m \u001b[39mpass\u001b[39;00m\n\u001b[0;32m 151\u001b[0m query \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mmogrify(query, args)\n\u001b[1;32m--> 153\u001b[0m result \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_query(query)\n\u001b[0;32m 154\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_executed \u001b[39m=\u001b[39m query\n\u001b[0;32m 155\u001b[0m \u001b[39mreturn\u001b[39;00m result\n", + "File \u001b[1;32mc:\\Users\\Sara\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\pymysql\\cursors.py:322\u001b[0m, in \u001b[0;36mCursor._query\u001b[1;34m(self, q)\u001b[0m\n\u001b[0;32m 320\u001b[0m conn \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_get_db()\n\u001b[0;32m 321\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_clear_result()\n\u001b[1;32m--> 322\u001b[0m conn\u001b[39m.\u001b[39mquery(q)\n\u001b[0;32m 323\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_do_get_result()\n\u001b[0;32m 324\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mrowcount\n", + "File \u001b[1;32mc:\\Users\\Sara\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\pymysql\\connections.py:558\u001b[0m, in \u001b[0;36mConnection.query\u001b[1;34m(self, sql, unbuffered)\u001b[0m\n\u001b[0;32m 556\u001b[0m sql \u001b[39m=\u001b[39m sql\u001b[39m.\u001b[39mencode(\u001b[39mself\u001b[39m\u001b[39m.\u001b[39mencoding, \u001b[39m\"\u001b[39m\u001b[39msurrogateescape\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[0;32m 557\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_execute_command(COMMAND\u001b[39m.\u001b[39mCOM_QUERY, sql)\n\u001b[1;32m--> 558\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_affected_rows \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_read_query_result(unbuffered\u001b[39m=\u001b[39munbuffered)\n\u001b[0;32m 559\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_affected_rows\n", + "File \u001b[1;32mc:\\Users\\Sara\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\pymysql\\connections.py:822\u001b[0m, in \u001b[0;36mConnection._read_query_result\u001b[1;34m(self, unbuffered)\u001b[0m\n\u001b[0;32m 820\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[0;32m 821\u001b[0m result \u001b[39m=\u001b[39m MySQLResult(\u001b[39mself\u001b[39m)\n\u001b[1;32m--> 822\u001b[0m result\u001b[39m.\u001b[39mread()\n\u001b[0;32m 823\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_result \u001b[39m=\u001b[39m result\n\u001b[0;32m 824\u001b[0m \u001b[39mif\u001b[39;00m result\u001b[39m.\u001b[39mserver_status \u001b[39mis\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mNone\u001b[39;00m:\n", + "File \u001b[1;32mc:\\Users\\Sara\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\pymysql\\connections.py:1200\u001b[0m, in \u001b[0;36mMySQLResult.read\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 1198\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mread\u001b[39m(\u001b[39mself\u001b[39m):\n\u001b[0;32m 1199\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[1;32m-> 1200\u001b[0m first_packet \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mconnection\u001b[39m.\u001b[39m_read_packet()\n\u001b[0;32m 1202\u001b[0m \u001b[39mif\u001b[39;00m first_packet\u001b[39m.\u001b[39mis_ok_packet():\n\u001b[0;32m 1203\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_read_ok_packet(first_packet)\n", + "File \u001b[1;32mc:\\Users\\Sara\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\pymysql\\connections.py:772\u001b[0m, in \u001b[0;36mConnection._read_packet\u001b[1;34m(self, packet_type)\u001b[0m\n\u001b[0;32m 770\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_result \u001b[39mis\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mNone\u001b[39;00m \u001b[39mand\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_result\u001b[39m.\u001b[39munbuffered_active \u001b[39mis\u001b[39;00m \u001b[39mTrue\u001b[39;00m:\n\u001b[0;32m 771\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_result\u001b[39m.\u001b[39munbuffered_active \u001b[39m=\u001b[39m \u001b[39mFalse\u001b[39;00m\n\u001b[1;32m--> 772\u001b[0m packet\u001b[39m.\u001b[39mraise_for_error()\n\u001b[0;32m 773\u001b[0m \u001b[39mreturn\u001b[39;00m packet\n", + "File \u001b[1;32mc:\\Users\\Sara\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\pymysql\\protocol.py:221\u001b[0m, in \u001b[0;36mMysqlPacket.raise_for_error\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 219\u001b[0m \u001b[39mif\u001b[39;00m DEBUG:\n\u001b[0;32m 220\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39merrno =\u001b[39m\u001b[39m\"\u001b[39m, errno)\n\u001b[1;32m--> 221\u001b[0m err\u001b[39m.\u001b[39mraise_mysql_exception(\u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_data)\n", + "File \u001b[1;32mc:\\Users\\Sara\\miniconda3\\envs\\ironhack\\Lib\\site-packages\\pymysql\\err.py:143\u001b[0m, in \u001b[0;36mraise_mysql_exception\u001b[1;34m(data)\u001b[0m\n\u001b[0;32m 141\u001b[0m \u001b[39mif\u001b[39;00m errorclass \u001b[39mis\u001b[39;00m \u001b[39mNone\u001b[39;00m:\n\u001b[0;32m 142\u001b[0m errorclass \u001b[39m=\u001b[39m InternalError \u001b[39mif\u001b[39;00m errno \u001b[39m<\u001b[39m \u001b[39m1000\u001b[39m \u001b[39melse\u001b[39;00m OperationalError\n\u001b[1;32m--> 143\u001b[0m \u001b[39mraise\u001b[39;00m errorclass(errno, errval)\n", + "\u001b[1;31mOperationalError\u001b[0m: (pymysql.err.OperationalError) (1055, \"Expression #1 of HAVING clause is not in GROUP BY clause and contains nonaggregated column 'sakila.actor.first_name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by\")\n[SQL: \nSELECT film.title, CONCAT(first_name, \" \",last_name) AS full_name\nFROM film \nJOIN film_actor ON film.film_id = film_actor.film_id\nJOIN actor ON film_actor.actor_id = actor.actor_id\nWHERE CONCAT(actor.first_name, ' ', actor.last_name) IN (SELECT CONCAT(first_name, ' ', last_name)\n FROM actor\n GROUP BY CONCAT(first_name, ' ', last_name)\n HAVING COUNT(actor_id) > 1)\n]\n(Background on this error at: https://sqlalche.me/e/14/e3q8)" + ] + } + ], + "source": [ + "# Q6_1 = \"\"\"\n", + "# SELECT CONCAT(first_name, \" \",last_name) AS full_name\n", + "# FROM actor\n", + "# \"\"\"\n", + "# df6 = pd.read_sql_query(Q6_1, engine)\n", + "# df6\n", + "\n", + "Q6_2 = \"\"\"\n", + "SELECT film.title, CONCAT(first_name, \" \",last_name) AS full_name\n", + "FROM film \n", + "JOIN film_actor ON film.film_id = film_actor.film_id\n", + "JOIN actor ON film_actor.actor_id = actor.actor_id\n", + "WHERE CONCAT(actor.first_name, ' ', actor.last_name) IN (SELECT CONCAT(first_name, ' ', last_name)\n", + " FROM actor\n", + " GROUP BY CONCAT(first_name, ' ', last_name)\n", + " HAVING COUNT(actor_id) > 1)\n", + "\"\"\"\n", + "df6_2 = pd.read_sql_query(Q6_2, engine)\n", + "df6_2\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "7. Films rented by most profitable customer. You can use the customer table and payment table to find the most profitable customer ie the customer that has made the largest sum of payments" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "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", + "
first_namelast_nametitlemax_payment
0KARENJACKSONSCORPION APOLLO11.99
1VANESSASIMSSCORPION APOLLO11.99
2ALMAAUSTINSHOW LORD11.99
3NICHOLASBARFIELDSTING PERSONAL11.99
4TANYAGILBERTTIES HUNGER11.99
...............
15428GAILKNIGHTCHASING FIGHT0.00
15429BILLGAVINCLEOPATRA DEVIL0.00
15430LUCYWHEELERCURTAIN VIDEOTAPE0.00
15431ALLISONSTANLEYCYCLONE FAMILY0.00
15432ANNETTEOLSONDEER VIRGINIAN0.00
\n", + "

15433 rows × 4 columns

\n", + "
" + ], + "text/plain": [ + " first_name last_name title max_payment\n", + "0 KAREN JACKSON SCORPION APOLLO 11.99\n", + "1 VANESSA SIMS SCORPION APOLLO 11.99\n", + "2 ALMA AUSTIN SHOW LORD 11.99\n", + "3 NICHOLAS BARFIELD STING PERSONAL 11.99\n", + "4 TANYA GILBERT TIES HUNGER 11.99\n", + "... ... ... ... ...\n", + "15428 GAIL KNIGHT CHASING FIGHT 0.00\n", + "15429 BILL GAVIN CLEOPATRA DEVIL 0.00\n", + "15430 LUCY WHEELER CURTAIN VIDEOTAPE 0.00\n", + "15431 ALLISON STANLEY CYCLONE FAMILY 0.00\n", + "15432 ANNETTE OLSON DEER VIRGINIAN 0.00\n", + "\n", + "[15433 rows x 4 columns]" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Q7_2 = \"\"\"\n", + "SELECT MAX(amount)\n", + "FROM payment\n", + "\"\"\"\n", + "\n", + "Q7_1 = \"\"\"\n", + "SELECT customer.first_name, customer.last_name,film.title,MAX(payment.amount) as max_payment\n", + "FROM film\n", + "JOIN inventory ON film.film_id = inventory.film_id\n", + "JOIN rental ON inventory.inventory_id = rental.inventory_id\n", + "JOIN payment ON rental.rental_id = payment.rental_id\n", + "JOIN customer ON payment.customer_id = customer.customer_id \n", + "GROUP BY customer.first_name, customer.last_name, film.title\n", + "ORDER BY max_payment DESC\n", + "\"\"\"\n", + "\n", + "df7_1 = pd.read_sql_query(Q7_1, engine)\n", + "df7_1\n", + "\n", + "df7_1 = pd.read_sql_query(Q7_1, engine)\n", + "df7_1" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "8. Get the `client_id` and the `total_amount_spent` of those clients who spent more than the average of the `total_amount` spent by each client." + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "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", + "
customer_idSUM(payment.amount)
01311.99
111611.99
219511.99
319611.99
420411.99
523711.99
630511.99
736211.99
859111.99
\n", + "
" + ], + "text/plain": [ + " customer_id SUM(payment.amount)\n", + "0 13 11.99\n", + "1 116 11.99\n", + "2 195 11.99\n", + "3 196 11.99\n", + "4 204 11.99\n", + "5 237 11.99\n", + "6 305 11.99\n", + "7 362 11.99\n", + "8 591 11.99" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Q8 = \"\"\"\n", + "SELECT customer.customer_id,SUM(payment.amount)\n", + "FROM customer\n", + "JOIN payment ON customer.customer_id = payment.customer_id\n", + "WHERE payment.amount = (SELECT MAX(amount) FROM payment)\n", + "GROUP BY customer.customer_id\n", + "\"\"\"\n", + "\n", + "df8 = pd.read_sql_query(Q8, engine)\n", + "df8" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "ironhack", + "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.5" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/main.sql b/main.sql new file mode 100644 index 0000000..e69de29