diff --git a/SQL/Advanced Select/1-the-pads.sql b/SQL/Advanced Select/1-the-pads.sql new file mode 100644 index 0000000..b62d770 --- /dev/null +++ b/SQL/Advanced Select/1-the-pads.sql @@ -0,0 +1,2 @@ +select concat(Name, '(', SUBSTRING(Occupation, 1, 1), ')') from OCCUPATIONS order by Name; +select concat('There are total ', count(*), ' ', lower(Occupation), 's.') from OCCUPATIONS group by Occupation order by count(*) ASC, Occupation ASC; diff --git a/SQL/Advanced Select/2-occupations.sql b/SQL/Advanced Select/2-occupations.sql new file mode 100644 index 0000000..7e328ea --- /dev/null +++ b/SQL/Advanced Select/2-occupations.sql @@ -0,0 +1,28 @@ +# brain not work for this. +SELECT + IFNULL(empId,'Totals') AS EmpId, -- outer query labels rollup row + sums.2005, sums.2006, sums.2007, -- and calculates horizontal sums + sums.2005 + sums.2006 + sums.2007 AS Sums +FROM ( -- inner query groups by employee + SELECT -- with an expression for each column + EmpID, + SUM(IF(Yr=2005,sales,0)) As '2005', + SUM(IF(Yr=2006,sales,0)) As '2006', + SUM(IF(Yr=2007,sales,0)) As '2007' + FROM Sales + GROUP BY EmpID WITH ROLLUP +) AS sums; + +SELECT + pivot.Doctor, pivot.Professor, + pivot.Single, pivot.Actor +FROM ( + SELECT + IF(Occupation='Doctor',Name,NULL) AS 'Doctor', + IF(Occupation='Professor',Name,NULL) AS 'Professor', + IF(Occupation='Single',Name,NULL) AS 'Single', + IF(Occupation='Actor',Name,NULL) AS 'Actor' + FROM OCCUPATIONS + HAVING count(*) = count(Name) + ORDER BY Name +) as pivot; diff --git a/SQL/Advanced Select/3-binary-search-tree-1.sql b/SQL/Advanced Select/3-binary-search-tree-1.sql new file mode 100644 index 0000000..3f36a6d --- /dev/null +++ b/SQL/Advanced Select/3-binary-search-tree-1.sql @@ -0,0 +1,8 @@ +SELECT N, +CASE +WHEN P is NULL THEN 'Root' +WHEN N in (SELECT P FROM BST) THEN 'Inner' +ELSE 'Leaf' +END +FROM BST +ORDER by N; diff --git a/SQL/Advanced Select/4-what-type-of-triangle.sql b/SQL/Advanced Select/4-what-type-of-triangle.sql new file mode 100644 index 0000000..87faa92 --- /dev/null +++ b/SQL/Advanced Select/4-what-type-of-triangle.sql @@ -0,0 +1,8 @@ +SELECT +CASE +WHEN (A + B <= C) OR (A + C <= B) OR (B + C < A) THEN 'Not A Triangle' +WHEN (A = B) AND (A = C) THEN 'Equilateral' +WHEN (A != B) AND (A != C) AND (B != C) THEN 'Scalene' +ELSE 'Isosceles' +END +FROM TRIANGLES diff --git a/SQL/Aggregation/1-revising-aggregations-the-count-function.sql b/SQL/Aggregation/1-revising-aggregations-the-count-function.sql new file mode 100644 index 0000000..0bf36fc --- /dev/null +++ b/SQL/Aggregation/1-revising-aggregations-the-count-function.sql @@ -0,0 +1 @@ +select count(*) from CITY WHERE POPULATION > 100000; diff --git a/SQL/Aggregation/10-weather-observation-station-15.sql b/SQL/Aggregation/10-weather-observation-station-15.sql new file mode 100644 index 0000000..5fb8779 --- /dev/null +++ b/SQL/Aggregation/10-weather-observation-station-15.sql @@ -0,0 +1 @@ +select round(LONG_W, 4) from STATION where LAT_N = (select max(LAT_N) from STATION where LAT_N < 137.2345); diff --git a/SQL/Aggregation/11-weather-observation-station-16.sql b/SQL/Aggregation/11-weather-observation-station-16.sql new file mode 100644 index 0000000..cf7ac0c --- /dev/null +++ b/SQL/Aggregation/11-weather-observation-station-16.sql @@ -0,0 +1 @@ +select round(min(LAT_N), 4) from STATION WHERE LAT_N > 38.7780; diff --git a/SQL/Aggregation/12-weather-observation-station-17.sql b/SQL/Aggregation/12-weather-observation-station-17.sql new file mode 100644 index 0000000..7254c87 --- /dev/null +++ b/SQL/Aggregation/12-weather-observation-station-17.sql @@ -0,0 +1 @@ +select round(LONG_W, 4) from STATION WHERE LAN_T = (select min(LAT_N) from STATION WHERE LAT_N > 38.7780); diff --git a/SQL/Aggregation/13-weather-observation-station-18.sql b/SQL/Aggregation/13-weather-observation-station-18.sql new file mode 100644 index 0000000..225af16 --- /dev/null +++ b/SQL/Aggregation/13-weather-observation-station-18.sql @@ -0,0 +1 @@ +select round(ABS(MIN(LAT_N) - MIN(LONG_W)) + ABS(MAX(LAT_N) - MAX(LONG_W)), 4) from STATION; diff --git a/SQL/Aggregation/14-weather-observation-station-19.sql b/SQL/Aggregation/14-weather-observation-station-19.sql new file mode 100644 index 0000000..ba3e8f2 --- /dev/null +++ b/SQL/Aggregation/14-weather-observation-station-19.sql @@ -0,0 +1 @@ +select round(SQRT(POW(MIN(LAT_N) - MIN(LONG_W), 2) + POW(MAX(LAT_N) - MAX(LONG_W), 2)), 4) from STATION; diff --git a/SQL/Aggregation/15-weather-observation-station-20.sql b/SQL/Aggregation/15-weather-observation-station-20.sql new file mode 100644 index 0000000..0694ba4 --- /dev/null +++ b/SQL/Aggregation/15-weather-observation-station-20.sql @@ -0,0 +1,6 @@ +SELECT ROUND(x.LAT_N,4) from +STATION x, STATION y +GROUP BY x.LAT_N +HAVING SUM(SIGN(1-SIGN(y.LAT_N-x.LAT_N)))/COUNT(*) > .5 LIMIT 1 + +# sql over. i can't solve them. diff --git a/SQL/Aggregation/2-revising-aggregations-sum.sql b/SQL/Aggregation/2-revising-aggregations-sum.sql new file mode 100644 index 0000000..dc12cab --- /dev/null +++ b/SQL/Aggregation/2-revising-aggregations-sum.sql @@ -0,0 +1 @@ +select sum(POPULATION) from CITY WHERE DISTRICT = 'California'; diff --git a/SQL/Aggregation/3-revising-aggregations-the-average-function.sql b/SQL/Aggregation/3-revising-aggregations-the-average-function.sql new file mode 100644 index 0000000..83cca6b --- /dev/null +++ b/SQL/Aggregation/3-revising-aggregations-the-average-function.sql @@ -0,0 +1 @@ +select avg(POPULATION) from CITY WHERE DISTRICT = 'California'; diff --git a/SQL/Aggregation/4-average-population.sql b/SQL/Aggregation/4-average-population.sql new file mode 100644 index 0000000..ba04642 --- /dev/null +++ b/SQL/Aggregation/4-average-population.sql @@ -0,0 +1 @@ +select FLOOR(avg(POPULATION)) from CITY; diff --git a/SQL/Aggregation/5-japan-population.sql b/SQL/Aggregation/5-japan-population.sql new file mode 100644 index 0000000..2c7d0dd --- /dev/null +++ b/SQL/Aggregation/5-japan-population.sql @@ -0,0 +1 @@ +SELECT SUM(POPULATION) FROM CITY WHERE COUNTRYCODE = 'JPN'; diff --git a/SQL/Aggregation/6-population-density-difference.sql b/SQL/Aggregation/6-population-density-difference.sql new file mode 100644 index 0000000..8769fab --- /dev/null +++ b/SQL/Aggregation/6-population-density-difference.sql @@ -0,0 +1 @@ +SELECT max(POPULATION) - min(POPULATION) FROM CITY; diff --git a/SQL/Aggregation/7-weather-observation-station-2.sql b/SQL/Aggregation/7-weather-observation-station-2.sql new file mode 100644 index 0000000..a36e48e --- /dev/null +++ b/SQL/Aggregation/7-weather-observation-station-2.sql @@ -0,0 +1 @@ +select concat(ROUND(sum(LAT_N),2), ' ', ROUND(sum(LONG_W),2)) from STATION; diff --git a/SQL/Aggregation/8-weather-observation-station-13.sql b/SQL/Aggregation/8-weather-observation-station-13.sql new file mode 100644 index 0000000..dd2db1f --- /dev/null +++ b/SQL/Aggregation/8-weather-observation-station-13.sql @@ -0,0 +1 @@ +select truncate(sum(LAT_N), 4) from STATION where LAT_N < 137.2345 AND LAT_N > 38.7880; diff --git a/SQL/Aggregation/9-weather-observation-station-14.sql b/SQL/Aggregation/9-weather-observation-station-14.sql new file mode 100644 index 0000000..70a1074 --- /dev/null +++ b/SQL/Aggregation/9-weather-observation-station-14.sql @@ -0,0 +1 @@ +select truncate(max(LAT_N), 4) from STATION where LAT_N < 137.2345; diff --git a/SQL/Basic Select/1-revising-the-select-query.sql b/SQL/Basic Select/1-revising-the-select-query.sql new file mode 100644 index 0000000..f5e1d2e --- /dev/null +++ b/SQL/Basic Select/1-revising-the-select-query.sql @@ -0,0 +1 @@ +select * from CITY where POPULATION > 100000 and COUNTRYCODE = 'USA'; diff --git a/SQL/Basic Select/10-weather-observation-station-5.sql b/SQL/Basic Select/10-weather-observation-station-5.sql new file mode 100644 index 0000000..1823825 --- /dev/null +++ b/SQL/Basic Select/10-weather-observation-station-5.sql @@ -0,0 +1,2 @@ +select city, length(city) from station order by length(city) limit 1; +select city, length(city) from station order by length(city) desc limit 1; diff --git a/SQL/Basic Select/11-weather-observation-station-6.sql b/SQL/Basic Select/11-weather-observation-station-6.sql new file mode 100644 index 0000000..84b4fa8 --- /dev/null +++ b/SQL/Basic Select/11-weather-observation-station-6.sql @@ -0,0 +1 @@ +select DISTINCT CITY from STATION WHERE (CITY LIKE 'A%' OR CITY LIKE 'E%' OR CITY LIKE 'I%' OR CITY LIKE 'O%' OR CITY LIKE 'U%'); diff --git a/SQL/Basic Select/12-weather-observation-station-7.sql b/SQL/Basic Select/12-weather-observation-station-7.sql new file mode 100644 index 0000000..08fb750 --- /dev/null +++ b/SQL/Basic Select/12-weather-observation-station-7.sql @@ -0,0 +1 @@ +select DISTINCT CITY from STATION WHERE (CITY LIKE '%A' OR CITY LIKE '%E' OR CITY LIKE '%I' OR CITY LIKE '%O' OR CITY LIKE '%U'); diff --git a/SQL/Basic Select/13-weather-observation-station-8.sql b/SQL/Basic Select/13-weather-observation-station-8.sql new file mode 100644 index 0000000..269e7c7 --- /dev/null +++ b/SQL/Basic Select/13-weather-observation-station-8.sql @@ -0,0 +1,2 @@ +# sql serve +select DISTINCT CITY from STATION WHERE CITY LIKE '[AEIOU]%[AEIOU]'; diff --git a/SQL/Basic Select/14-weather-observation-station-9.sql b/SQL/Basic Select/14-weather-observation-station-9.sql new file mode 100644 index 0000000..db39d19 --- /dev/null +++ b/SQL/Basic Select/14-weather-observation-station-9.sql @@ -0,0 +1,3 @@ +# sql serve +select DISTINCT CITY from STATION WHERE CITY RLIKE '[^AEIOUaeiou]%'; +select DISTINCT CITY from STATION WHERE CITY LIKE '[^AEIOU]%'; diff --git a/SQL/Basic Select/15-weather-observation-station-10.sql b/SQL/Basic Select/15-weather-observation-station-10.sql new file mode 100644 index 0000000..640053c --- /dev/null +++ b/SQL/Basic Select/15-weather-observation-station-10.sql @@ -0,0 +1 @@ +select DISTINCT CITY from STATION WHERE CITY LIKE '%[^AEIOU]'; diff --git a/SQL/Basic Select/16-weather-observation-station-11.sql b/SQL/Basic Select/16-weather-observation-station-11.sql new file mode 100644 index 0000000..b6f5bd8 --- /dev/null +++ b/SQL/Basic Select/16-weather-observation-station-11.sql @@ -0,0 +1 @@ +select DISTINCT CITY from STATION WHERE CITY LIKE '[^AEIOU]%' OR CITY LIKE '%[^AEIOU]'; diff --git a/SQL/Basic Select/17-weather-observation-station-12.sql b/SQL/Basic Select/17-weather-observation-station-12.sql new file mode 100644 index 0000000..74155ba --- /dev/null +++ b/SQL/Basic Select/17-weather-observation-station-12.sql @@ -0,0 +1 @@ +select DISTINCT CITY from STATION WHERE CITY LIKE '[^AEIOU]%[^AEIOU]'; diff --git a/SQL/Basic Select/18-more-than-75-marks.sql b/SQL/Basic Select/18-more-than-75-marks.sql new file mode 100644 index 0000000..9cddd8d --- /dev/null +++ b/SQL/Basic Select/18-more-than-75-marks.sql @@ -0,0 +1 @@ +select Name from STUDENTS where Marks > 75 order by substr(Name, -3) ASC, ID ASC diff --git a/SQL/Basic Select/2-revising-the-select-query-2.sql b/SQL/Basic Select/2-revising-the-select-query-2.sql new file mode 100644 index 0000000..76b0138 --- /dev/null +++ b/SQL/Basic Select/2-revising-the-select-query-2.sql @@ -0,0 +1 @@ +select NAME from CITY where POPULATION > 120000 and COUNTRYCODE = 'USA'; diff --git a/SQL/Basic Select/3-select-all-sql.sql b/SQL/Basic Select/3-select-all-sql.sql new file mode 100644 index 0000000..cd9be6f --- /dev/null +++ b/SQL/Basic Select/3-select-all-sql.sql @@ -0,0 +1 @@ +select * from CITY; diff --git a/SQL/Basic Select/4-select-by-id.sql b/SQL/Basic Select/4-select-by-id.sql new file mode 100644 index 0000000..a984c33 --- /dev/null +++ b/SQL/Basic Select/4-select-by-id.sql @@ -0,0 +1 @@ +select * from CITY WHERE ID = 1661; diff --git a/SQL/Basic Select/5-japanese-cities-detail.sql b/SQL/Basic Select/5-japanese-cities-detail.sql new file mode 100644 index 0000000..35309dc --- /dev/null +++ b/SQL/Basic Select/5-japanese-cities-detail.sql @@ -0,0 +1,2 @@ +select * from CITY WHERE COUNTRYCODE = 'JPN' +select * from CITY WHERE COUNTRYCODE = 'JPN'; diff --git a/SQL/Basic Select/6-japanese-cities-name.sql b/SQL/Basic Select/6-japanese-cities-name.sql new file mode 100644 index 0000000..72968fe --- /dev/null +++ b/SQL/Basic Select/6-japanese-cities-name.sql @@ -0,0 +1 @@ +select NAME from CITY where COUNTRYCODE = 'JPN'; diff --git a/SQL/Basic Select/7-weather-observation-station-1.sql b/SQL/Basic Select/7-weather-observation-station-1.sql new file mode 100644 index 0000000..b8a04bb --- /dev/null +++ b/SQL/Basic Select/7-weather-observation-station-1.sql @@ -0,0 +1 @@ +select DISTINCT(CITY) from STATION where ID % 2 = 0 ORDER by CITY DESC; diff --git a/SQL/Basic Select/8-weather-observation-station-3.sql b/SQL/Basic Select/8-weather-observation-station-3.sql new file mode 100644 index 0000000..b8a04bb --- /dev/null +++ b/SQL/Basic Select/8-weather-observation-station-3.sql @@ -0,0 +1 @@ +select DISTINCT(CITY) from STATION where ID % 2 = 0 ORDER by CITY DESC; diff --git a/SQL/Basic Select/9-weather-observation-station-4.sql b/SQL/Basic Select/9-weather-observation-station-4.sql new file mode 100644 index 0000000..40d7366 --- /dev/null +++ b/SQL/Basic Select/9-weather-observation-station-4.sql @@ -0,0 +1 @@ +select count(CITY) - count(DISTINCT(CITY)) FROM STATION;