diff --git a/create_schema.sql b/create_schema.sql new file mode 100644 index 0000000..c679fd4 --- /dev/null +++ b/create_schema.sql @@ -0,0 +1,35 @@ +-- Schema +DROP TABLE IF EXISTS students; +CREATE TABLE students ( + id serial PRIMARY KEY, + first_name varchar(255) NOT NULL, + last_name varchar(255) NOT NULL, + birthdate date NOT NULL, + address_id integer +); + +DROP TABLE IF EXISTS addresses; +CREATE TABLE addresses ( + id serial PRIMARY KEY, + line_1 varchar(255) NOT NULL, + city varchar(64) NOT NULL, + state varchar(64) NOT NULL, + zipcode varchar(64) NOT NULL +); + +DROP TABLE IF EXISTS classes; +CREATE TABLE classes ( + id serial PRIMARY KEY, + name varchar(255) NOT NULL, + credits varchar(64) NOT NULL +); + +DROP TABLE IF EXISTS enrollments; +CREATE TABLE enrollments ( + id serial PRIMARY KEY, + student_id INT NOT NULL, + class_id INT NOT NULL, + grade varchar(64) +); + + diff --git a/seed_data.sql b/seed_data.sql new file mode 100644 index 0000000..4fdbc8c --- /dev/null +++ b/seed_data.sql @@ -0,0 +1,64 @@ +-- Note: We're inserting records with an id value set because we want to use +-- particular ids as foreign keys. I.e. setting the address_id for a student +-- so it references a specific address record. If we didn't specify a value +-- for the id columns, the database would choose an integer for us. + +INSERT INTO addresses (id, line_1, city, state, zipcode) VALUES (1, '6232 Guiseppe Courts', 'Jamartown', 'Maryland', '49028'); +INSERT INTO addresses (id, line_1, city, state, zipcode) VALUES (2, '704 Cecil Mountain', 'West Jon', 'South Dakota', '91578'); +INSERT INTO addresses (id, line_1, city, state, zipcode) VALUES (3, '41613 Huel Ranch', 'Loycefort', 'Florida', '12109'); +INSERT INTO addresses (id, line_1, city, state, zipcode) VALUES (4, '1397 Braden Shoals', 'New Karine', 'New York', '03913'); + + +INSERT INTO students (id, first_name, last_name, birthdate, address_id) VALUES (1, 'Tianna', 'Lowe', '1985-02-17', 1); +INSERT INTO students (id, first_name, last_name, birthdate, address_id) VALUES (2, 'Elda', 'Sipes', '1989-08-03', 2); +INSERT INTO students (id, first_name, last_name, birthdate, address_id) VALUES (3, 'Jed', 'Kunde', '1987-01-22', 3); +INSERT INTO students (id, first_name, last_name, birthdate, address_id) VALUES (4, 'Leopold', 'Towne', '1984-10-07', NULL); +INSERT INTO students (id, first_name, last_name, birthdate, address_id) VALUES (5, 'Andre', 'Rohan', '1989-09-01', 4); + +INSERT INTO classes (id, name, credits) VALUES (1, 'CS 101', 4); +INSERT INTO classes (id, name, credits) VALUES (2, 'HIST 107', 3); +INSERT INTO classes (id, name, credits) VALUES (3, 'SPAN 210', 3); +INSERT INTO classes (id, name, credits) VALUES (4, 'PHYS 218', 4); +INSERT INTO classes (id, name, credits) VALUES (5, 'ART 118', 2); + + +-- Tianna took HIST 107 and got a 'B' +INSERT INTO enrollments (id, student_id, class_id, grade) VALUES (1, 1, 2, 'B'); +-- Tiana is taking SPAN 210 +INSERT INTO enrollments (id, student_id, class_id, grade) VALUES (2, 1, 3, NULL); +-- Elda took ART 118 and got an 'A' +INSERT INTO enrollments (id, student_id, class_id, grade) VALUES (3, 2, 5, 'A'); +-- Elda is taking SPAN 210 +INSERT INTO enrollments (id, student_id, class_id, grade) VALUES (4, 2, 3, NULL); +-- Elda is taking CS 101 +INSERT INTO enrollments (id, student_id, class_id, grade) VALUES (5, 2, 1, NULL); +-- Jed is taking CS 101 +INSERT INTO enrollments (id, student_id, class_id, grade) VALUES (6, 3, 1, NULL); +-- Jed is taking HIST 107 +INSERT INTO enrollments (id, student_id, class_id, grade) VALUES (7, 3, 2, NULL); +-- Leopold is taking HIST 107 +INSERT INTO enrollments (id, student_id, class_id, grade) VALUES (8, 4, 2, NULL); +-- Leopold is taking CS 101 +INSERT INTO enrollments (id, student_id, class_id, grade) VALUES (9, 4, 1, NULL); +-- Leopold is taking SPAN 210 +INSERT INTO enrollments (id, student_id, class_id, grade) VALUES (10, 4, 3, NULL); +-- Leopold took PHYS 218 and got a 'C' +INSERT INTO enrollments (id, student_id, class_id, grade) VALUES (11, 4, 4, 'C'); +-- Leopold took CS 101 and got an 'A' +INSERT INTO enrollments (id, student_id, class_id, grade) VALUES (12, 4, 1, 'A'); +-- Andre took CS 101 and got an 'B' +INSERT INTO enrollments (id, student_id, class_id, grade) VALUES (13, 5, 1, 'B'); +-- Andre took HIST 107 and got an 'B' +INSERT INTO enrollments (id, student_id, class_id, grade) VALUES (14, 5, 2, 'B'); +-- Andre is taking SPAN 210 +INSERT INTO enrollments (id, student_id, class_id, grade) VALUES (15, 5, 3, NULL); +-- Andre is taking ART 118 +INSERT INTO enrollments (id, student_id, class_id, grade) VALUES (16, 5, 5, NULL); + +-- Because we've inserted rows with hardcoded values for the primary key, id +-- the sequence used to generate id values for new rows is stuck at "1". So +-- we manually reset the sequences to the max value of id for each table. +SELECT setval('addresses_id_seq', (SELECT MAX(id) FROM addresses)); +SELECT setval('students_id_seq', (SELECT MAX(id) FROM students)); +SELECT setval('classes_id_seq', (SELECT MAX(id) FROM classes)); +SELECT setval('enrollments_id_seq', (SELECT MAX(id) FROM enrollments)); diff --git a/statements.sql b/statements.sql new file mode 100644 index 0000000..d48cb36 --- /dev/null +++ b/statements.sql @@ -0,0 +1,62 @@ +select * from classes; + +select name, credits from classes +where credits > '3'; + +select * from classes +where mod(credits::int,2) = 0; + + +select first_name, name from enrollments +join students on enrollments.student_id = students.id +join classes on enrollments.class_id = classes.id +where first_name = 'Tianna' +and grade is null; + +select * from students +where birthdate < '1986-01-01' +and (first_name ilike '%t%' or last_name ilike '%t%'); + +select avg(age(birthdate,now())) from students; + +select * from addresses +where city like '% %'; + +select first_name, last_name, line_1, city,state,zipcode +from addresses +join students on students.address_id=addresses.id +where city like '% %'; + +select avg(credits::int) from classes; + +select * from enrollments +join students on enrollments.student_id=students.id +where grade = 'A'; + +select first_name, sum(credits::int) from enrollments +join students on enrollments.student_id=students.id +join classes on enrollments.class_id = classes.id +group by first_name; + + +select first_name, sum(credits::int) from enrollments +join students on enrollments.student_id=students.id +join classes on enrollments.class_id = classes.id +where grade is not null +group by first_name; + +select enrollments.*, name from enrollments +join classes on enrollments.class_id = classes.id; + +select * from students +where birthdate>='1982-01-01' +and birthdate<='1985-12-31'; + +insert into enrollments(student_id,class_id,grade) +values('5','4','A'); + +select * from enrollments +join classes on enrollments.class_id = classes.id +join students on enrollments.student_id=students.id; + +