Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DROP TABLE IF EXISTS teachers, сontests, participants;
- CREATE TABLE teachers (
- teacher_id serial PRIMARY KEY,
- fio text
- );
- CREATE TABLE сontests (
- сontest_id serial PRIMARY KEY,
- start_date DATE,
- name text
- );
- CREATE TABLE participants (
- participant_id serial,
- fio text,
- contest_id INTEGER REFERENCES сontests(сontest_id),
- teacher_id INTEGER REFERENCES teachers(teacher_id),
- RESULT INTEGER
- );
- INSERT INTO teachers(fio) VALUES
- ('Иванова И.'),
- ('Петорва П.'),
- ('Сидорова С.');
- INSERT INTO сontests(start_date, name) VALUES
- (to_date('2020', 'YYYY'), 'New Vasuki conference'),
- (to_date('2021', 'YYYY'), 'New Vasuki online-conference');
- INSERT INTO participants(fio, contest_id, teacher_id, RESULT) VALUES
- ('Карцев', 2, 1, 1),
- ('Бендер', 1, 1, 1),
- ('Угольников', 2 , 2, 8),
- ('Хазанов', 2, 2, 4),
- ('Арлазоров', 1, 3, 4),
- ('Задорнов', 1, 3, 5);
- SELECT teachers.teacher_id FROM teachers
- JOIN participants USING(teacher_id)
- WHERE participants.RESULT NOT BETWEEN 1 AND 3
- GROUP BY teachers.teacher_id
- HAVING COUNT(participants.teacher_id) = ALL(SELECT COUNT(*) FROM participants GROUP BY participants.teacher_id);
- SELECT teachers.teacher_id FROM teachers JOIN
- participants USING(teacher_id) JOIN (
- SELECT teacher_id, COUNT(*) AS bad_count FROM participants
- WHERE RESULT NOT BETWEEN 1 AND 3
- GROUP BY teacher_id
- ) AS tt USING(teacher_id)
- GROUP BY teachers.teacher_id, bad_count
- HAVING COUNT(participants.teacher_id) = bad_count;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement