Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Andrew and Musa_sql queries_solution #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions create_schema.sql
Original file line number Diff line number Diff line change
@@ -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)
);


64 changes: 64 additions & 0 deletions seed_data.sql
Original file line number Diff line number Diff line change
@@ -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));
62 changes: 62 additions & 0 deletions statements.sql
Original file line number Diff line number Diff line change
@@ -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;