Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DROP DATABASE IF EXISTS school_sport_clubs;
- CREATE DATABASE school_sport_clubs;
- USE school_sport_clubs;
- CREATE TABLE school_sport_clubs.sports (
- id INT AUTO_INCREMENT PRIMARY KEY,
- name VARCHAR(255) NOT NULL
- );
- CREATE TABLE school_sport_clubs.coaches (
- id INT AUTO_INCREMENT PRIMARY KEY,
- name VARCHAR(255) NOT NULL,
- egn VARCHAR(10) NOT NULL UNIQUE
- );
- CREATE TABLE school_sport_clubs.students (
- id INT AUTO_INCREMENT PRIMARY KEY,
- name VARCHAR(255) NOT NULL,
- egn VARCHAR(10) NOT NULL UNIQUE,
- address VARCHAR(255) NOT NULL,
- phone VARCHAR(20) NULL DEFAULT NULL,
- class VARCHAR(10) NULL DEFAULT NULL
- );
- CREATE TABLE school_sport_clubs.sportGroups (
- id INT AUTO_INCREMENT PRIMARY KEY,
- location VARCHAR(255) NOT NULL,
- dayOfWeek ENUM('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'),
- hourOfTraining TIME NOT NULL,
- sport_id INT NOT NULL,
- coach_id INT NOT NULL,
- UNIQUE KEY (location , dayOfWeek , hourOfTraining),
- CONSTRAINT FOREIGN KEY (sport_id)
- REFERENCES sports (id),
- CONSTRAINT FOREIGN KEY (coach_id)
- REFERENCES coaches (id)
- );
- CREATE TABLE school_sport_clubs.student_sport (
- student_id INT NOT NULL,
- sportGroup_id INT NOT NULL,
- CONSTRAINT FOREIGN KEY (student_id)
- REFERENCES students (id),
- CONSTRAINT FOREIGN KEY (sportGroup_id)
- REFERENCES sportGroups (id),
- PRIMARY KEY (student_id , sportGroup_id)
- );
- CREATE TABLE taxesPayments (
- id INT AUTO_INCREMENT PRIMARY KEY,
- student_id INT NOT NULL,
- group_id INT NOT NULL,
- paymentAmount DOUBLE NOT NULL,
- month TINYINT,
- year YEAR,
- dateOfPayment DATETIME NOT NULL,
- CONSTRAINT FOREIGN KEY (student_id)
- REFERENCES students (id),
- CONSTRAINT FOREIGN KEY (group_id)
- REFERENCES sportgroups (id)
- );
- CREATE TABLE salaryPayments (
- id INT AUTO_INCREMENT PRIMARY KEY,
- coach_id INT NOT NULL,
- month TINYINT,
- year YEAR,
- salaryAmount DOUBLE,
- dateOfPayment DATETIME NOT NULL,
- CONSTRAINT FOREIGN KEY (coach_id)
- REFERENCES coaches (id),
- UNIQUE KEY (coach_id , `month` , `year`)
- );
- INSERT INTO sports
- VALUES (NULL, 'Football') ,
- (NULL, 'Volleyball'),
- (NULL, 'Tennis');
- INSERT INTO coaches
- VALUES (NULL, 'Ivan Todorov Petkov', '7509041245') ,
- (NULL, 'georgi Ivanov Todorov', '8010091245') ,
- (NULL, 'Ilian Todorov Georgiev', '8407106352') ,
- (NULL, 'Petar Slavkov Yordanov', '7010102045') ,
- (NULL, 'Todor Ivanov Ivanov', '8302160980') ,
- (NULL, 'Slavi Petkov Petkov', '7106041278');
- INSERT INTO students (name, egn, address, phone, class)
- VALUES ('Iliyan Ivanov', '9401150045', 'Sofia-Mladost 1', '0893452120', '10') ,
- ('Ivan Iliev Georgiev', '9510104512', 'Sofia-Liylin', '0894123456', '11') ,
- ('Elena Petrova Petrova', '9505052154', 'Sofia-Mladost 3', '0897852412', '11') ,
- ('Ivan Iliev Iliev', '9510104542', 'Sofia-Mladost 3', '0894123457', '11') ,
- ('Maria Hristova Dimova', '9510104547', 'Sofia-Mladost 4', '0894123442', '11') ,
- ('Antoaneta Ivanova Georgieva', '9411104547', 'Sofia-Krasno selo', '0874526235', '10');
- INSERT INTO sportGroups
- VALUES (NULL, 'Sofia-Mladost 1', 'Monday', '08:00:00', 1, 1 ) ,
- (NULL, 'Sofia-Mladost 1', 'Monday', '09:30:00', 1, 2 ) ,
- (NULL, 'Sofia-Liylin 7', 'Sunday', '08:00:00', 2, 1) ,
- (NULL, 'Sofia-Liylin 7', 'Sunday', '09:30:00', 2, 2) ,
- (NULL, 'Plovdiv', 'Monday', '12:00:00', '1', '1');
- INSERT INTO student_sport
- VALUES (1, 1),
- (2, 1),
- (3, 1),
- (4, 2),
- (5, 2),
- (6, 2),
- (1, 3),
- (2, 3),
- (3, 3);
- INSERT INTO school_sport_clubs.`taxespayments`
- VALUES (NULL, '1', '1', '200', '1', 2015, now()),
- (NULL, '1', '1', '200', '2', 2015, now()),
- (NULL, '1', '1', '200', '3', 2015, now()),
- (NULL, '1', '1', '200', '4', 2015, now()),
- (NULL, '1', '1', '200', '5', 2015, now()),
- (NULL, '1', '1', '200', '6', 2015, now()),
- (NULL, '1', '1', '200', '7', 2015, now()),
- (NULL, '1', '1', '200', '8', 2015, now()),
- (NULL, '1', '1', '200', '9', 2015, now()),
- (NULL, '1', '1', '200', '10', 2015, now()),
- (NULL, '1', '1', '200', '11', 2015, now()),
- (NULL, '1', '1', '200', '12', 2015, now()),
- (NULL, '2', '1', '250', '1', 2015, now()),
- (NULL, '2', '1', '250', '2', 2015, now()),
- (NULL, '2', '1', '250', '3', 2015, now()),
- (NULL, '2', '1', '250', '4', 2015, now()),
- (NULL, '2', '1', '250', '5', 2015, now()),
- (NULL, '2', '1', '250', '6', 2015, now()),
- (NULL, '2', '1', '250', '7', 2015, now()),
- (NULL, '2', '1', '250', '8', 2015, now()),
- (NULL, '2', '1', '250', '9', 2015, now()),
- (NULL, '2', '1', '250', '10', 2015, now()),
- (NULL, '2', '1', '250', '11', 2015, now()),
- (NULL, '2', '1', '250', '12', 2015, now()),
- (NULL, '3', '1', '250', '1', 2015, now()),
- (NULL, '3', '1', '250', '2', 2015, now()),
- (NULL, '3', '1', '250', '3', 2015, now()),
- (NULL, '3', '1', '250', '4', 2015, now()),
- (NULL, '3', '1', '250', '5', 2015, now()),
- (NULL, '3', '1', '250', '6', 2015, now()),
- (NULL, '3', '1', '250', '7', 2015, now()),
- (NULL, '3', '1', '250', '8', 2015, now()),
- (NULL, '3', '1', '250', '9', 2015, now()),
- (NULL, '3', '1', '250', '10', 2015, now()),
- (NULL, '3', '1', '250', '11', 2015, now()),
- (NULL, '3', '1', '250', '12', 2015, now()),
- (NULL, '1', '2', '200', '1', 2015, now()),
- (NULL, '1', '2', '200', '2', 2015, now()),
- (NULL, '1', '2', '200', '3', 2015, now()),
- (NULL, '1', '2', '200', '4', 2015, now()),
- (NULL, '1', '2', '200', '5', 2015, now()),
- (NULL, '1', '2', '200', '6', 2015, now()),
- (NULL, '1', '2', '200', '7', 2015, now()),
- (NULL, '1', '2', '200', '8', 2015, now()),
- (NULL, '1', '2', '200', '9', 2015, now()),
- (NULL, '1', '2', '200', '10', 2015, now()),
- (NULL, '1', '2', '200', '11', 2015, now()),
- (NULL, '1', '2', '200', '12', 2015, now()),
- (NULL, '4', '2', '200', '1', 2015, now()),
- (NULL, '4', '2', '200', '2', 2015, now()),
- (NULL, '4', '2', '200', '3', 2015, now()),
- (NULL, '4', '2', '200', '4', 2015, now()),
- (NULL, '4', '2', '200', '5', 2015, now()),
- (NULL, '4', '2', '200', '6', 2015, now()),
- (NULL, '4', '2', '200', '7', 2015, now()),
- (NULL, '4', '2', '200', '8', 2015, now()),
- (NULL, '4', '2', '200', '9', 2015, now()),
- (NULL, '4', '2', '200', '10', 2015, now()),
- (NULL, '4', '2', '200', '11', 2015, now()),
- (NULL, '4', '2', '200', '12', 2015, now()),
- /**2014**/
- (NULL, '1', '1', '200', '1', 2014, now()),
- (NULL, '1', '1', '200', '2', 2014, now()),
- (NULL, '1', '1', '200', '3', 2014, now()),
- (NULL, '1', '1', '200', '4', 2014, now()),
- (NULL, '1', '1', '200', '5', 2014, now()),
- (NULL, '1', '1', '200', '6', 2014, now()),
- (NULL, '1', '1', '200', '7', 2014, now()),
- (NULL, '1', '1', '200', '8', 2014, now()),
- (NULL, '1', '1', '200', '9', 2014, now()),
- (NULL, '1', '1', '200', '10', 2014, now()),
- (NULL, '1', '1', '200', '11', 2014, now()),
- (NULL, '1', '1', '200', '12', 2014, now()),
- (NULL, '2', '1', '250', '1', 2014, now()),
- (NULL, '2', '1', '250', '2', 2014, now()),
- (NULL, '2', '1', '250', '3', 2014, now()),
- (NULL, '2', '1', '250', '4', 2014, now()),
- (NULL, '2', '1', '250', '5', 2014, now()),
- (NULL, '2', '1', '250', '6', 2014, now()),
- (NULL, '2', '1', '250', '7', 2014, now()),
- (NULL, '2', '1', '250', '8', 2014, now()),
- (NULL, '2', '1', '250', '9', 2014, now()),
- (NULL, '2', '1', '250', '10', 2014, now()),
- (NULL, '2', '1', '250', '11', 2014, now()),
- (NULL, '2', '1', '250', '12', 2014, now()),
- (NULL, '3', '1', '250', '1', 2014, now()),
- (NULL, '3', '1', '250', '2', 2014, now()),
- (NULL, '3', '1', '250', '3', 2014, now()),
- (NULL, '3', '1', '250', '4', 2014, now()),
- (NULL, '3', '1', '250', '5', 2014, now()),
- (NULL, '3', '1', '250', '6', 2014, now()),
- (NULL, '3', '1', '250', '7', 2014, now()),
- (NULL, '3', '1', '250', '8', 2014, now()),
- (NULL, '3', '1', '250', '9', 2014, now()),
- (NULL, '3', '1', '250', '10', 2014, now()),
- (NULL, '3', '1', '250', '11', 2014, now()),
- (NULL, '3', '1', '250', '12', 2014, now()),
- (NULL, '1', '2', '200', '1', 2014, now()),
- (NULL, '1', '2', '200', '2', 2014, now()),
- (NULL, '1', '2', '200', '3', 2014, now()),
- (NULL, '1', '2', '200', '4', 2014, now()),
- (NULL, '1', '2', '200', '5', 2014, now()),
- (NULL, '1', '2', '200', '6', 2014, now()),
- (NULL, '1', '2', '200', '7', 2014, now()),
- (NULL, '1', '2', '200', '8', 2014, now()),
- (NULL, '1', '2', '200', '9', 2014, now()),
- (NULL, '1', '2', '200', '10', 2014, now()),
- (NULL, '1', '2', '200', '11', 2014, now()),
- (NULL, '1', '2', '200', '12', 2014, now()),
- (NULL, '4', '2', '200', '1', 2014, now()),
- (NULL, '4', '2', '200', '2', 2014, now()),
- (NULL, '4', '2', '200', '3', 2014, now()),
- (NULL, '4', '2', '200', '4', 2014, now()),
- (NULL, '4', '2', '200', '5', 2014, now()),
- (NULL, '4', '2', '200', '6', 2014, now()),
- (NULL, '4', '2', '200', '7', 2014, now()),
- (NULL, '4', '2', '200', '8', 2014, now()),
- (NULL, '4', '2', '200', '9', 2014, now()),
- (NULL, '4', '2', '200', '10', 2014, now()),
- (NULL, '4', '2', '200', '11', 2014, now()),
- (NULL, '4', '2', '200', '12', 2014, now()),
- /**2016**/
- (NULL, '1', '1', '200', '1', 2016, now()),
- (NULL, '1', '1', '200', '2', 2016, now()),
- (NULL, '1', '1', '200', '3', 2016, now()),
- (NULL, '2', '1', '250', '1', 2016, now()),
- (NULL, '3', '1', '250', '1', 2016, now()),
- (NULL, '3', '1', '250', '2', 2016, now()),
- (NULL, '1', '2', '200', '1', 2016, now()),
- (NULL, '1', '2', '200', '2', 2016, now()),
- (NULL, '1', '2', '200', '3', 2016, now()),
- (NULL, '4', '2', '200', '1', 2016, now()),
- (NULL, '4', '2', '200', '2', 2016, now());
- SELECT
- st.name, sp.name
- FROM
- students AS st
- JOIN
- sports AS sp ON st.id IN (SELECT
- ss.student_id
- FROM
- student_sport AS ss
- WHERE
- ss.sportGroup_id IN (SELECT
- sg.id
- FROM
- sportgroups AS sg
- WHERE
- sg.sport_id = sp.id));
- SELECT
- students.name, sports.name
- FROM
- students
- JOIN
- student_sport AS ss ON students.id = ss.student_id
- JOIN
- sportgroups ON sportgroups.id = ss.sportGroup_id
- JOIN
- sports ON sports.id = sportgroups.sport_id;
- SELECT
- st.name, c.name, c.egn
- FROM
- students AS st
- JOIN
- coaches AS c ON st.id IN (SELECT
- tax.student_id
- FROM
- taxespayments AS tax
- WHERE
- tax.group_id IN (SELECT
- sg.id
- FROM
- sportgroups AS sg
- WHERE
- sg.coach_id = c.id));
- SELECT DISTINCT
- st.name, SUM(tax.paymentAmount)
- FROM
- students AS st
- JOIN
- taxespayments AS tax ON st.id = tax.student_id
- WHERE
- st.id = 1
- ORDER BY tax.student_id;
- SELECT
- s.name, SUM(paymentAmount)
- FROM
- sports AS s
- JOIN
- taxespayments AS tp ON s.id IN (SELECT
- sg.sport_id
- FROM
- sportgroups AS sg
- WHERE
- sg.id = tp.group_id);
- SELECT
- st1.name AS Student1, st2.name AS Student2, sp.name AS Sport
- FROM
- students AS st1
- JOIN
- students AS st2 ON st1.id > st2.id
- JOIN
- sports AS sp ON (st1.id IN (SELECT
- ss.student_id
- FROM
- student_sport AS ss
- WHERE
- ss.sportGroup_id IN (SELECT
- sg.id
- FROM
- sportgroups AS sg
- WHERE
- sg.sport_id = sp.id))
- AND (st2.id IN (SELECT
- ss.student_id
- FROM
- student_sport AS ss
- WHERE
- ss.sportGroup_id IN (SELECT
- sg.id
- FROM
- sportgroups AS sg
- WHERE
- sg.sport_id = sp.id))))
- WHERE
- st1.id IN (SELECT
- student_id
- FROM
- student_sport
- WHERE
- sportGroup_id IN (SELECT
- sportGroup_id
- FROM
- student_sport
- WHERE
- student_id = st2.id))
- ORDER BY Sport;
- SELECT
- st1.name AS Student1, st2.name AS Student2, sp.name AS Sport
- FROM
- students AS st1
- JOIN
- students AS st2 ON st1.id > st2.id
- JOIN
- sports AS sp ON (st1.id IN (SELECT
- ss.student_id
- FROM
- student_sport AS ss
- WHERE
- ss.sportGroup_id IN (SELECT
- sg.id
- FROM
- sportgroups AS sg
- WHERE
- sg.sport_id = sp.id))
- AND (st2.id IN (SELECT
- ss.student_id
- FROM
- student_sport AS ss
- WHERE
- ss.sportGroup_id IN (SELECT
- sg.id
- FROM
- sportgroups AS sg
- WHERE
- sg.sport_id = sp.id))));
- SELECT
- st.name, sp.name
- FROM
- students AS st
- JOIN
- sports AS sp ON st.id IN (SELECT
- ss.student_id
- FROM
- student_sport AS ss
- WHERE
- ss.sportGroup_id IN (SELECT
- sg.id
- FROM
- sportgroups AS sg
- WHERE
- sg.sport_id = sp.id));
- SELECT
- sg.location, sp.name
- FROM
- sportgroups AS sg
- LEFT JOIN
- sports AS sp ON sg.sport_id = sp.id
- UNION (SELECT
- sg.location, sp.name
- FROM
- sportgroups AS sg
- RIGHT JOIN
- sports AS sp ON sg.sport_id = sp.id);
- SELECT
- students.name, sports.name, coaches.name
- FROM
- students
- JOIN
- sports ON students.id IN (SELECT
- student_sport.student_id
- FROM
- student_sport
- WHERE
- student_sport.sportGroup_id IN (SELECT
- sportgroups.id
- FROM
- sportgroups
- WHERE
- sportgroups.sport_id = sports.id))
- JOIN
- coaches ON coaches.id = students.id
- WHERE
- coaches.name LIKE '%Ivan%'
- AND students.class = '11';
- SELECT
- students.name, sports.name
- FROM
- students
- JOIN
- sports ON students.id IN (SELECT
- student_sport.student_id
- FROM
- student_sport
- WHERE
- student_sport.sportGroup_id IN (SELECT
- sportgroups.id
- FROM
- sportgroups
- WHERE
- sportgroups.sport_id = sports.id))
- WHERE
- students.class = 11
- AND sports.name = 'Football';
- SELECT
- st.name, sp.name, tax.year
- FROM
- students AS st
- JOIN
- student_sport AS ss ON st.id = ss.student_id
- JOIN
- sportgroups AS sg ON sg.id = ss.sportGroup_id
- JOIN
- sports AS sp ON sp.id = sg.sport_id
- JOIN
- taxespayments AS tax ON st.id = tax.student_id
- WHERE
- st.class = '11' AND sp.name = 'Football';
- SELECT
- st.name, sg.hourOfTraining
- FROM
- students AS st
- JOIN
- sportgroups AS sg ON st.id IN (SELECT
- tax.student_id
- FROM
- taxespayments AS tax
- WHERE
- tax.group_id = sg.id);
- SELECT
- c.name, sp.name
- FROM
- coaches AS c
- JOIN
- sports AS sp ON c.id IN (SELECT
- sg.coach_id
- FROM
- sportgroups AS sg
- WHERE
- sg.sport_id = sp.id);
- SELECT
- st.name, st.address, SUM(paymentAmount)
- FROM
- students AS st
- JOIN
- taxespayments AS tax ON st.id = tax.student_id
- WHERE
- st.id = 1 OR st.id = 2 OR st.id = 3
- OR st.id = 4
- OR st.id = 5
- OR st.id = 6
- GROUP BY paymentAmount
- LIMIT 6;
- SELECT
- group_id, SUM(paymentAmount)
- FROM
- taxespayments
- GROUP BY SUM(paymentAmount);
- UPDATE taxespayments
- SET
- group_id = '3'
- WHERE
- id = 15;
- SELECT
- st.name, sp.name
- FROM
- students AS st
- JOIN
- sports AS sp ON st.id IN (SELECT
- ss.student_id
- FROM
- student_sport AS ss
- WHERE
- ss.sportGroup_id IN (SELECT
- sg.id
- FROM
- sportgroups AS sg
- WHERE
- sg.sport_id = sp.id));
- select st.name,sp.name
- from students as st
- join (sports as sp )
- on st.id in(
- select tax.student_id
- from taxespayments as tax
- where tax.group_id in(
- select sg.id
- from sportgroups as sg
- where sg.id=sp.id))
- where sp.name like 'football';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement