diff --git a/exercices/correction/serie 6/1.sql b/exercices/correction/serie 6/1.sql new file mode 100644 index 0000000000000000000000000000000000000000..a296f4572070a91c2b9de5549b801c85100af061 --- /dev/null +++ b/exercices/correction/serie 6/1.sql @@ -0,0 +1,5 @@ +-- 1 +SELECT V.login, firstname, lastname, loyalty_points +FROM Visitor as V +INNER JOIN Fidelity as F ON V.login = F.login +WHERE loyalty_points < 50; \ No newline at end of file diff --git a/exercices/correction/serie 6/10.sql b/exercices/correction/serie 6/10.sql new file mode 100644 index 0000000000000000000000000000000000000000..4c72f4f3b0970072af97ce0efdf5058f04298822 --- /dev/null +++ b/exercices/correction/serie 6/10.sql @@ -0,0 +1,11 @@ +-- 10 +SELECT * +FROM Visitor +WHERE login NOT IN (SELECT login FROM Registration); + +--ou + +SELECT * +FROM Visitor +LEFT JOIN Registration ON Visitor.login = Registration.login +WHERE id_conference IS NULL; diff --git a/exercices/correction/serie 6/11.sql b/exercices/correction/serie 6/11.sql new file mode 100644 index 0000000000000000000000000000000000000000..1d22a99dca64585101e469b6240efe543955c394 --- /dev/null +++ b/exercices/correction/serie 6/11.sql @@ -0,0 +1,5 @@ +-- 11 +SELECT V.login, firstname, lastname, COUNT(id_conference) +FROM Visitor AS V +LEFT JOIN Registration AS R ON V.login = R.login +GROUP BY V.login; \ No newline at end of file diff --git a/exercices/correction/serie 6/12.sql b/exercices/correction/serie 6/12.sql new file mode 100644 index 0000000000000000000000000000000000000000..fa51ae0eadc1173196ec3e76440606c4b021dd73 --- /dev/null +++ b/exercices/correction/serie 6/12.sql @@ -0,0 +1,5 @@ +-- 12 +SELECT lastname, firstname, start_date, end_date, name +FROM Speaker AS S +INNER JOIN Stay AS ST ON S.login = ST.login +INNER JOIN Hotel AS H ON ST.id_hotel = H.id_hotel; \ No newline at end of file diff --git a/exercices/correction/serie 6/13.sql b/exercices/correction/serie 6/13.sql new file mode 100644 index 0000000000000000000000000000000000000000..32e71f9a49669c7425405f96fdd8a6b9e2afeb14 --- /dev/null +++ b/exercices/correction/serie 6/13.sql @@ -0,0 +1,6 @@ +-- 13 +SELECT T.title, COUNT(DISTINCT login) +FROM Topic AS T +LEFT JOIN Conference AS C ON T.title = C.title +LEFT JOIN Registration AS R ON C.id_conference = R.id_conference +GROUP BY T.title; \ No newline at end of file diff --git a/exercices/correction/serie 6/14.sql b/exercices/correction/serie 6/14.sql new file mode 100644 index 0000000000000000000000000000000000000000..6fbb188a3c88f844b18d810ba57adb22206564fe --- /dev/null +++ b/exercices/correction/serie 6/14.sql @@ -0,0 +1,11 @@ +-- 14 +SELECT C.name, start_date, end_date, +( + SELECT COUNT(R.login) FROM Registration AS R + WHERE C.id_conference = R.id_conference +) AS Visitors, +( + SELECT COUNT(P.login) FROM Participation AS P + WHERE C.id_conference = P.id_conference +) AS Speakers +FROM Conference AS C; \ No newline at end of file diff --git a/exercices/correction/serie 6/15.sql b/exercices/correction/serie 6/15.sql new file mode 100644 index 0000000000000000000000000000000000000000..e98bc66a5a81f4710f05e0255eb7eb9301cd9065 --- /dev/null +++ b/exercices/correction/serie 6/15.sql @@ -0,0 +1,6 @@ +-- 15 +SELECT name, SUM(IFNULL(fees,0)) AS total +FROM Conference AS C +LEFT JOIN Participation AS P ON C.id_conference = P.id_conference +GROUP BY C.id_conference +ORDER BY total DESC; \ No newline at end of file diff --git a/exercices/correction/serie 6/16.sql b/exercices/correction/serie 6/16.sql new file mode 100644 index 0000000000000000000000000000000000000000..1a17493da96a6ef616293e1f36acef45f605153b --- /dev/null +++ b/exercices/correction/serie 6/16.sql @@ -0,0 +1,6 @@ +-- 16 +SELECT T.title, SUM(IFNULL(fees,0)) AS S +FROM Topic AS T LEFT JOIN Conference AS C ON T.title = C.title +LEFT JOIN Participation AS P ON C.id_conference = P.id_conference +GROUP BY T.title +HAVING S < 20000; \ No newline at end of file diff --git a/exercices/correction/serie 6/17.sql b/exercices/correction/serie 6/17.sql new file mode 100644 index 0000000000000000000000000000000000000000..e9c745b9de36c9676685b9a026a2a528cc3f0141 --- /dev/null +++ b/exercices/correction/serie 6/17.sql @@ -0,0 +1,5 @@ +-- 17 +SELECT S.login, firstname, lastname, COUNT(id_conference) +FROM Speaker AS S +LEFT JOIN Participation AS P ON S.login = P.login +GROUP BY S.login; \ No newline at end of file diff --git a/exercices/correction/serie 6/18.sql b/exercices/correction/serie 6/18.sql new file mode 100644 index 0000000000000000000000000000000000000000..6278899e1e0c3bb276c087bdedae41b4d0f618fe --- /dev/null +++ b/exercices/correction/serie 6/18.sql @@ -0,0 +1,26 @@ +-- 18 +SELECT login, firstname, lastname +FROM Speaker +WHERE NOT EXISTS ( + SELECT * + FROM Participation + WHERE Participation.login = Speaker.login +); + +SELECT login, firstname, lastname +FROM Speaker +WHERE login NOT IN ( + SELECT login + FROM Participation +); + +SELECT Speaker.login, firstname, lastname +FROM Speaker +LEFT JOIN Participation ON Speaker.login = Participation.login +WHERE Participation.login IS NULL; + +SELECT Speaker.login, firstname, lastname +FROM Speaker +LEFT JOIN Participation ON Speaker.login = Participation.login +GROUP BY Speaker.login +HAVING COUNT(Participation.id_conference) = 0; \ No newline at end of file diff --git a/exercices/correction/serie 6/19.sql b/exercices/correction/serie 6/19.sql new file mode 100644 index 0000000000000000000000000000000000000000..c385333d0043245f3bffc5f5375b921d95989a63 --- /dev/null +++ b/exercices/correction/serie 6/19.sql @@ -0,0 +1,6 @@ +-- 19 +SELECT DISTINCT T.title +FROM Topic AS T +INNER JOIN Conference AS C ON C.title = T.title +INNER JOIN Participation AS P ON C.id_conference = P.id_conference +WHERE login LIKE "stregunna14"; \ No newline at end of file diff --git a/exercices/correction/serie 6/2.sql b/exercices/correction/serie 6/2.sql new file mode 100644 index 0000000000000000000000000000000000000000..014f5daf5884336a40f10c02820f740bfa40758a --- /dev/null +++ b/exercices/correction/serie 6/2.sql @@ -0,0 +1,12 @@ +-- 2 +SELECT V.login, firstname, lastname, loyalty_points +FROM Visitor as V +LEFT JOIN Fidelity as F ON V.login = F.login +WHERE IFNULL(loyalty_points,0) < 50 AND V.login LIKE "l%"; + +-- ou + +SELECT V.login, firstname, lastname, loyalty_points +FROM Visitor as V +LEFT JOIN Fidelity as F ON V.login = F.login +WHERE (loyalty_points < 50 OR loyalty_points IS NULL) AND V.login GLOB "l*"; \ No newline at end of file diff --git a/exercices/correction/serie 6/20.sql b/exercices/correction/serie 6/20.sql new file mode 100644 index 0000000000000000000000000000000000000000..d868c8e3610e85a9d32aa5dc9e892bdc0c0505c0 --- /dev/null +++ b/exercices/correction/serie 6/20.sql @@ -0,0 +1,7 @@ +-- 20 +SELECT DISTINCT S.login, firstname, lastname, fees +FROM Speaker AS S +INNER JOIN Participation AS P ON S.login = P.login +WHERE fees = ( + SELECT MAX(fees) FROM Participation +); \ No newline at end of file diff --git a/exercices/correction/serie 6/21.sql b/exercices/correction/serie 6/21.sql new file mode 100644 index 0000000000000000000000000000000000000000..2dd9d50d5528a87130f3c320a94f5fe6b68effa1 --- /dev/null +++ b/exercices/correction/serie 6/21.sql @@ -0,0 +1,30 @@ +-- 21 +SELECT Speaker.login, firstname, lastname +FROM Speaker INNER JOIN Participation ON Speaker.login = Participation.login +GROUP BY Speaker.login +HAVING SUM(fees) = (SELECT MAX(SUM) FROM +(SELECT login, SUM(fees) AS SUM +FROM Participation +GROUP BY login)); + + +SELECT S.login, firstname, lastname, SUM(fees) AS F +FROM Speaker AS S +INNER JOIN Participation AS P ON S.login = P.login +GROUP BY S.login +HAVING F = ( + SELECT MAX(S) FROM ( + SELECT SUM(fees) AS S FROM Participation GROUP BY login + ) AS G +); + + +WITH GBY (login, firstname, lastname, sum) AS ( + SELECT S.login, firstname, lastname, SUM(fees) AS F + FROM Speaker AS S + INNER JOIN Participation AS P ON S.login = P.login + GROUP BY S.login +) +SELECT login, firstname, lastname, sum +FROM GBY +WHERE sum = (SELECT MAX(sum) FROM GBY); \ No newline at end of file diff --git a/exercices/correction/serie 6/22.sql b/exercices/correction/serie 6/22.sql new file mode 100644 index 0000000000000000000000000000000000000000..289298e2bd8d84b93c0e92c2cd7e2ae96e4da658 --- /dev/null +++ b/exercices/correction/serie 6/22.sql @@ -0,0 +1,5 @@ +-- 22 +SELECT COUNT(DISTINCT login) +FROM Participation +INNER JOIN Conference USING (id_conference) +WHERE strftime("%Y", start_date) = "2019"; \ No newline at end of file diff --git a/exercices/correction/serie 6/23.sql b/exercices/correction/serie 6/23.sql new file mode 100644 index 0000000000000000000000000000000000000000..5781f5749828849b8c8656279bf8cbb965ef1ead --- /dev/null +++ b/exercices/correction/serie 6/23.sql @@ -0,0 +1,10 @@ +-- 23 +SELECT * +FROM Visitor +WHERE login NOT IN ( + SELECT Visitor.login + FROM Visitor + INNER JOIN Registration USING (login) + INNER JOIN Conference USING (id_conference) + WHERE strftime("%Y", start_date) = "2019" +); \ No newline at end of file diff --git a/exercices/correction/serie 6/24.sql b/exercices/correction/serie 6/24.sql new file mode 100644 index 0000000000000000000000000000000000000000..b2c9b5253609cacfdfa69081cab6b52a9dd6777c --- /dev/null +++ b/exercices/correction/serie 6/24.sql @@ -0,0 +1,6 @@ +-- 24 +SELECT substr(A.lastname,1,2) AS SUB, A.login, A.firstname, A.lastname, B.login, B.firstname, B.lastname +FROM Visitor AS A +INNER JOIN Visitor AS B ON substr(A.lastname,1,2) = substr(B.lastname,1,2) +WHERE A.login > B.login +ORDER BY SUB; \ No newline at end of file diff --git a/exercices/correction/serie 6/3.sql b/exercices/correction/serie 6/3.sql new file mode 100644 index 0000000000000000000000000000000000000000..dad4f2b45af83b8ed6b7932a02135c6f993faacd --- /dev/null +++ b/exercices/correction/serie 6/3.sql @@ -0,0 +1,4 @@ +-- 3 +SELECT firstname, lastname, loyalty_points +FROM Visitor as V +INNER JOIN Fidelity as F ON V.login = F.login; diff --git a/exercices/correction/serie 6/4.sql b/exercices/correction/serie 6/4.sql new file mode 100644 index 0000000000000000000000000000000000000000..a18ae10fe310ab945a1c88055429c50826620cf1 --- /dev/null +++ b/exercices/correction/serie 6/4.sql @@ -0,0 +1,10 @@ +-- 4 +SELECT * +FROM Conference +WHERE title IN ("databases", "microservices"); + +-- ou + +SELECT * +FROM Conference +WHERE title IS "databases" OR title IS "microservices"; \ No newline at end of file diff --git a/exercices/correction/serie 6/5_et_6.sql b/exercices/correction/serie 6/5_et_6.sql new file mode 100644 index 0000000000000000000000000000000000000000..20c2198e4f86731f17227bb9fe02d32ce030acdc --- /dev/null +++ b/exercices/correction/serie 6/5_et_6.sql @@ -0,0 +1,7 @@ +-- 5 +SELECT title +FROM Topic; + +-- 6 +SELECT DISTINCT title +FROM Conference; \ No newline at end of file diff --git a/exercices/correction/serie 6/7.sql b/exercices/correction/serie 6/7.sql new file mode 100644 index 0000000000000000000000000000000000000000..87d96f6a135c94ba98bf28c27a82a9b5112566fe --- /dev/null +++ b/exercices/correction/serie 6/7.sql @@ -0,0 +1,28 @@ +-- 7 +-- non standard, sqlite only +SELECT * +FROM Visitor +INTERSECT +SELECT * +FROM Speaker; + +-- standard +SELECT * +FROM Visitor +INNER JOIN Speaker ON Visitor.login = Speaker.login; + +-- sous-requête IN +SELECT * +FROM Visitor +WHERE login IN ( + SELECT login FROM Speaker +); + +-- sous-requête EXISTS +SELECT * +FROM Visitor +WHERE EXISTS ( + SELECT login FROM Speaker + WHERE Speaker.login = Visitor.login +); + diff --git a/exercices/correction/serie 6/8.sql b/exercices/correction/serie 6/8.sql new file mode 100644 index 0000000000000000000000000000000000000000..c7694624ba7dbb1eb8e8b0e742e5cdc7b7f53958 --- /dev/null +++ b/exercices/correction/serie 6/8.sql @@ -0,0 +1,6 @@ +-- 8 +SELECT * +FROM Speaker +WHERE login NOT IN ( + SELECT login FROM Visitor +); \ No newline at end of file diff --git a/exercices/correction/serie 6/9.sql b/exercices/correction/serie 6/9.sql new file mode 100644 index 0000000000000000000000000000000000000000..a97fe752c398414ae3654271a28020afd24e8555 --- /dev/null +++ b/exercices/correction/serie 6/9.sql @@ -0,0 +1,5 @@ +-- 9 +SELECT Topic.title, COUNT(id_conference) +FROM Topic +LEFT JOIN Conference ON Conference.title = Topic.title +GROUP BY Topic.title; \ No newline at end of file diff --git a/exercices/correction/serie 6/COM 1 2 3.sql b/exercices/correction/serie 6/COM 1 2 3.sql new file mode 100644 index 0000000000000000000000000000000000000000..520bdcf153130e3e8ede3c9a4ffb9b7c29a9e1ed --- /dev/null +++ b/exercices/correction/serie 6/COM 1 2 3.sql @@ -0,0 +1,13 @@ +-- COMMANDES + +-- 1 +INSERT INTO Visitor VALUES ('toto21', 'Di', 'Dju'); + +-- 2 +INSERT INTO Fidelity VALUES ('toto21', 50); + +-- 3 +INSERT INTO Registration +SELECT 'toto21', id_conference +FROM Conference +WHERE name = 'CurryOn'; \ No newline at end of file diff --git a/exercices/correction/serie 6/COM 4 5.sql b/exercices/correction/serie 6/COM 4 5.sql new file mode 100644 index 0000000000000000000000000000000000000000..52892d2940f7f485aa8da21fdeeaf3d12497b077 --- /dev/null +++ b/exercices/correction/serie 6/COM 4 5.sql @@ -0,0 +1,10 @@ +-- 4 +UPDATE Fidelity +SET loyalty_points = loyalty_points * 2; + +-- 5 +INSERT INTO Fidelity +SELECT login, 50 +FROM Visitor +WHERE login NOT IN (SELECT login FROM Fidelity); + diff --git a/exercices/correction/serie 6/conf.sql b/exercices/correction/serie 6/conf.sql new file mode 100644 index 0000000000000000000000000000000000000000..26fbce69c3619e53f73e713358f53d9ac29d96f8 --- /dev/null +++ b/exercices/correction/serie 6/conf.sql @@ -0,0 +1,278 @@ +-- 1 +SELECT V.login, firstname, lastname, loyalty_points +FROM Visitor as V +INNER JOIN Fidelity as F ON V.login = F.login +WHERE loyalty_points < 50; + +-- 2 +SELECT V.login, firstname, lastname, loyalty_points +FROM Visitor as V +LEFT JOIN Fidelity as F ON V.login = F.login +WHERE IFNULL(loyalty_points,0) < 50 AND V.login LIKE "l%"; + +SELECT V.login, firstname, lastname, loyalty_points +FROM Visitor as V +LEFT JOIN Fidelity as F ON V.login = F.login +WHERE (loyalty_points < 50 OR loyalty_points IS NULL) AND V.login GLOB "l*"; + +-- 3 +SELECT firstname, lastname, loyalty_points +FROM Visitor as V +INNER JOIN Fidelity as F ON V.login = F.login; + +-- 4 +SELECT * +FROM Conference +WHERE title IN ("databases", "microservices"); + +SELECT * +FROM Conference +WHERE title IS "databases" OR title IS "microservices"; + +-- 5 +SELECT title +FROM Topic; + +-- 6 +SELECT DISTINCT title +FROM Conference; + +-- 7 +-- non standard, sqlite only +SELECT * +FROM Visitor +INTERSECT +SELECT * +FROM Speaker; + +-- standard +SELECT * +FROM Visitor +INNER JOIN Speaker ON Visitor.login = Speaker.login; + +-- sous-requête IN +SELECT * +FROM Visitor +WHERE login IN ( + SELECT login FROM Speaker +); + +-- sous-requête EXISTS +SELECT * +FROM Visitor +WHERE EXISTS ( + SELECT login FROM Speaker + WHERE Speaker.login = Visitor.login +); + + +-- 8 +SELECT * +FROM Speaker +WHERE login NOT IN ( + SELECT login FROM Visitor +); + +-- 9 +SELECT Topic.title, COUNT(id_conference) +FROM Topic +LEFT JOIN Conference ON Conference.title = Topic.title +GROUP BY Topic.title; + +-- 10 +SELECT * +FROM Visitor +WHERE login NOT IN (SELECT login FROM Registration); + +SELECT * +FROM Visitor +LEFT JOIN Registration ON Visitor.login = Registration.login +WHERE id_conference IS NULL; + +-- 11 +SELECT V.login, firstname, lastname, COUNT(id_conference) +FROM Visitor AS V +LEFT JOIN Registration AS R ON V.login = R.login +GROUP BY V.login; + + +-- 12 +SELECT lastname, firstname, start_date, end_date, name +FROM Speaker AS S +INNER JOIN Stay AS ST ON S.login = ST.login +INNER JOIN Hotel AS H ON ST.id_hotel = H.id_hotel; + +-- 13 +SELECT T.title, COUNT(DISTINCT login) +FROM Topic AS T +LEFT JOIN Conference AS C ON T.title = C.title +LEFT JOIN Registration AS R ON C.id_conference = R.id_conference +GROUP BY T.title; + +-- 14 +SELECT C.name, start_date, end_date, +( + SELECT COUNT(R.login) FROM Registration AS R + WHERE C.id_conference = R.id_conference +) AS Visitors, +( + SELECT COUNT(P.login) FROM Participation AS P + WHERE C.id_conference = P.id_conference +) AS Speakers +FROM Conference AS C; + +-- 15 +SELECT name, SUM(IFNULL(fees,0)) AS total +FROM Conference AS C +LEFT JOIN Participation AS P ON C.id_conference = P.id_conference +GROUP BY C.id_conference +ORDER BY total DESC; + +-- 16 +SELECT T.title, SUM(IFNULL(fees,0)) AS S +FROM Topic AS T LEFT JOIN Conference AS C ON T.title = C.title +LEFT JOIN Participation AS P ON C.id_conference = P.id_conference +GROUP BY T.title +HAVING S < 20000; + +-- 17 +SELECT S.login, firstname, lastname, COUNT(id_conference) +FROM Speaker AS S +LEFT JOIN Participation AS P ON S.login = P.login +GROUP BY S.login; + +-- 18 +SELECT login, firstname, lastname +FROM Speaker +WHERE NOT EXISTS ( + SELECT * + FROM Participation + WHERE Participation.login = Speaker.login +); + +SELECT login, firstname, lastname +FROM Speaker +WHERE login NOT IN ( + SELECT login + FROM Participation +); + +SELECT Speaker.login, firstname, lastname +FROM Speaker +LEFT JOIN Participation ON Speaker.login = Participation.login +WHERE Participation.login IS NULL; + +SELECT Speaker.login, firstname, lastname +FROM Speaker +LEFT JOIN Participation ON Speaker.login = Participation.login +GROUP BY Speaker.login +HAVING COUNT(Participation.id_conference) = 0; + +-- 19 +SELECT DISTINCT T.title +FROM Topic AS T +INNER JOIN Conference AS C ON C.title = T.title +INNER JOIN Participation AS P ON C.id_conference = P.id_conference +WHERE login LIKE "stregunna14"; + +-- 20 +SELECT DISTINCT S.login, firstname, lastname, fees +FROM Speaker AS S +INNER JOIN Participation AS P ON S.login = P.login +WHERE fees = ( + SELECT MAX(fees) FROM Participation +); + + + + + + + +-- 21 +SELECT Speaker.login, firstname, lastname +FROM Speaker INNER JOIN Participation ON Speaker.login = Participation.login +GROUP BY Speaker.login +HAVING SUM(fees) = (SELECT MAX(SUM) FROM +(SELECT login, SUM(fees) AS SUM +FROM Participation +GROUP BY login)); + + +SELECT S.login, firstname, lastname, SUM(fees) AS F +FROM Speaker AS S +INNER JOIN Participation AS P ON S.login = P.login +GROUP BY S.login +HAVING F = ( + SELECT MAX(S) FROM ( + SELECT SUM(fees) AS S FROM Participation GROUP BY login + ) AS G +); + + +WITH GBY (login, firstname, lastname, sum) AS ( + SELECT S.login, firstname, lastname, SUM(fees) AS F + FROM Speaker AS S + INNER JOIN Participation AS P ON S.login = P.login + GROUP BY S.login +) +SELECT login, firstname, lastname, sum +FROM GBY +WHERE sum = (SELECT MAX(sum) FROM GBY); + + +-- 22 +SELECT COUNT(DISTINCT login) +FROM Participation +INNER JOIN Conference USING (id_conference) +WHERE strftime("%Y", start_date) = "2019"; + + + + + +-- 23 +SELECT * +FROM Visitor +WHERE login NOT IN ( + SELECT Visitor.login + FROM Visitor + INNER JOIN Registration USING (login) + INNER JOIN Conference USING (id_conference) + WHERE strftime("%Y", start_date) = "2019" +); + +-- 24 +SELECT substr(A.lastname,1,2) AS SUB, A.login, A.firstname, A.lastname, B.login, B.firstname, B.lastname +FROM Visitor AS A +INNER JOIN Visitor AS B ON substr(A.lastname,1,2) = substr(B.lastname,1,2) +WHERE A.login > B.login +ORDER BY SUB; + + + + + +-- COMMANDES + +-- 1 +INSERT INTO Visitor VALUES ('toto21', 'Di', 'Dju'); + +-- 2 +INSERT INTO Fidelity VALUES ('toto21', 50); + +-- 3 +INSERT INTO Registration +SELECT 'toto21', id_conference +FROM Conference +WHERE name = 'CurryOn'; + +-- 4 +UPDATE Fidelity +SET loyalty_points = loyalty_points * 2; + +-- 5 +INSERT INTO Fidelity +SELECT login, 50 +FROM Visitor +WHERE login NOT IN (SELECT login FROM Fidelity); \ No newline at end of file diff --git a/exercices/correction/serie5.pdf b/exercices/correction/serie5.pdf new file mode 100644 index 0000000000000000000000000000000000000000..5a98822d02929c7a7e35a676ac1f5a883a3db30c Binary files /dev/null and b/exercices/correction/serie5.pdf differ