Assignment
Assignment
3.student_batch_maps table: this table is a mapping of the student and his batch.
deactivated_at is the time when a student is made inactive in a batch.
CREATE SEQUENCE student_batch_maps_id_seq;
CREATE TABLE student_batch_maps (
id INTEGER PRIMARY KEY NOT NULL DEFAULT NEXTVAL('student_batch_maps_id_seq'),
user_id INTEGER NOT NULL REFERENCES users(id),
batch_id INTEGER NOT NULL REFERENCES batches(id),
active BOOLEAN NOT NULL DEFAULT true,
deactivated_at TIMESTAMP NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
5.sessions table: Every day session happens where the teacher takes a session or
class of students.
CREATE SEQUENCE sessions_id_seq;
CREATE TABLE sessions (
id INTEGER PRIMARY KEY NOT NULL DEFAULT NEXTVAL('sessions_id_seq'),
conducted_by INTEGER NOT NULL REFERENCES users(id),
batch_id INTEGER NOT NULL REFERENCES batches(id),
start_time TIMESTAMP NOT NULL,
end_time TIMESTAMP NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
);
6.attendances table: After session or class happens between teacher and student,
attendance is given by student. students provide ratings to the teacher.
CREATE TABLE attendances (
student_id INTEGER NOT NULL REFERENCES users(id),
session_id INTEGER NOT NULL REFERENCES sessions(id),
rating DOUBLE PRECISION NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (student_id, session_id)
);
7.tests table: Test is created by instructor. total_mark is the maximum marks for
the test.
CREATE SEQUENCE tests_id_seq;
CREATE TABLE tests (
id INTEGER PRIMARY KEY NOT NULL DEFAULT NEXTVAL('tests_id_seq'),
batch_id INTEGER REFERENCES batches(id),
created_by INTEGER REFERENCES users(id),
total_mark INTEGER NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
);
8.test_scores table: Marks scored by students are added in the test_scores table.
CREATE TABLE test_scores (
test_id INTEGER REFERENCES tests(id),
user_id INTEGER REFERENCES users(id),
score INTEGER NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(test_id, user_id)
);
Using the above table schema, please write the following queries. To test your
queries, you can use some dummy data.
1.Calculate the average rating given by students to each teacher for each session
created. Also, provide the batch name for which session was conducted.
2.Find the attendance percentage for each session for each batch. Also mention the
batch name and users name who has conduct that session
3.What is the average marks scored by each student in all the tests the student had
appeared?
4.A student is passed when he scores 40 percent of total marks in a test. Find out
how many students passed in each test. Also mention the batch name for that test.
Note - Data is not provided for these tables, you can insert some dummy data if
required.
6. What is the average percentage of marks scored by each student in all the tests
the student had appeared?
7. A student is passed when he scores 40 percent of total marks in a test. Find out
how many percentage of students have passed in each test. Also mention the batch
name for that test.