Created
June 27, 2026 03:11
-
-
Save npras/70c9b372f739fe9e46757af8c9b14885 to your computer and use it in GitHub Desktop.
fcc - certification - relational database - worldcup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /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. | |
| declare -A team_to_id | |
| val_doesnt_matter=1 | |
| while IFS="," read -r year round winner opponent winner_goals opponent_goals; do | |
| [[ -z $year ]] && continue # skip empty row | |
| team_to_id[$winner]="$val_doesnt_matter" | |
| team_to_id[$opponent]="$val_doesnt_matter" | |
| done < <(tail -n +2 games.csv) | |
| ### | |
| $PSQL "truncate teams, games" > /dev/null | |
| $PSQL "alter sequence teams_team_id_seq restart;" > /dev/null | |
| $PSQL "alter sequence games_game_id_seq restart;" > /dev/null | |
| # bulk insert to teams | |
| insert_teams_val="" | |
| for team in "${!team_to_id[@]}"; do | |
| insert_teams_val="${insert_teams_val}, ('${team}')" | |
| done | |
| insert_teams_val=$(echo $insert_teams_val | sed -r 's/^, //') | |
| insert_teams_sql="insert into teams (name) values ${insert_teams_val};" | |
| $PSQL "$insert_teams_sql" > /dev/null | |
| # slurp all team ids into $team_to_id | |
| teams=$($PSQL "select * from teams;") | |
| while IFS="|" read -r team_id team_name; do | |
| team_to_id["${team_name}"]="${team_id}" | |
| done <<< "$(echo -e "$teams")" | |
| # bulk insert to games | |
| insert_games_val="" | |
| while IFS="," read -r year round winner opponent winner_goals opponent_goals; do | |
| [[ -z $year ]] && continue # skip empty row | |
| winner_id="${team_to_id[$winner]}" | |
| opponent_id="${team_to_id[$opponent]}" | |
| insert_games_val="${insert_games_val}, ($year, '$round', $winner_id, $opponent_id, $winner_goals, $opponent_goals)" | |
| done < <(tail -n +2 games.csv) | |
| insert_games_val=$(echo $insert_games_val | sed -r 's/^, //') | |
| insert_games_sql="insert into games (year, round, winner_id, opponent_id, winner_goals, opponent_goals) values ${insert_games_val};" | |
| $PSQL "$insert_games_sql" > /dev/null |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /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 + 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 greatest(MAX(winner_goals), MAX(opponent_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 teams inner join games 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 "$($PSQL "select distinct name from teams inner join games on team_id in (winner_id, opponent_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 "$($PSQL "select distinct name from teams inner join games on team_id = winner_id order by name;")" | |
| echo -e "\nYear and team name of all the champions:" | |
| echo "$($PSQL "select year, name from teams inner join games on team_id = winner_id where round = 'Final' order by year;")" | |
| echo -e "\nList of teams that start with 'Co':" | |
| echo "$($PSQL "select name from teams where name ilike 'Co%' order by name;")" | |
| echo "" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- | |
| -- PostgreSQL database dump | |
| -- | |
| -- Dumped from database version 12.22 (Ubuntu 12.22-0ubuntu0.20.04.4) | |
| -- Dumped by pg_dump version 12.22 (Ubuntu 12.22-0ubuntu0.20.04.4) | |
| 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(30) 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(50) 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 | |
| -- | |
| -- | |
| -- Data for Name: teams; Type: TABLE DATA; Schema: public; Owner: freecodecamp | |
| -- | |
| -- | |
| -- Name: games_game_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp | |
| -- | |
| SELECT pg_catalog.setval('public.games_game_id_seq', 1, false); | |
| -- | |
| -- Name: teams_team_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp | |
| -- | |
| SELECT pg_catalog.setval('public.teams_team_id_seq', 1, false); | |
| -- | |
| -- 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 | |
| -- | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment