From a39e540d67379c6668531a9fb00685a7b171bb20 Mon Sep 17 00:00:00 2001 From: Manish Date: Thu, 23 Jun 2022 08:58:37 +0200 Subject: [PATCH] Solution --- actual_output.txt | 65 +++++++++++ expected_output.txt | 65 +++++++++++ games.csv | 33 ++++++ games_test.csv | 5 + insert_data.sh | 64 +++++++++++ queries.sh | 41 +++++++ worldcup.sql | 258 ++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 531 insertions(+) create mode 100644 actual_output.txt create mode 100644 expected_output.txt create mode 100644 games.csv create mode 100644 games_test.csv create mode 100644 insert_data.sh create mode 100644 queries.sh create mode 100644 worldcup.sql diff --git a/actual_output.txt b/actual_output.txt new file mode 100644 index 0000000..526632f --- /dev/null +++ b/actual_output.txt @@ -0,0 +1,65 @@ + +Total number of goals in all games from winning teams: +68 + +Total number of goals in all games from both teams combined: +90 + +Average number of goals in all games from the winning teams: +2.1250000000000000 + +Average number of goals in all games from the winning teams rounded to two decimal places: +2.13 + +Average number of goals in all games from both teams: +2.8125000000000000 + +Most goals scored in a single game by one team: +7 + +Number of games where the winning team scored more than two goals: +6 + +Winner of the 2018 tournament team name: +France + +List of teams who played in the 2014 'Eighth-Final' round: +Algeria +Argentina +Belgium +Brazil +Chile +Colombia +Costa Rica +France +Germany +Greece +Mexico +Netherlands +Nigeria +Switzerland +United States +Uruguay + +List of unique winning team names in the whole data set: +Argentina +Belgium +Brazil +Colombia +Costa Rica +Croatia +England +France +Germany +Netherlands +Russia +Sweden +Uruguay + +Year and team name of all the champions: +2014|Germany +2018|France + +List of teams that start with 'Co': +Colombia +Costa Rica diff --git a/expected_output.txt b/expected_output.txt new file mode 100644 index 0000000..526632f --- /dev/null +++ b/expected_output.txt @@ -0,0 +1,65 @@ + +Total number of goals in all games from winning teams: +68 + +Total number of goals in all games from both teams combined: +90 + +Average number of goals in all games from the winning teams: +2.1250000000000000 + +Average number of goals in all games from the winning teams rounded to two decimal places: +2.13 + +Average number of goals in all games from both teams: +2.8125000000000000 + +Most goals scored in a single game by one team: +7 + +Number of games where the winning team scored more than two goals: +6 + +Winner of the 2018 tournament team name: +France + +List of teams who played in the 2014 'Eighth-Final' round: +Algeria +Argentina +Belgium +Brazil +Chile +Colombia +Costa Rica +France +Germany +Greece +Mexico +Netherlands +Nigeria +Switzerland +United States +Uruguay + +List of unique winning team names in the whole data set: +Argentina +Belgium +Brazil +Colombia +Costa Rica +Croatia +England +France +Germany +Netherlands +Russia +Sweden +Uruguay + +Year and team name of all the champions: +2014|Germany +2018|France + +List of teams that start with 'Co': +Colombia +Costa Rica diff --git a/games.csv b/games.csv new file mode 100644 index 0000000..40a829c --- /dev/null +++ b/games.csv @@ -0,0 +1,33 @@ +year,round,winner,opponent,winner_goals,opponent_goals +2018,Final,France,Croatia,4,2 +2018,Third Place,Belgium,England,2,0 +2018,Semi-Final,Croatia,England,2,1 +2018,Semi-Final,France,Belgium,1,0 +2018,Quarter-Final,Croatia,Russia,3,2 +2018,Quarter-Final,England,Sweden,2,0 +2018,Quarter-Final,Belgium,Brazil,2,1 +2018,Quarter-Final,France,Uruguay,2,0 +2018,Eighth-Final,England,Colombia,2,1 +2018,Eighth-Final,Sweden,Switzerland,1,0 +2018,Eighth-Final,Belgium,Japan,3,2 +2018,Eighth-Final,Brazil,Mexico,2,0 +2018,Eighth-Final,Croatia,Denmark,2,1 +2018,Eighth-Final,Russia,Spain,2,1 +2018,Eighth-Final,Uruguay,Portugal,2,1 +2018,Eighth-Final,France,Argentina,4,3 +2014,Final,Germany,Argentina,1,0 +2014,Third Place,Netherlands,Brazil,3,0 +2014,Semi-Final,Argentina,Netherlands,1,0 +2014,Semi-Final,Germany,Brazil,7,1 +2014,Quarter-Final,Netherlands,Costa Rica,1,0 +2014,Quarter-Final,Argentina,Belgium,1,0 +2014,Quarter-Final,Brazil,Colombia,2,1 +2014,Quarter-Final,Germany,France,1,0 +2014,Eighth-Final,Brazil,Chile,2,1 +2014,Eighth-Final,Colombia,Uruguay,2,0 +2014,Eighth-Final,France,Nigeria,2,0 +2014,Eighth-Final,Germany,Algeria,2,1 +2014,Eighth-Final,Netherlands,Mexico,2,1 +2014,Eighth-Final,Costa Rica,Greece,2,1 +2014,Eighth-Final,Argentina,Switzerland,1,0 +2014,Eighth-Final,Belgium,United States,2,1 diff --git a/games_test.csv b/games_test.csv new file mode 100644 index 0000000..2d3543e --- /dev/null +++ b/games_test.csv @@ -0,0 +1,5 @@ +year,round,winner,opponent,winner_goals,opponent_goals +2018,Final,France,Croatia,4,2 +2018,Third Place,Belgium,England,2,0 +2018,Semi-Final,Croatia,England,2,1 +2018,Semi-Final,France,Belgium,1,0 diff --git a/insert_data.sh b/insert_data.sh new file mode 100644 index 0000000..fa28c5d --- /dev/null +++ b/insert_data.sh @@ -0,0 +1,64 @@ +#! /bin/bash + +if [[ $1 == "test" ]] +then + PSQL="psql --username=postgres --dbname=worldcuptest -t --no-align -c" +else + PSQL="psql --username=freecodecamp --dbname=worldcup -t --no-align -c" +fi + +# Do not change code above this line. Use the PSQL variable above to query your database. +echo $($PSQL "TRUNCATE teams, games") +cat games_test.csv | while IFS="," read YEAR ROUND WINNER OPPONENT WINNER_GOALS OPPONENT_GOALS +do + if [[ $YEAR != year ]] + then + echo $YEAR : $ROUND : $WINNER : $OPPONENT : $WINNER_GOAL : $OPPONENT_GOAL + # insert teams + WINNER_ID=$($PSQL "SELECT team_id FROM teams WHERE name='$WINNER'") + + if [[ -z $WINNER_ID ]] + then + INSERT_RESULT=$($PSQL "INSERT INTO teams(name) VALUES('$WINNER')") + + if [[ $INSERT_RESULT == "INSERT 0 1" ]] + then + echo Inserted team, $WINNER + WINNER_ID=$($PSQL "SELECT team_id FROM teams WHERE name='$WINNER'") + else + echo Insert failed: $INSERT_RESULT + exit + fi + else + echo Already exist, $WINNER : $WINNER_ID + fi + + OPPONENT_ID=$($PSQL "SELECT team_id FROM teams WHERE name='$OPPONENT'") + + if [[ -z $OPPONENT_ID ]] + then + INSERT_RESULT=$($PSQL "INSERT INTO teams(name) VALUES('$OPPONENT')") + + if [[ $INSERT_RESULT == "INSERT 0 1" ]] + then + echo Inserted team, $OPPONENT + OPPONENT_ID=$($PSQL "SELECT team_id FROM teams WHERE name='$OPPONENT'") + else + echo Insert failed: $INSERT_RESULT + exit + fi + else + echo Already exist, $OPPONENT : $OPPONENT_ID + fi + + # insert games using teams id + GAME_INSERT_RESULT=$($PSQL "INSERT INTO games(year, round, winner_id, opponent_id, winner_goals, opponent_goals) VALUES($YEAR, '$ROUND', $WINNER_ID, $OPPONENT_ID, $WINNER_GOALS, $OPPONENT_GOALS)") + if [[ $GAME_INSERT_RESULT == "INSERT 0 1" ]] + then + echo Inssert succesful, $GAME_INSERT_RESULT : $YEAR : $ROUND : $WINNER_ID : $OPPONENT_ID : $WINNER_GOALS : $OPPONENET_GOALS + else + echo Game Insert Failed, $GAME_INSERT_RESULT : $YEAR : $ROUND : $WINNER_ID : $OPPONENT_ID : $WINNER_GOALS : $OPPONENET_GOALS + exit + fi + fi +done diff --git a/queries.sh b/queries.sh new file mode 100644 index 0000000..0516fa7 --- /dev/null +++ b/queries.sh @@ -0,0 +1,41 @@ +#! /bin/bash + +PSQL="psql --username=freecodecamp --dbname=worldcup --no-align --tuples-only -c" + +# Do not change code above this line. Use the PSQL variable above to query your database. + +echo -e "\nTotal number of goals in all games from winning teams:" +echo "$($PSQL "SELECT SUM(winner_goals) FROM games")" + +echo -e "\nTotal number of goals in all games from both teams combined:" +echo "$($PSQL "SELECT SUM(winner_goals) + SUM(opponent_goals) FROM games")" + +echo -e "\nAverage number of goals in all games from the winning teams:" +echo "$($PSQL "SELECT AVG(winner_goals) FROM games")" + +echo -e "\nAverage number of goals in all games from the winning teams rounded to two decimal places:" +echo "$($PSQL "SELECT ROUND(AVG(winner_goals), 2) FROM games")" + +echo -e "\nAverage number of goals in all games from both teams:" +echo "$($PSQL "SELECT AVG(winner_goals + opponent_goals) FROM games")" + +echo -e "\nMost goals scored in a single game by one team:" +echo "$($PSQL "SELECT MAX(winner_goals) FROM games")" + +echo -e "\nNumber of games where the winning team scored more than two goals:" +echo $($PSQL "SELECT COUNT(*) FROM games WHERE winner_goals > 2") + +echo -e "\nWinner of the 2018 tournament team name:" +echo "$($PSQL "SELECT name FROM games FULL JOIN teams ON games.winner_id = teams.team_id WHERE year=2018 AND round='Final'")" + +echo -e "\nList of teams who played in the 2014 'Eighth-Final' round:" +echo -e "$($PSQL "(SELECT name FROM games INNER JOIN teams ON games.winner_id = teams.team_id WHERE year=2014 AND round='Eighth-Final') UNION (SELECT name FROM games FULL JOIN teams ON games.opponent_id = teams.team_id WHERE year=2014 AND round='Eighth-Final') ORDER BY name")" + +echo -e "\nList of unique winning team names in the whole data set:" +echo -e "$($PSQL "SELECT DISTINCT(name) FROM games INNER JOIN teams ON games.winner_id = teams.team_id ORDER BY name")" + +echo -e "\nYear and team name of all the champions:" +echo -e "$($PSQL "SELECT year, name FROM games INNER JOIN teams ON games.winner_id = teams.team_id WHERE round='Final' ORDER BY year")" + +echo -e "\nList of teams that start with 'Co':" +echo -e "$($PSQL "SELECT name FROM teams WHERE name LIKE 'Co%'")" diff --git a/worldcup.sql b/worldcup.sql new file mode 100644 index 0000000..7d3d7e3 --- /dev/null +++ b/worldcup.sql @@ -0,0 +1,258 @@ +-- +-- PostgreSQL database dump +-- + +-- Dumped from database version 12.9 (Ubuntu 12.9-2.pgdg20.04+1) +-- Dumped by pg_dump version 12.9 (Ubuntu 12.9-2.pgdg20.04+1) + +SET statement_timeout = 0; +SET lock_timeout = 0; +SET idle_in_transaction_session_timeout = 0; +SET client_encoding = 'UTF8'; +SET standard_conforming_strings = on; +SELECT pg_catalog.set_config('search_path', '', false); +SET check_function_bodies = false; +SET xmloption = content; +SET client_min_messages = warning; +SET row_security = off; + +DROP DATABASE worldcup; +-- +-- Name: worldcup; Type: DATABASE; Schema: -; Owner: freecodecamp +-- + +CREATE DATABASE worldcup WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'C.UTF-8' LC_CTYPE = 'C.UTF-8'; + + +ALTER DATABASE worldcup OWNER TO freecodecamp; + +\connect worldcup + +SET statement_timeout = 0; +SET lock_timeout = 0; +SET idle_in_transaction_session_timeout = 0; +SET client_encoding = 'UTF8'; +SET standard_conforming_strings = on; +SELECT pg_catalog.set_config('search_path', '', false); +SET check_function_bodies = false; +SET xmloption = content; +SET client_min_messages = warning; +SET row_security = off; + +SET default_tablespace = ''; + +SET default_table_access_method = heap; + +-- +-- Name: games; Type: TABLE; Schema: public; Owner: freecodecamp +-- + +CREATE TABLE public.games ( + game_id integer NOT NULL, + year integer NOT NULL, + round character varying(100) NOT NULL, + winner_id integer NOT NULL, + opponent_id integer NOT NULL, + winner_goals integer NOT NULL, + opponent_goals integer NOT NULL +); + + +ALTER TABLE public.games OWNER TO freecodecamp; + +-- +-- Name: games_game_id_seq; Type: SEQUENCE; Schema: public; Owner: freecodecamp +-- + +CREATE SEQUENCE public.games_game_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE public.games_game_id_seq OWNER TO freecodecamp; + +-- +-- Name: games_game_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: freecodecamp +-- + +ALTER SEQUENCE public.games_game_id_seq OWNED BY public.games.game_id; + + +-- +-- Name: teams; Type: TABLE; Schema: public; Owner: freecodecamp +-- + +CREATE TABLE public.teams ( + team_id integer NOT NULL, + name character varying(100) NOT NULL +); + + +ALTER TABLE public.teams OWNER TO freecodecamp; + +-- +-- Name: teams_team_id_seq; Type: SEQUENCE; Schema: public; Owner: freecodecamp +-- + +CREATE SEQUENCE public.teams_team_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE public.teams_team_id_seq OWNER TO freecodecamp; + +-- +-- Name: teams_team_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: freecodecamp +-- + +ALTER SEQUENCE public.teams_team_id_seq OWNED BY public.teams.team_id; + + +-- +-- Name: games game_id; Type: DEFAULT; Schema: public; Owner: freecodecamp +-- + +ALTER TABLE ONLY public.games ALTER COLUMN game_id SET DEFAULT nextval('public.games_game_id_seq'::regclass); + + +-- +-- Name: teams team_id; Type: DEFAULT; Schema: public; Owner: freecodecamp +-- + +ALTER TABLE ONLY public.teams ALTER COLUMN team_id SET DEFAULT nextval('public.teams_team_id_seq'::regclass); + + +-- +-- Data for Name: games; Type: TABLE DATA; Schema: public; Owner: freecodecamp +-- + +INSERT INTO public.games VALUES (9, 2018, 'Final', 27, 28, 4, 2); +INSERT INTO public.games VALUES (10, 2018, 'Third Place', 29, 30, 2, 0); +INSERT INTO public.games VALUES (11, 2018, 'Semi-Final', 28, 30, 2, 1); +INSERT INTO public.games VALUES (12, 2018, 'Semi-Final', 27, 29, 1, 0); +INSERT INTO public.games VALUES (13, 2018, 'Quarter-Final', 28, 31, 3, 2); +INSERT INTO public.games VALUES (14, 2018, 'Quarter-Final', 30, 32, 2, 0); +INSERT INTO public.games VALUES (15, 2018, 'Quarter-Final', 29, 33, 2, 1); +INSERT INTO public.games VALUES (16, 2018, 'Quarter-Final', 27, 34, 2, 0); +INSERT INTO public.games VALUES (17, 2018, 'Eighth-Final', 30, 35, 2, 1); +INSERT INTO public.games VALUES (18, 2018, 'Eighth-Final', 32, 36, 1, 0); +INSERT INTO public.games VALUES (19, 2018, 'Eighth-Final', 29, 37, 3, 2); +INSERT INTO public.games VALUES (20, 2018, 'Eighth-Final', 33, 38, 2, 0); +INSERT INTO public.games VALUES (21, 2018, 'Eighth-Final', 28, 39, 2, 1); +INSERT INTO public.games VALUES (22, 2018, 'Eighth-Final', 31, 40, 2, 1); +INSERT INTO public.games VALUES (23, 2018, 'Eighth-Final', 34, 41, 2, 1); +INSERT INTO public.games VALUES (24, 2018, 'Eighth-Final', 27, 42, 4, 3); +INSERT INTO public.games VALUES (25, 2014, 'Final', 43, 42, 1, 0); +INSERT INTO public.games VALUES (26, 2014, 'Third Place', 44, 33, 3, 0); +INSERT INTO public.games VALUES (27, 2014, 'Semi-Final', 42, 44, 1, 0); +INSERT INTO public.games VALUES (28, 2014, 'Semi-Final', 43, 33, 7, 1); +INSERT INTO public.games VALUES (29, 2014, 'Quarter-Final', 44, 45, 1, 0); +INSERT INTO public.games VALUES (30, 2014, 'Quarter-Final', 42, 29, 1, 0); +INSERT INTO public.games VALUES (31, 2014, 'Quarter-Final', 33, 35, 2, 1); +INSERT INTO public.games VALUES (32, 2014, 'Quarter-Final', 43, 27, 1, 0); +INSERT INTO public.games VALUES (33, 2014, 'Eighth-Final', 33, 46, 2, 1); +INSERT INTO public.games VALUES (34, 2014, 'Eighth-Final', 35, 34, 2, 0); +INSERT INTO public.games VALUES (35, 2014, 'Eighth-Final', 27, 47, 2, 0); +INSERT INTO public.games VALUES (36, 2014, 'Eighth-Final', 43, 48, 2, 1); +INSERT INTO public.games VALUES (37, 2014, 'Eighth-Final', 44, 38, 2, 1); +INSERT INTO public.games VALUES (38, 2014, 'Eighth-Final', 45, 49, 2, 1); +INSERT INTO public.games VALUES (39, 2014, 'Eighth-Final', 42, 36, 1, 0); +INSERT INTO public.games VALUES (40, 2014, 'Eighth-Final', 29, 50, 2, 1); + + +-- +-- Data for Name: teams; Type: TABLE DATA; Schema: public; Owner: freecodecamp +-- + +INSERT INTO public.teams VALUES (27, 'France'); +INSERT INTO public.teams VALUES (28, 'Croatia'); +INSERT INTO public.teams VALUES (29, 'Belgium'); +INSERT INTO public.teams VALUES (30, 'England'); +INSERT INTO public.teams VALUES (31, 'Russia'); +INSERT INTO public.teams VALUES (32, 'Sweden'); +INSERT INTO public.teams VALUES (33, 'Brazil'); +INSERT INTO public.teams VALUES (34, 'Uruguay'); +INSERT INTO public.teams VALUES (35, 'Colombia'); +INSERT INTO public.teams VALUES (36, 'Switzerland'); +INSERT INTO public.teams VALUES (37, 'Japan'); +INSERT INTO public.teams VALUES (38, 'Mexico'); +INSERT INTO public.teams VALUES (39, 'Denmark'); +INSERT INTO public.teams VALUES (40, 'Spain'); +INSERT INTO public.teams VALUES (41, 'Portugal'); +INSERT INTO public.teams VALUES (42, 'Argentina'); +INSERT INTO public.teams VALUES (43, 'Germany'); +INSERT INTO public.teams VALUES (44, 'Netherlands'); +INSERT INTO public.teams VALUES (45, 'Costa Rica'); +INSERT INTO public.teams VALUES (46, 'Chile'); +INSERT INTO public.teams VALUES (47, 'Nigeria'); +INSERT INTO public.teams VALUES (48, 'Algeria'); +INSERT INTO public.teams VALUES (49, 'Greece'); +INSERT INTO public.teams VALUES (50, 'United States'); + + +-- +-- Name: games_game_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp +-- + +SELECT pg_catalog.setval('public.games_game_id_seq', 40, true); + + +-- +-- Name: teams_team_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp +-- + +SELECT pg_catalog.setval('public.teams_team_id_seq', 50, true); + + +-- +-- Name: games games_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp +-- + +ALTER TABLE ONLY public.games + ADD CONSTRAINT games_pkey PRIMARY KEY (game_id); + + +-- +-- Name: teams teams_name_key; Type: CONSTRAINT; Schema: public; Owner: freecodecamp +-- + +ALTER TABLE ONLY public.teams + ADD CONSTRAINT teams_name_key UNIQUE (name); + + +-- +-- Name: teams teams_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp +-- + +ALTER TABLE ONLY public.teams + ADD CONSTRAINT teams_pkey PRIMARY KEY (team_id); + + +-- +-- Name: games games_opponent_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: freecodecamp +-- + +ALTER TABLE ONLY public.games + ADD CONSTRAINT games_opponent_id_fkey FOREIGN KEY (opponent_id) REFERENCES public.teams(team_id); + + +-- +-- Name: games games_winner_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: freecodecamp +-- + +ALTER TABLE ONLY public.games + ADD CONSTRAINT games_winner_id_fkey FOREIGN KEY (winner_id) REFERENCES public.teams(team_id); + + +-- +-- PostgreSQL database dump complete +-- +