Skip to content

Commit

Permalink
i think sql should over
Browse files Browse the repository at this point in the history
  • Loading branch information
sword-jin committed Mar 14, 2016
1 parent 9d0cd63 commit 6fe294e
Show file tree
Hide file tree
Showing 37 changed files with 89 additions and 0 deletions.
2 changes: 2 additions & 0 deletions SQL/Advanced Select/1-the-pads.sql
Original file line number Diff line number Diff line change
@@ -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;
28 changes: 28 additions & 0 deletions SQL/Advanced Select/2-occupations.sql
Original file line number Diff line number Diff line change
@@ -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;
8 changes: 8 additions & 0 deletions SQL/Advanced Select/3-binary-search-tree-1.sql
Original file line number Diff line number Diff line change
@@ -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;
8 changes: 8 additions & 0 deletions SQL/Advanced Select/4-what-type-of-triangle.sql
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select count(*) from CITY WHERE POPULATION > 100000;
1 change: 1 addition & 0 deletions SQL/Aggregation/10-weather-observation-station-15.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select round(LONG_W, 4) from STATION where LAT_N = (select max(LAT_N) from STATION where LAT_N < 137.2345);
1 change: 1 addition & 0 deletions SQL/Aggregation/11-weather-observation-station-16.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select round(min(LAT_N), 4) from STATION WHERE LAT_N > 38.7780;
1 change: 1 addition & 0 deletions SQL/Aggregation/12-weather-observation-station-17.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select round(LONG_W, 4) from STATION WHERE LAN_T = (select min(LAT_N) from STATION WHERE LAT_N > 38.7780);
1 change: 1 addition & 0 deletions SQL/Aggregation/13-weather-observation-station-18.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select round(ABS(MIN(LAT_N) - MIN(LONG_W)) + ABS(MAX(LAT_N) - MAX(LONG_W)), 4) from STATION;
1 change: 1 addition & 0 deletions SQL/Aggregation/14-weather-observation-station-19.sql
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 6 additions & 0 deletions SQL/Aggregation/15-weather-observation-station-20.sql
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions SQL/Aggregation/2-revising-aggregations-sum.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select sum(POPULATION) from CITY WHERE DISTRICT = 'California';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select avg(POPULATION) from CITY WHERE DISTRICT = 'California';
1 change: 1 addition & 0 deletions SQL/Aggregation/4-average-population.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select FLOOR(avg(POPULATION)) from CITY;
1 change: 1 addition & 0 deletions SQL/Aggregation/5-japan-population.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT SUM(POPULATION) FROM CITY WHERE COUNTRYCODE = 'JPN';
1 change: 1 addition & 0 deletions SQL/Aggregation/6-population-density-difference.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT max(POPULATION) - min(POPULATION) FROM CITY;
1 change: 1 addition & 0 deletions SQL/Aggregation/7-weather-observation-station-2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select concat(ROUND(sum(LAT_N),2), ' ', ROUND(sum(LONG_W),2)) from STATION;
1 change: 1 addition & 0 deletions SQL/Aggregation/8-weather-observation-station-13.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select truncate(sum(LAT_N), 4) from STATION where LAT_N < 137.2345 AND LAT_N > 38.7880;
1 change: 1 addition & 0 deletions SQL/Aggregation/9-weather-observation-station-14.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select truncate(max(LAT_N), 4) from STATION where LAT_N < 137.2345;
1 change: 1 addition & 0 deletions SQL/Basic Select/1-revising-the-select-query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select * from CITY where POPULATION > 100000 and COUNTRYCODE = 'USA';
2 changes: 2 additions & 0 deletions SQL/Basic Select/10-weather-observation-station-5.sql
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions SQL/Basic Select/11-weather-observation-station-6.sql
Original file line number Diff line number Diff line change
@@ -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%');
1 change: 1 addition & 0 deletions SQL/Basic Select/12-weather-observation-station-7.sql
Original file line number Diff line number Diff line change
@@ -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');
2 changes: 2 additions & 0 deletions SQL/Basic Select/13-weather-observation-station-8.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# sql serve
select DISTINCT CITY from STATION WHERE CITY LIKE '[AEIOU]%[AEIOU]';
3 changes: 3 additions & 0 deletions SQL/Basic Select/14-weather-observation-station-9.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# sql serve
select DISTINCT CITY from STATION WHERE CITY RLIKE '[^AEIOUaeiou]%';
select DISTINCT CITY from STATION WHERE CITY LIKE '[^AEIOU]%';
1 change: 1 addition & 0 deletions SQL/Basic Select/15-weather-observation-station-10.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select DISTINCT CITY from STATION WHERE CITY LIKE '%[^AEIOU]';
1 change: 1 addition & 0 deletions SQL/Basic Select/16-weather-observation-station-11.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select DISTINCT CITY from STATION WHERE CITY LIKE '[^AEIOU]%' OR CITY LIKE '%[^AEIOU]';
1 change: 1 addition & 0 deletions SQL/Basic Select/17-weather-observation-station-12.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select DISTINCT CITY from STATION WHERE CITY LIKE '[^AEIOU]%[^AEIOU]';
1 change: 1 addition & 0 deletions SQL/Basic Select/18-more-than-75-marks.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select Name from STUDENTS where Marks > 75 order by substr(Name, -3) ASC, ID ASC
1 change: 1 addition & 0 deletions SQL/Basic Select/2-revising-the-select-query-2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select NAME from CITY where POPULATION > 120000 and COUNTRYCODE = 'USA';
1 change: 1 addition & 0 deletions SQL/Basic Select/3-select-all-sql.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select * from CITY;
1 change: 1 addition & 0 deletions SQL/Basic Select/4-select-by-id.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select * from CITY WHERE ID = 1661;
2 changes: 2 additions & 0 deletions SQL/Basic Select/5-japanese-cities-detail.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
select * from CITY WHERE COUNTRYCODE = 'JPN'
select * from CITY WHERE COUNTRYCODE = 'JPN';
1 change: 1 addition & 0 deletions SQL/Basic Select/6-japanese-cities-name.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select NAME from CITY where COUNTRYCODE = 'JPN';
1 change: 1 addition & 0 deletions SQL/Basic Select/7-weather-observation-station-1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select DISTINCT(CITY) from STATION where ID % 2 = 0 ORDER by CITY DESC;
1 change: 1 addition & 0 deletions SQL/Basic Select/8-weather-observation-station-3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select DISTINCT(CITY) from STATION where ID % 2 = 0 ORDER by CITY DESC;
1 change: 1 addition & 0 deletions SQL/Basic Select/9-weather-observation-station-4.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select count(CITY) - count(DISTINCT(CITY)) FROM STATION;

0 comments on commit 6fe294e

Please sign in to comment.