generated from FAMAF-resources/Template-repository
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from FAyrolo/ejercicios_resueltos_y_scripts_de_BD
- Loading branch information
Showing
14 changed files
with
52,970 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
CREATE DATABASE world; | ||
USE world; | ||
|
||
CREATE TABLE country ( `Code` char(3) NOT NULL AUTO_INCREMENT DEFAULT '', | ||
`CountryName` varchar(60) NOT NULL DEFAULT '', | ||
`Continent` varchar(30) NOT NULL DEFAULT '', | ||
`Region` varchar(60) NOT NULL DEFAULT '', | ||
`SurfaceArea` numeric(20, 2) NOT NULL DEFAULT '0.00', | ||
`IndepYear` smallint DEFAULT '0', | ||
`Population` int NOT NULL DEFAULT '0', | ||
`LifeExpectancy` smallint DEFAULT '0', | ||
`GNP` numeric(40, 3) NOT NULL DEFAULT '0.000', | ||
`GNPOld` numeric(40, 3) DEFAULT '0.000', | ||
`LocalName` varchar(60) DEFAULT '', | ||
`GovernmentForm` varchar(60) NOT NULL DEFAULT '', | ||
`HeadOfState` varchar(60) DEFAULT '', | ||
`Capital` int DEFAULT '0', | ||
`Code2` char(2) NOT NULL DEFAULT '', | ||
PRIMARY KEY (`Code`)); | ||
|
||
|
||
CREATE TABLE city ( | ||
`ID` int NOT NULL AUTO_INCREMENT, | ||
`CityName` varchar(60) NOT NULL DEFAULT '', | ||
`CountryCode` char(3) NOT NULL DEFAULT '', | ||
`District` varchar(40) NOT NULL DEFAULT '', | ||
`Population` int NOT NULL DEFAULT '0', | ||
PRIMARY KEY (`ID`), | ||
KEY `CountryCode` (`CountryCode`), | ||
CONSTRAINT `fk_city_country` FOREIGN KEY (`CountryCode`) REFERENCES `country` (`Code`) | ||
); | ||
|
||
|
||
CREATE TABLE countrylanguage ( | ||
`CountryCode` char(3) NOT NULL DEFAULT '', | ||
`Language` varchar(30) NOT NULL DEFAULT '', | ||
`IsOfficial` enum('T', 'F') NOT NULL DEFAULT 'F', | ||
`Percentage` numeric(5, 2) NOT NULL DEFAULT '0.0', | ||
PRIMARY KEY (`CountryCode`, `Language`), | ||
KEY `CountryCode` (`CountryCode`), | ||
CONSTRAINT `fk_countrylanguage_country` FOREIGN KEY (`CountryCode`) REFERENCES `country` (`Code`) | ||
); | ||
|
||
|
||
CREATE TABLE continent ( | ||
`ContinentName` varchar(30) NOT NULL DEFAULT '', | ||
`ContinentArea` int NOT NULL DEFAULT '0', | ||
`LandmassPercent` numeric(4, 2) NOT NULL DEFAULT '0.0', | ||
`MostPopulousCity` varchar(60) DEFAULT '', | ||
PRIMARY KEY (`ContinentName`) | ||
); | ||
|
||
ALTER TABLE country ADD CONSTRAINT fk_country_continent FOREIGN KEY (Continent) REFERENCES continent(ContinentName); | ||
|
||
INSERT INTO continent VALUES ('Africa', '30370000', '20.4', 'Cairo, Egypt'); | ||
INSERT INTO continent VALUES ('Antarctica', '14000000', '9.2', 'McMurdo Station'); | ||
INSERT INTO continent VALUES ('Asia', '44579000', '29.5', 'Mumbai, India'); | ||
INSERT INTO continent VALUES ('Europe', '10180000', '6.8', 'Instanbul, Turquia'); | ||
INSERT INTO continent VALUES ('North America', '24709000', '16.5', 'Ciudad de México, Mexico'); | ||
INSERT INTO continent VALUES ('Oceania', '8600000', '5.9', 'Sydney, Australia'); | ||
INSERT INTO continent VALUES ('South America', '17840000', '12.0', 'São Paulo, Brazil'); | ||
|
||
|
||
-- Parte 2 | ||
--1 | ||
SELECT CountryName, Region from country ORDER BY CountryName, Region; | ||
--2 | ||
SELECT CityName, Population FROM city ORDER BY Population DESC LIMIT 10; | ||
--3 | ||
SELECT CountryName, Region, SurfaceArea, GovernmentForm FROM country ORDER BY SurfaceArea ASC LIMIT 10; | ||
--4 | ||
SELECT CountryName, GovernmentForm FROM country WHERE IndepYear IS NULL; | ||
--5 | ||
SELECT Language, Percentage FROM countrylanguage WHERE IsOfficial = 'T'; | ||
--extra, 6 | ||
UPDATE countrylanguage SET Percentage = '100.0' WHERE CountryCode = 'AIA' AND Language = 'English'; | ||
UPDATE countrylanguage SET Percentage = '0.0' WHERE CountryCode = 'AIA' AND Language = 'English'; | ||
--7 | ||
SELECT * FROM city WHERE District LIKE '%rdoba' AND CountryCode = 'ARG'; -- No me toma la tilde la consola en MySQL | ||
--8 | ||
DELETE FROM city WHERE District LIKE '%rdoba' AND CountryCode != 'ARG'; | ||
--9 | ||
SELECT CountryName, HeadOfState FROM country WHERE HeadOfState LIKE '%John %'; | ||
--10 | ||
SELECT CountryName, Population FROM country WHERE Population BETWEEN 35000000 AND 45000000; | ||
--11 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
-- Parte 2 | ||
--1 | ||
SELECT CountryName, Region from country ORDER BY CountryName, Region; | ||
--2 | ||
SELECT CityName, Population FROM city ORDER BY Population DESC LIMIT 10; | ||
--3 | ||
SELECT CountryName, Region, SurfaceArea, GovernmentForm FROM country ORDER BY SurfaceArea ASC LIMIT 10; | ||
--4 | ||
SELECT CountryName, GovernmentForm FROM country WHERE IndepYear IS NULL; | ||
--5 | ||
SELECT Language, Percentage FROM countrylanguage WHERE IsOfficial = 'T'; | ||
--extra, 6 | ||
UPDATE countrylanguage SET Percentage = '100.0' WHERE CountryCode = 'AIA'; | ||
--7 | ||
SELECT * FROM city WHERE District LIKE '%rdoba' AND CountryCode = 'ARG'; -- No me toma la tilde la consola en MySQL | ||
--8 | ||
DELETE FROM city WHERE District LIKE '%rdoba' AND CountryCode != 'ARG'; | ||
--9 | ||
SELECT CountryName, HeadOfState FROM country WHERE HeadOfState LIKE '%John %'; | ||
--10 | ||
SELECT CountryName, Population FROM country WHERE Population BETWEEN 35000000 AND 45000000; | ||
--11 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
--1 | ||
SELECT city.Name, c.Name, c.Region, c.GovernmentForm | ||
FROM city INNER JOIN country AS c | ||
ON city.CountryCode = c.Code | ||
ORDER BY city.Population DESC LIMIT 10; | ||
--2 | ||
SELECT co.Name, ci.Name, co.Capital, ci.ID | ||
FROM country AS co LEFT JOIN city AS ci | ||
ON co.Capital = ci.ID | ||
ORDER BY co.Population ASC LIMIT 10; | ||
--3 | ||
SELECT co.Name, co.Continent, lang.Language | ||
FROM country AS co INNER JOIN countrylanguage AS lang | ||
ON co.Code = lang.CountryCode | ||
WHERE lang.IsOfficial = 'T'; | ||
--4 | ||
SELECT co.Name, ci.Name | ||
FROM country AS co INNER JOIN city AS ci | ||
ON co.Capital = ci.ID | ||
ORDER BY co.SurfaceArea DESC LIMIT 20; | ||
--5 | ||
SELECT city.Name, lang.Language, lang.Percentage | ||
FROM city INNER JOIN countrylanguage AS lang | ||
ON city.CountryCode = lang.CountryCode | ||
WHERE lang.IsOfficial = 'T' LIMIT 20; | ||
--6 | ||
( | ||
SELECT Name, Population | ||
FROM country | ||
ORDER BY Population DESC LIMIT 10 | ||
) | ||
UNION | ||
( | ||
SELECT Name, Population | ||
FROM country | ||
ORDER BY Population ASC LIMIT 10); | ||
--7 | ||
( | ||
SELECT c.Name | ||
FROM country AS c INNER JOIN countrylanguage AS lang | ||
ON c.Code = lang.CountryCode | ||
WHERE lang.IsOfficial = 'T' AND lang.Language = 'English' | ||
) | ||
INTERSECT | ||
( | ||
SELECT c.Name | ||
FROM country AS c INNER JOIN countrylanguage AS lang | ||
ON c.Code = lang.CountryCode | ||
WHERE lang.IsOfficial = 'T' AND lang.Language = 'French'); | ||
--8 | ||
( | ||
SELECT c.Name | ||
FROM country AS c INNER JOIN countrylanguage AS lang | ||
ON c.Code = lang.CountryCode | ||
WHERE lang.Language = 'English' | ||
) | ||
EXCEPT | ||
( | ||
SELECT c.Name | ||
FROM country AS c INNER JOIN countrylanguage AS lang | ||
ON c.Code = lang.CountryCode | ||
WHERE lang.Language = 'Spanish'); | ||
|
||
--PArte 2 | ||
--1: Si, porque hacer un join por igualdad del codigo y despues quedarse solo las filas que son Argentina, es como hacer un join con una tabla donde ya te quedaste las filas que son Argentina | ||
--Se podría decir que el WHERE actúa como un AND en términos de lógioa de selecciones en tablas | ||
--2. No porque hacer el left join con las 2 condiciones (city.codigo=country.codigo y name=ARgentina) hace que generes la tabla que hacía originalmente el INNER JOIN, concatenada con la tabla en que tenés todos los nombres de ciudad asociados a un país NULL | ||
-- con el WHERE name=Argentina después de joinear se descartan de la tabla las filas que tienen nombre de país NULL |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
--1 | ||
SELECT city.Name, country.Name | ||
FROM city, country | ||
WHERE country.Name IN ( | ||
SELECT country.Name FROM country | ||
WHERE country.Population < 10000 | ||
); | ||
|
||
--2 | ||
SELECT city.name, city.Population | ||
FROM city | ||
WHERE city.Population > ( | ||
SELECT AVG(city.Population) FROM city | ||
); | ||
|
||
--3 | ||
--Minimo de poblacion de un pais asiatico | ||
--SELECT MIN(asian_countries.Population) FROM (SELECT * FROM country WHERE country.Continent = 'Asia') AS asian_countries; | ||
SELECT city.Name, city.Population FROM city | ||
WHERE city.Population >= ( | ||
SELECT MIN(asian_countries.Population) | ||
FROM ( | ||
SELECT * FROM country | ||
WHERE country.Continent = 'Asia' | ||
) AS asian_countries | ||
); | ||
|
||
--Este un extra, para poder corroborar que efectivamente las ciudades no son de un pais asiatico | ||
SELECT ci.Name, co.Name, co.Continent, ci.Population, co.Code FROM ( | ||
SELECT city.Name, city.Population, city.CountryCode FROM city | ||
WHERE city.Population >= ( | ||
SELECT MIN(asian_countries.Population) | ||
FROM ( | ||
SELECT * FROM country | ||
WHERE country.Continent = 'Asia' | ||
) AS asian_countries | ||
) | ||
) AS ci INNER JOIN ( | ||
SELECT * FROM country | ||
WHERE country.Continent != 'Asia' | ||
) AS co | ||
WHERE ci.CountryCode = co.Code; | ||
|
||
--4 | ||
--sub_tabla = (SELECT * FROM countrylanguage WHERE countrylanguage.CountryCode = sp.CountryCode AND countrylanguage.IsOfficial = 'T') AS sub | ||
|
||
SELECT co.Code, co.Name, cl.Language, cl.Percentage | ||
FROM country AS co INNER JOIN countrylanguage AS cl | ||
ON co.Code = cl.CountryCode | ||
WHERE cl.Percentage >= ( | ||
SELECT MAX(sub.Percentage) | ||
FROM ( | ||
SELECT * FROM countrylanguage | ||
WHERE countrylanguage.CountryCode = cl.CountryCode AND countrylanguage.IsOfficial = 'T' | ||
) AS sub | ||
) | ||
AND cl.IsOfficial = 'F'; | ||
|
||
--5 | ||
--ciudades = SELECT co.Region, co.SurfaceArea, ci.Population FROM country AS co INNER JOIN city AS ci ON co.Code = ci.CountryCode WHERE ci.Population > 100000 | ||
SELECT DISTINCT ciudades_100k.Region | ||
FROM ( | ||
SELECT co.Region, co.Name, co.SurfaceArea, ci.Population | ||
FROM country AS co INNER JOIN city AS ci | ||
ON co.Code = ci.CountryCode | ||
WHERE ci.Population > 100000 | ||
) AS ciudades_100k | ||
WHERE ciudades_100k.SurfaceArea < 1000; | ||
|
||
--SIN SUBQUERIES | ||
SELECT DISTINCT co.Region, co.Name, co.SurfaceArea, ci.Population | ||
FROM country AS co INNER JOIN city AS ci | ||
ON co.Code = ci.CountryCode | ||
WHERE co.SurfaceArea < 1000 AND ci.Population > 100000; | ||
|
||
--6 | ||
SELECT co.Code, co.Name, SUM(ci.Population) | ||
FROM country AS co INNER JOIN city AS ci | ||
ON co.Code = ci.CountryCode | ||
GROUP BY co.Code; | ||
|
||
--7 | ||
--inner = SELECT AVG(coso.Percentage) FROM ( SELECT * FROM countrylanguage WHERE countrylanguage.IsOfficial = 'T') as coso; | ||
SELECT co.Code, co.Name, cl.Language, cl.Percentage, cl.IsOfficial | ||
FROM country AS co INNER JOIN countrylanguage AS cl | ||
WHERE cl.Percentage > | ||
( | ||
SELECT AVG(sub.Percentage) | ||
FROM ( | ||
SELECT * FROM countrylanguage | ||
WHERE countrylanguage.IsOfficial = 'T' | ||
) as sub | ||
) | ||
AND cl.IsOfficial = 'F'; | ||
|
||
--8 | ||
SELECT country.Continent, SUM(country.Population) | ||
FROM country | ||
GROUP BY country.Continent; | ||
|
||
--9 | ||
SELECT exp.Continent, exp.LifeExpectancy | ||
FROM ( | ||
SELECT country.Continent, AVG(country.LifeExpectancy) AS 'LifeExpectancy' | ||
FROM country | ||
GROUP BY country.Continent | ||
) AS exp | ||
WHERE exp.LifeExpectancy BETWEEN 40 AND 70; | ||
|
||
--10 | ||
SELECT country.Continent, MIN(country.Population) AS Minima, | ||
MAX(country.Population) AS Maxima, | ||
AVG(country.Population) AS Promedio, | ||
SUM(country.Population) AS Total | ||
FROM country GROUP BY country.Continent; | ||
|
||
--Si hubiese que hacerlo por ciudad de cada continente | ||
SELECT co.Continent, MIN(ci.Population) AS Minima, | ||
MAX(ci.Population) AS Maxima, | ||
AVG(ci.Population) AS Promedio, | ||
SUM(ci.Population) AS Total | ||
FROM country AS co INNER JOIN city AS ci | ||
ON co.Code = ci.CountryCode | ||
GROUP BY co.Continent; | ||
|
||
---PREGUNTA EXTRA: | ||
-- La forma cochina es agregando la columna city.Name a la query, y desactivando el modo "only full group by" de SQL :) |
Oops, something went wrong.