Created
December 28, 2023 17:37
-
-
Save carloocchiena/34f941b336116685d0e52f2fe583b241 to your computer and use it in GitHub Desktop.
SQL code for tutorial for Aziona blog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- create our table | |
CREATE TABLE Videogiochi ( | |
ID INT PRIMARY KEY AUTO_INCREMENT, | |
Nome VARCHAR(100) NOT NULL, | |
AnnoUscita INT NOT NULL, | |
Rating INT CHECK (Rating >= 1 AND Rating <= 5) | |
); | |
-- insert the data | |
INSERT INTO Videogiochi (Nome, AnnoUscita, Rating) VALUES | |
('The Legend of Zelda: Breath of the Wild', 2017, 5), | |
('Red Dead Redemption 2', 2018, 4), | |
('Minecraft', 2011, 5), | |
('Fortnite', 2017, 3), | |
('Cyberpunk 2077', 2020, 2), | |
('Among Us', 2018, 4); | |
-- select all the data | |
SELECT * FROM Videogiochi; | |
-- let's select some specific records | |
SELECT Nome | |
FROM Videogiochi | |
WHERE Rating > 3; | |
SELECT Nome, AnnoUscita, Rating | |
FROM Videogiochi | |
ORDER BY Rating ASC | |
LIMIT 1; | |
SELECT Nome, AnnoUscita | |
FROM Videogiochi | |
ORDER BY AnnoUscita ASC; | |
SELECT AnnoUscita AS AnnoDiRiferimento, AVG(Rating) AS MediaRating | |
FROM Videogiochi | |
GROUP BY AnnoUscita; | |
-- add "Generi" to our original table | |
-- create a new table | |
CREATE TABLE Generi ( | |
ID_Genere INT PRIMARY KEY, | |
NomeGenere VARCHAR(50) NOT NULL | |
); | |
-- insert data into it | |
INSERT INTO Generi (ID_Genere, NomeGenere) VALUES | |
(1, 'Azione'), | |
(2, 'Avventura'), | |
(3, 'RPG'), | |
(4, 'Simulazione'); | |
-- add a new column in the Videogiochi table | |
ALTER TABLE Videogiochi ADD COLUMN ID_Genere INT; | |
-- update the original records | |
UPDATE Videogiochi SET ID_Genere = 1 WHERE Nome = 'Red Dead Redemption 2'; | |
UPDATE Videogiochi SET ID_Genere = 2 WHERE Nome = 'The Legend of Zelda: Breath of the Wild'; | |
UPDATE Videogiochi SET ID_Genere = 3 WHERE Nome = 'Cyberpunk 2077'; | |
UPDATE Videogiochi SET ID_Genere = 4 WHERE Nome = 'Minecraft'; | |
-- select joinining Videogiochi and Generi | |
SELECT Videogiochi.Nome, Generi.NomeGenere | |
FROM Videogiochi | |
JOIN Generi ON Videogiochi.ID_Genere = Generi.ID_Genere; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment