diff --git a/README.md b/README.md new file mode 100644 index 0000000..742515f --- /dev/null +++ b/README.md @@ -0,0 +1,77 @@ +## Banco de dados de Alunos, Cursos e Disciplinas + +##### Modelagem de um banco de dados relacional de estudantes de uma universidade, assim como a inserção de dados de dois arquivos CSV (um para os [cursos](courses.csv) e outro para os [alunos](students.csv)) utilizando [script BASH shell](insert_data.sh) , e também a consulta de informações através de queries, em [outro script BASH shell](students_info.sh). + +Utilizando o PostgreSQL construi esse banco de dados seguindo o seguinte modelo: + +![modelo](images/bd_model.png) + +Para a inserção dos dados, desenvolvi um script shell, que lê arquivos CSV enquanto salva em variáveis e depois insere no banco de dados em loop: +``` +# Exemplo da inserção dos cursos(o método para as disciplinas e alunos é o mesmo): + +PSQL="psql -X --username=freecodecamp --dbname=students --no-align --tuples-only -c" + +cat courses.csv | while IFS="," read MAJOR COURSE +do + if [[ $MAJOR != "major" ]] + then + # get major_id + MAJOR_ID=$($PSQL "SELECT major_id FROM majors WHERE major='$MAJOR'") + + # if not found + if [[ -z $MAJOR_ID ]] + then + # insert major + INSERT_MAJOR_RESULT=$($PSQL "INSERT INTO majors(major) VALUES('$MAJOR')") + if [[ $INSERT_MAJOR_RESULT == "INSERT 0 1" ]] + then + echo Inserted into majors, $MAJOR + fi + + # get new major_id + MAJOR_ID=$($PSQL "SELECT major_id FROM majors WHERE major='$MAJOR'") + fi + +``` +Após a execução do script, as tabelas ficaram populadas assim: + + - tabelas de cursos e disciplinas: + ![cursos_e_disciplinas](images/tables_1.png) + + - tabelas de alunos: + ![alunos](images/tables_2.png) + + - tabela de relação curso_disciplina: + ![cursos_disciplina](images/tables_3.png) + + +--- + + +###### Consulta de informações utilizando queries SQL: + +Para consultar dados significativos com queries SQL, também desenvolvi um [script shell que realiza as queries](students_info.sh). O output no terminal desse script ficou assim: + +![querie1](images/query_1.png) +![querie2](images/query_2.png) +![querie3](images/query_3.png) + + +--- + +Com esse projeto eu aprendi MUITO, manipulação de arquivos com BASH, além de piping, comparações numéricas, comparações de string, execução de expressões, variáveis e muito mais... E isso só de bash scripting... + +Sobre SQL eu realmente adquiri expertise em queries, e utilizei diversas funções, JOINs, ordenação, agrupamento e bem mais. + +E o mais importante de tudo, eu me diverti pra caramba com esse projeto! + +Para aqueles que possuem um ambiente linux e quiserem ver esse projeto em sua máquina, siga os passos a seguir: + +> 1. Instale o postgreSQL em sua máquina +> +> 2. insira o comando `psql students < students.sql` que vai ciar o BD sem os dados ou `psql students < students_full.sql` que já cria o BD populado! +> +> 3. Caso tenha criado só o modelo sem os dados, execute o script que insere os dados `./insert_data.sh` +> +> 4. Agora finalmente, execute o script com as queries `./students_info.sh` diff --git a/courses.csv b/courses.csv new file mode 100644 index 0000000..c582d99 --- /dev/null +++ b/courses.csv @@ -0,0 +1,29 @@ +major,course +Database Administration,Data Structures and Algorithms +Web Development,Web Programming +Database Administration,Database Systems +Data Science,Data Structures and Algorithms +Network Engineering,Computer Networks +Database Administration,SQL +Data Science,Machine Learning +Network Engineering,Computer Systems +Computer Programming,Computer Networks +Database Administration,Web Applications +Game Design,Artificial Intelligence +Data Science,Python +Computer Programming,Object-Oriented Programming +System Administration,Computer Systems +Game Design,Calculus +Web Development,Data Structures and Algorithms +Data Science,Calculus +Web Development,Object-Oriented Programming +Game Design,Game Architecture +System Administration,Computer Networks +Game Design,Algorithms +System Administration,UNIX +System Administration,Server Administration +Computer Programming,Computer Systems +Computer Programming,Python +Network Engineering,Network Security +Web Development,Web Applications +Network Engineering,Algorithms diff --git a/images/bd_model.png b/images/bd_model.png new file mode 100644 index 0000000..d0da8d1 Binary files /dev/null and b/images/bd_model.png differ diff --git a/images/query_1.png b/images/query_1.png new file mode 100644 index 0000000..7c6a3ca Binary files /dev/null and b/images/query_1.png differ diff --git a/images/query_2.png b/images/query_2.png new file mode 100644 index 0000000..07723e5 Binary files /dev/null and b/images/query_2.png differ diff --git a/images/query_3.png b/images/query_3.png new file mode 100644 index 0000000..93dc8e9 Binary files /dev/null and b/images/query_3.png differ diff --git a/images/tables_1.png b/images/tables_1.png new file mode 100644 index 0000000..59d01f2 Binary files /dev/null and b/images/tables_1.png differ diff --git a/images/tables_2.png b/images/tables_2.png new file mode 100644 index 0000000..d1eb1d8 Binary files /dev/null and b/images/tables_2.png differ diff --git a/images/tables_3.png b/images/tables_3.png new file mode 100644 index 0000000..3557d24 Binary files /dev/null and b/images/tables_3.png differ diff --git a/insert_data.sh b/insert_data.sh new file mode 100644 index 0000000..1b62f51 --- /dev/null +++ b/insert_data.sh @@ -0,0 +1,76 @@ +#!/bin/bash + +# Script to insert data from courses.csv and students.csv into students database + +PSQL="psql -X --username=freecodecamp --dbname=students --no-align --tuples-only -c" +echo $($PSQL "TRUNCATE students, majors, courses, majors_courses") + +cat courses.csv | while IFS="," read MAJOR COURSE +do + if [[ $MAJOR != "major" ]] + then + # get major_id + MAJOR_ID=$($PSQL "SELECT major_id FROM majors WHERE major='$MAJOR'") + + # if not found + if [[ -z $MAJOR_ID ]] + then + # insert major + INSERT_MAJOR_RESULT=$($PSQL "INSERT INTO majors(major) VALUES('$MAJOR')") + if [[ $INSERT_MAJOR_RESULT == "INSERT 0 1" ]] + then + echo Inserted into majors, $MAJOR + fi + + # get new major_id + MAJOR_ID=$($PSQL "SELECT major_id FROM majors WHERE major='$MAJOR'") + fi + + # get course_id + COURSE_ID=$($PSQL "SELECT course_id FROM courses WHERE course='$COURSE'") + + # if not found + if [[ -z $COURSE_ID ]] + then + # insert course + INSERT_COURSE_RESULT=$($PSQL "INSERT INTO courses(course) VALUES('$COURSE')") + if [[ $INSERT_COURSE_RESULT == "INSERT 0 1" ]] + then + echo Inserted into courses, $COURSE + fi + + # get new course_id + COURSE_ID=$($PSQL "SELECT course_id FROM courses WHERE course='$COURSE'") + fi + + # insert into majors_courses + INSERT_MAJORS_COURSES_RESULT=$($PSQL "INSERT INTO majors_courses(major_id, course_id) VALUES($MAJOR_ID, $COURSE_ID)") + if [[ $INSERT_MAJORS_COURSES_RESULT == "INSERT 0 1" ]] + then + echo Inserted into majors_courses, $MAJOR : $COURSE + fi + fi +done + +cat students.csv | while IFS="," read FIRST LAST MAJOR GPA +do + if [[ $FIRST != "first_name" ]] + then + # get major_id + MAJOR_ID=$($PSQL "SELECT major_id FROM majors WHERE major='$MAJOR'") + + # if not found + if [[ -z $MAJOR_ID ]] + then + # set to null + MAJOR_ID=null + fi + + # insert student + INSERT_STUDENT_RESULT=$($PSQL "INSERT INTO students(first_name, last_name, major_id, gpa) VALUES('$FIRST', '$LAST', $MAJOR_ID, $GPA)") + if [[ $INSERT_STUDENT_RESULT == "INSERT 0 1" ]] + then + echo Inserted into students, $FIRST $LAST + fi + fi +done \ No newline at end of file diff --git a/students.csv b/students.csv new file mode 100644 index 0000000..8447fdb --- /dev/null +++ b/students.csv @@ -0,0 +1,32 @@ +first_name,last_name,major,gpa +Rhea,Kellems,Database Administration,2.5 +Emma,Gilbert,null,null +Kimberly,Whitley,Web Development,3.8 +Jimmy,Felipe,Database Administration,3.7 +Kyle,Stimson,null,2.8 +Casares,Hijo,Game Design,4.0 +Noe,Savage,null,3.6 +Sterling,Boss,Game Design,3.9 +Brian,Davis,null,2.3 +Kaija,Uronen,Game Design,3.7 +Faye,Conn,Game Design,2.1 +Efren,Reilly,Web Development,3.9 +Danh,Nhung,null,2.4 +Maxine,Hagenes,Database Administration,2.9 +Larry,Saunders,Data Science,2.2 +Karl,Kuhar,Web Development,null +Lieke,Hazenveld,Game Design,3.5 +Obie,Hilpert,Web Development,null +Peter,Booysen,null,2.9 +Nathan,Turner,Database Administration,3.3 +Gerald,Osiki,Data Science,2.2 +Vanya,Hassanah,Game Design,4.0 +Roxelana,Florescu,Database Administration,3.2 +Helene,Parker,Data Science,3.4 +Mariana,Russel,Web Development,1.8 +Ajit,Dhungel,null,3.0 +Mehdi,Vandenberghe,Database Administration,1.9 +Dejon,Howell,Web Development,4.0 +Aliya,Gulgowski,System Administration,2.6 +Ana,Tupajic,Data Science,3.1 +Hugo,Duran,null,3.8 diff --git a/students.sql b/students.sql new file mode 100644 index 0000000..3af453f --- /dev/null +++ b/students.sql @@ -0,0 +1,298 @@ +-- +-- 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 students; +-- +-- Name: students; Type: DATABASE; Schema: -; Owner: freecodecamp +-- + +CREATE DATABASE students WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'C.UTF-8' LC_CTYPE = 'C.UTF-8'; + + +ALTER DATABASE students OWNER TO freecodecamp; + +\connect students + +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: courses; Type: TABLE; Schema: public; Owner: freecodecamp +-- + +CREATE TABLE public.courses ( + course_id integer NOT NULL, + course character varying(100) NOT NULL +); + + +ALTER TABLE public.courses OWNER TO freecodecamp; + +-- +-- Name: courses_course_id_seq; Type: SEQUENCE; Schema: public; Owner: freecodecamp +-- + +CREATE SEQUENCE public.courses_course_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE public.courses_course_id_seq OWNER TO freecodecamp; + +-- +-- Name: courses_course_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: freecodecamp +-- + +ALTER SEQUENCE public.courses_course_id_seq OWNED BY public.courses.course_id; + + +-- +-- Name: majors; Type: TABLE; Schema: public; Owner: freecodecamp +-- + +CREATE TABLE public.majors ( + major_id integer NOT NULL, + major character varying(50) NOT NULL +); + + +ALTER TABLE public.majors OWNER TO freecodecamp; + +-- +-- Name: majors_courses; Type: TABLE; Schema: public; Owner: freecodecamp +-- + +CREATE TABLE public.majors_courses ( + major_id integer NOT NULL, + course_id integer NOT NULL +); + + +ALTER TABLE public.majors_courses OWNER TO freecodecamp; + +-- +-- Name: majors_major_id_seq; Type: SEQUENCE; Schema: public; Owner: freecodecamp +-- + +CREATE SEQUENCE public.majors_major_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE public.majors_major_id_seq OWNER TO freecodecamp; + +-- +-- Name: majors_major_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: freecodecamp +-- + +ALTER SEQUENCE public.majors_major_id_seq OWNED BY public.majors.major_id; + + +-- +-- Name: students; Type: TABLE; Schema: public; Owner: freecodecamp +-- + +CREATE TABLE public.students ( + student_id integer NOT NULL, + first_name character varying(50) NOT NULL, + last_name character varying(50) NOT NULL, + major_id integer, + gpa numeric(2,1) +); + + +ALTER TABLE public.students OWNER TO freecodecamp; + +-- +-- Name: students_student_id_seq; Type: SEQUENCE; Schema: public; Owner: freecodecamp +-- + +CREATE SEQUENCE public.students_student_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE public.students_student_id_seq OWNER TO freecodecamp; + +-- +-- Name: students_student_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: freecodecamp +-- + +ALTER SEQUENCE public.students_student_id_seq OWNED BY public.students.student_id; + + +-- +-- Name: courses course_id; Type: DEFAULT; Schema: public; Owner: freecodecamp +-- + +ALTER TABLE ONLY public.courses ALTER COLUMN course_id SET DEFAULT nextval('public.courses_course_id_seq'::regclass); + + +-- +-- Name: majors major_id; Type: DEFAULT; Schema: public; Owner: freecodecamp +-- + +ALTER TABLE ONLY public.majors ALTER COLUMN major_id SET DEFAULT nextval('public.majors_major_id_seq'::regclass); + + +-- +-- Name: students student_id; Type: DEFAULT; Schema: public; Owner: freecodecamp +-- + +ALTER TABLE ONLY public.students ALTER COLUMN student_id SET DEFAULT nextval('public.students_student_id_seq'::regclass); + + +-- +-- Data for Name: courses; Type: TABLE DATA; Schema: public; Owner: freecodecamp +-- + +INSERT INTO public.courses VALUES (17, 'Data Structures and Algorithms'); +INSERT INTO public.courses VALUES (18, 'Web Programming'); +INSERT INTO public.courses VALUES (19, 'Database Systems'); + + +-- +-- Data for Name: majors; Type: TABLE DATA; Schema: public; Owner: freecodecamp +-- + +INSERT INTO public.majors VALUES (30, 'Database Administration'); +INSERT INTO public.majors VALUES (31, 'Web Development'); +INSERT INTO public.majors VALUES (32, 'Data Science'); + + +-- +-- Data for Name: majors_courses; Type: TABLE DATA; Schema: public; Owner: freecodecamp +-- + +INSERT INTO public.majors_courses VALUES (30, 17); +INSERT INTO public.majors_courses VALUES (31, 18); +INSERT INTO public.majors_courses VALUES (30, 19); +INSERT INTO public.majors_courses VALUES (32, 17); + + +-- +-- Data for Name: students; Type: TABLE DATA; Schema: public; Owner: freecodecamp +-- + + + +-- +-- Name: courses_course_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp +-- + +SELECT pg_catalog.setval('public.courses_course_id_seq', 19, true); + + +-- +-- Name: majors_major_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp +-- + +SELECT pg_catalog.setval('public.majors_major_id_seq', 32, true); + + +-- +-- Name: students_student_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp +-- + +SELECT pg_catalog.setval('public.students_student_id_seq', 1, true); + + +-- +-- Name: courses courses_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp +-- + +ALTER TABLE ONLY public.courses + ADD CONSTRAINT courses_pkey PRIMARY KEY (course_id); + + +-- +-- Name: majors_courses majors_courses_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp +-- + +ALTER TABLE ONLY public.majors_courses + ADD CONSTRAINT majors_courses_pkey PRIMARY KEY (major_id, course_id); + + +-- +-- Name: majors majors_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp +-- + +ALTER TABLE ONLY public.majors + ADD CONSTRAINT majors_pkey PRIMARY KEY (major_id); + + +-- +-- Name: students students_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp +-- + +ALTER TABLE ONLY public.students + ADD CONSTRAINT students_pkey PRIMARY KEY (student_id); + + +-- +-- Name: majors_courses majors_courses_course_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: freecodecamp +-- + +ALTER TABLE ONLY public.majors_courses + ADD CONSTRAINT majors_courses_course_id_fkey FOREIGN KEY (course_id) REFERENCES public.courses(course_id); + + +-- +-- Name: majors_courses majors_courses_major_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: freecodecamp +-- + +ALTER TABLE ONLY public.majors_courses + ADD CONSTRAINT majors_courses_major_id_fkey FOREIGN KEY (major_id) REFERENCES public.majors(major_id); + + +-- +-- Name: students students_major_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: freecodecamp +-- + +ALTER TABLE ONLY public.students + ADD CONSTRAINT students_major_id_fkey FOREIGN KEY (major_id) REFERENCES public.majors(major_id); + + +-- +-- PostgreSQL database dump complete +-- + diff --git a/students_full.sql b/students_full.sql new file mode 100644 index 0000000..6622dc1 --- /dev/null +++ b/students_full.sql @@ -0,0 +1,374 @@ +SELECT pg_terminate_backend(pg_stat_activity.pid) +FROM pg_stat_activity +WHERE usename = 'freecodecamp'; + +-- +-- PostgreSQL database dump +-- + +-- Dumped from database version 12.6 (Debian 12.6-1.pgdg90+1) +-- Dumped by pg_dump version 12.6 (Debian 12.6-1.pgdg90+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 IF EXISTS students; +-- +-- Name: students; Type: DATABASE; Schema: -; Owner: freecodecamp +-- + +CREATE DATABASE students WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'C.UTF-8' LC_CTYPE = 'C.UTF-8'; + + +ALTER DATABASE students OWNER TO freecodecamp; + +\connect students + +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: courses; Type: TABLE; Schema: public; Owner: freecodecamp +-- + +CREATE TABLE public.courses ( + course_id integer NOT NULL, + course character varying(100) NOT NULL +); + + +ALTER TABLE public.courses OWNER TO freecodecamp; + +-- +-- Name: courses_course_id_seq; Type: SEQUENCE; Schema: public; Owner: freecodecamp +-- + +CREATE SEQUENCE public.courses_course_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE public.courses_course_id_seq OWNER TO freecodecamp; + +-- +-- Name: courses_course_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: freecodecamp +-- + +ALTER SEQUENCE public.courses_course_id_seq OWNED BY public.courses.course_id; + + +-- +-- Name: majors; Type: TABLE; Schema: public; Owner: freecodecamp +-- + +CREATE TABLE public.majors ( + major_id integer NOT NULL, + major character varying(50) NOT NULL +); + + +ALTER TABLE public.majors OWNER TO freecodecamp; + +-- +-- Name: majors_courses; Type: TABLE; Schema: public; Owner: freecodecamp +-- + +CREATE TABLE public.majors_courses ( + major_id integer NOT NULL, + course_id integer NOT NULL +); + + +ALTER TABLE public.majors_courses OWNER TO freecodecamp; + +-- +-- Name: majors_major_id_seq; Type: SEQUENCE; Schema: public; Owner: freecodecamp +-- + +CREATE SEQUENCE public.majors_major_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE public.majors_major_id_seq OWNER TO freecodecamp; + +-- +-- Name: majors_major_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: freecodecamp +-- + +ALTER SEQUENCE public.majors_major_id_seq OWNED BY public.majors.major_id; + + +-- +-- Name: students; Type: TABLE; Schema: public; Owner: freecodecamp +-- + +CREATE TABLE public.students ( + student_id integer NOT NULL, + first_name character varying(50) NOT NULL, + last_name character varying(50) NOT NULL, + major_id integer, + gpa numeric(2,1) +); + + +ALTER TABLE public.students OWNER TO freecodecamp; + +-- +-- Name: students_student_id_seq; Type: SEQUENCE; Schema: public; Owner: freecodecamp +-- + +CREATE SEQUENCE public.students_student_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER TABLE public.students_student_id_seq OWNER TO freecodecamp; + +-- +-- Name: students_student_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: freecodecamp +-- + +ALTER SEQUENCE public.students_student_id_seq OWNED BY public.students.student_id; + + +-- +-- Name: courses course_id; Type: DEFAULT; Schema: public; Owner: freecodecamp +-- + +ALTER TABLE ONLY public.courses ALTER COLUMN course_id SET DEFAULT nextval('public.courses_course_id_seq'::regclass); + + +-- +-- Name: majors major_id; Type: DEFAULT; Schema: public; Owner: freecodecamp +-- + +ALTER TABLE ONLY public.majors ALTER COLUMN major_id SET DEFAULT nextval('public.majors_major_id_seq'::regclass); + + +-- +-- Name: students student_id; Type: DEFAULT; Schema: public; Owner: freecodecamp +-- + +ALTER TABLE ONLY public.students ALTER COLUMN student_id SET DEFAULT nextval('public.students_student_id_seq'::regclass); + + +-- +-- Data for Name: courses; Type: TABLE DATA; Schema: public; Owner: freecodecamp +-- + +INSERT INTO public.courses VALUES (23, 'Data Structures and Algorithms'); +INSERT INTO public.courses VALUES (24, 'Web Programming'); +INSERT INTO public.courses VALUES (25, 'Database Systems'); +INSERT INTO public.courses VALUES (26, 'Computer Networks'); +INSERT INTO public.courses VALUES (27, 'SQL'); +INSERT INTO public.courses VALUES (28, 'Machine Learning'); +INSERT INTO public.courses VALUES (29, 'Computer Systems'); +INSERT INTO public.courses VALUES (30, 'Web Applications'); +INSERT INTO public.courses VALUES (31, 'Artificial Intelligence'); +INSERT INTO public.courses VALUES (32, 'Python'); +INSERT INTO public.courses VALUES (33, 'Object-Oriented Programming'); +INSERT INTO public.courses VALUES (34, 'Calculus'); +INSERT INTO public.courses VALUES (35, 'Game Architecture'); +INSERT INTO public.courses VALUES (36, 'Algorithms'); +INSERT INTO public.courses VALUES (37, 'UNIX'); +INSERT INTO public.courses VALUES (38, 'Server Administration'); +INSERT INTO public.courses VALUES (39, 'Network Security'); + + +-- +-- Data for Name: majors; Type: TABLE DATA; Schema: public; Owner: freecodecamp +-- + +INSERT INTO public.majors VALUES (36, 'Database Administration'); +INSERT INTO public.majors VALUES (37, 'Web Development'); +INSERT INTO public.majors VALUES (38, 'Data Science'); +INSERT INTO public.majors VALUES (39, 'Network Engineering'); +INSERT INTO public.majors VALUES (40, 'Computer Programming'); +INSERT INTO public.majors VALUES (41, 'Game Design'); +INSERT INTO public.majors VALUES (42, 'System Administration'); + + +-- +-- Data for Name: majors_courses; Type: TABLE DATA; Schema: public; Owner: freecodecamp +-- + +INSERT INTO public.majors_courses VALUES (36, 23); +INSERT INTO public.majors_courses VALUES (37, 24); +INSERT INTO public.majors_courses VALUES (36, 25); +INSERT INTO public.majors_courses VALUES (38, 23); +INSERT INTO public.majors_courses VALUES (39, 26); +INSERT INTO public.majors_courses VALUES (36, 27); +INSERT INTO public.majors_courses VALUES (38, 28); +INSERT INTO public.majors_courses VALUES (39, 29); +INSERT INTO public.majors_courses VALUES (40, 26); +INSERT INTO public.majors_courses VALUES (36, 30); +INSERT INTO public.majors_courses VALUES (41, 31); +INSERT INTO public.majors_courses VALUES (38, 32); +INSERT INTO public.majors_courses VALUES (40, 33); +INSERT INTO public.majors_courses VALUES (42, 29); +INSERT INTO public.majors_courses VALUES (41, 34); +INSERT INTO public.majors_courses VALUES (37, 23); +INSERT INTO public.majors_courses VALUES (38, 34); +INSERT INTO public.majors_courses VALUES (37, 33); +INSERT INTO public.majors_courses VALUES (41, 35); +INSERT INTO public.majors_courses VALUES (42, 26); +INSERT INTO public.majors_courses VALUES (41, 36); +INSERT INTO public.majors_courses VALUES (42, 37); +INSERT INTO public.majors_courses VALUES (42, 38); +INSERT INTO public.majors_courses VALUES (40, 29); +INSERT INTO public.majors_courses VALUES (40, 32); +INSERT INTO public.majors_courses VALUES (39, 39); +INSERT INTO public.majors_courses VALUES (37, 30); +INSERT INTO public.majors_courses VALUES (39, 36); + + +-- +-- Data for Name: students; Type: TABLE DATA; Schema: public; Owner: freecodecamp +-- + +INSERT INTO public.students VALUES (6, 'Rhea', 'Kellems', 36, 2.5); +INSERT INTO public.students VALUES (7, 'Emma', 'Gilbert', NULL, NULL); +INSERT INTO public.students VALUES (8, 'Kimberly', 'Whitley', 37, 3.8); +INSERT INTO public.students VALUES (9, 'Jimmy', 'Felipe', 36, 3.7); +INSERT INTO public.students VALUES (10, 'Kyle', 'Stimson', NULL, 2.8); +INSERT INTO public.students VALUES (11, 'Casares', 'Hijo', 41, 4.0); +INSERT INTO public.students VALUES (12, 'Noe', 'Savage', NULL, 3.6); +INSERT INTO public.students VALUES (13, 'Sterling', 'Boss', 41, 3.9); +INSERT INTO public.students VALUES (14, 'Brian', 'Davis', NULL, 2.3); +INSERT INTO public.students VALUES (15, 'Kaija', 'Uronen', 41, 3.7); +INSERT INTO public.students VALUES (16, 'Faye', 'Conn', 41, 2.1); +INSERT INTO public.students VALUES (17, 'Efren', 'Reilly', 37, 3.9); +INSERT INTO public.students VALUES (18, 'Danh', 'Nhung', NULL, 2.4); +INSERT INTO public.students VALUES (19, 'Maxine', 'Hagenes', 36, 2.9); +INSERT INTO public.students VALUES (20, 'Larry', 'Saunders', 38, 2.2); +INSERT INTO public.students VALUES (21, 'Karl', 'Kuhar', 37, NULL); +INSERT INTO public.students VALUES (22, 'Lieke', 'Hazenveld', 41, 3.5); +INSERT INTO public.students VALUES (23, 'Obie', 'Hilpert', 37, NULL); +INSERT INTO public.students VALUES (24, 'Peter', 'Booysen', NULL, 2.9); +INSERT INTO public.students VALUES (25, 'Nathan', 'Turner', 36, 3.3); +INSERT INTO public.students VALUES (26, 'Gerald', 'Osiki', 38, 2.2); +INSERT INTO public.students VALUES (27, 'Vanya', 'Hassanah', 41, 4.0); +INSERT INTO public.students VALUES (28, 'Roxelana', 'Florescu', 36, 3.2); +INSERT INTO public.students VALUES (29, 'Helene', 'Parker', 38, 3.4); +INSERT INTO public.students VALUES (30, 'Mariana', 'Russel', 37, 1.8); +INSERT INTO public.students VALUES (31, 'Ajit', 'Dhungel', NULL, 3.0); +INSERT INTO public.students VALUES (32, 'Mehdi', 'Vandenberghe', 36, 1.9); +INSERT INTO public.students VALUES (33, 'Dejon', 'Howell', 37, 4.0); +INSERT INTO public.students VALUES (34, 'Aliya', 'Gulgowski', 42, 2.6); +INSERT INTO public.students VALUES (35, 'Ana', 'Tupajic', 38, 3.1); +INSERT INTO public.students VALUES (36, 'Hugo', 'Duran', NULL, 3.8); + + +-- +-- Name: courses_course_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp +-- + +SELECT pg_catalog.setval('public.courses_course_id_seq', 39, true); + + +-- +-- Name: majors_major_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp +-- + +SELECT pg_catalog.setval('public.majors_major_id_seq', 42, true); + + +-- +-- Name: students_student_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp +-- + +SELECT pg_catalog.setval('public.students_student_id_seq', 36, true); + + +-- +-- Name: courses courses_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp +-- + +ALTER TABLE ONLY public.courses + ADD CONSTRAINT courses_pkey PRIMARY KEY (course_id); + + +-- +-- Name: majors_courses majors_courses_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp +-- + +ALTER TABLE ONLY public.majors_courses + ADD CONSTRAINT majors_courses_pkey PRIMARY KEY (major_id, course_id); + + +-- +-- Name: majors majors_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp +-- + +ALTER TABLE ONLY public.majors + ADD CONSTRAINT majors_pkey PRIMARY KEY (major_id); + + +-- +-- Name: students students_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp +-- + +ALTER TABLE ONLY public.students + ADD CONSTRAINT students_pkey PRIMARY KEY (student_id); + + +-- +-- Name: majors_courses majors_courses_course_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: freecodecamp +-- + +ALTER TABLE ONLY public.majors_courses + ADD CONSTRAINT majors_courses_course_id_fkey FOREIGN KEY (course_id) REFERENCES public.courses(course_id); + + +-- +-- Name: majors_courses majors_courses_major_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: freecodecamp +-- + +ALTER TABLE ONLY public.majors_courses + ADD CONSTRAINT majors_courses_major_id_fkey FOREIGN KEY (major_id) REFERENCES public.majors(major_id); + + +-- +-- Name: students students_major_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: freecodecamp +-- + +ALTER TABLE ONLY public.students + ADD CONSTRAINT students_major_id_fkey FOREIGN KEY (major_id) REFERENCES public.majors(major_id); + + +-- +-- PostgreSQL database dump complete +-- \ No newline at end of file diff --git a/students_info.sh b/students_info.sh new file mode 100644 index 0000000..87c1cce --- /dev/null +++ b/students_info.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +# Info about my computer science students from students database + +echo -e "\n~~ My Computer Science Students ~~\n" + +PSQL="psql -X --username=freecodecamp --dbname=students --no-align --tuples-only -c" + +echo -e "\nFirst name, last name, and GPA of students with a 4.0 GPA:" +echo "$($PSQL "SELECT first_name, last_name, gpa FROM students WHERE gpa = 4.0")" + +echo -e "\nAll course names whose first letter is before 'D' in the alphabet:" +echo "$($PSQL "SELECT course FROM courses WHERE course < 'D'")" + +echo -e "\nFirst name, last name, and GPA of students whose last name begins with an 'R' or after and have a GPA greater than 3.8 or less than 2.0:" +echo "$($PSQL "SELECT first_name, last_name, gpa FROM students WHERE last_name >= 'R' AND (gpa > 3.8 OR gpa < 2.0)")" + +echo -e "\nLast name of students whose last name contains a case insensitive 'sa' or have an 'r' as the second to last letter:" +echo "$($PSQL "SELECT last_name FROM students WHERE last_name ILIKE '%sa%' OR last_name ILIKE '%r_'")" + +echo -e "\nFirst name, last name, and GPA of students who have not selected a major and either their first name begins with 'D' or they have a GPA greater than 3.0:" +echo "$($PSQL "SELECT first_name, last_name, gpa FROM students WHERE major_id IS NULL AND (first_name LIKE 'D%' OR gpa > 3.0)")" + +echo -e "\nCourse name of the first five courses, in reverse alphabetical order, that have an 'e' as the second letter or end with an 's':" +echo "$($PSQL "SELECT course FROM courses WHERE course LIKE '_e%' OR course LIKE '%s' ORDER BY course DESC LIMIT 5")" + +echo -e "\nAverage GPA of all students rounded to two decimal places:" +echo "$($PSQL "SELECT ROUND(AVG(gpa), 2) FROM students")" + +echo -e "\nMajor ID, total number of students in a column named 'number_of_students', and average GPA rounded to two decimal places in a column name 'average_gpa', for each major ID in the students table having a student count greater than 1:" +echo "$($PSQL "SELECT major_id, COUNT(*) AS number_of_students, ROUND(AVG(gpa), 2) AS average_gpa FROM students GROUP BY major_id HAVING COUNT(*) > 1")" + +echo -e "\nList of majors, in alphabetical order, that either no student is taking or has a student whose first name contains a case insensitive 'ma':" +echo "$($PSQL "SELECT major FROM students FULL JOIN majors ON students.major_id = majors.major_id WHERE major IS NOT NULL AND (student_id IS NULL OR first_name ILIKE '%ma%') ORDER BY major")" + +echo -e "\nList of unique courses, in reverse alphabetical order, that no student or 'Obie Hilpert' is taking:" +echo "$($PSQL "SELECT DISTINCT course FROM students FULL JOIN majors USING(major_id) FULL JOIN majors_courses USING(major_id) FULL JOIN courses USING(course_id) WHERE student_id IS NULL OR (first_name='Obie' AND last_name='Hilpert') ORDER BY course DESC")" + +echo -e "\nList of courses, in alphabetical order, with only one student enrolled:" +echo "$($PSQL "SELECT course FROM students FULL JOIN majors USING(major_id) FULL JOIN majors_courses USING(major_id) FULL JOIN courses USING(course_id) GROUP BY course HAVING COUNT(student_id)=1 ORDER BY course ")"