hackerrank-sql刷题-easy部分

hackerrank之mysql的放弃

1. 用mysql选择奇偶数

获取偶数的方法

select * from pos_info_report_tmp_20110712 r

where mod(r.id,2) = 0;

获取奇数的方法

select * from pos_info_report_tmp_20110712 r

where mod(r.id,2) = 1;

在hackerrank上面的例子


10450029-5e300dd3da300ecb.png
image.png
select distinct(city)
from station
where mod(id,2)=0; 

2.求两个column的差

题目为:


10450029-d1ab1aaf53535a79.png
image.png
select count(city) - count(distinct(city))
from station;

3.Weather Observation Station 5

10450029-a5db4a00a9fd4082.png
image.png
select CITY,LENGTH(CITY) from STATION order by Length(CITY) asc, CITY limit 1;
select CITY,LENGTH(CITY) from STATION order by Length(CITY) desc, CITY limit 1; 

4.Weather Observation Station 6

10450029-3e842026bf9e45c9.png
image.png

查找以元音字母开头的城市名称
具体笔记在mysql中的60. 牵扯到了正则表达式

select distinct city
from station
where city regexp '^[aeiou]';

5.Weather Observation Station 7

10450029-55ab5005b1dc066a.png
image.png

与第4题类似,求以元音字母结尾的城市

select distinct city
from station
where city regexp '[aeiou]$';

6.Weather Observation Station 8

10450029-0acd35e3e96b39eb.png
image.png

同类型题,查找以元音字母开头和结尾的城市名称

select distinct city
from station
where  city regexp '^[aeiou]'
and city regexp '[aeiou]$';

感觉还可以优化一下。。。

SELECT DISTINCT city FROM station WHERE city RLIKE '^[aeiou].*[aeiou]$';

在讨论区看见的解法,使用RLIKE

7.Weather Observation Station 9

10450029-b334bf5adceeb6b1.png
image.png

同类型题,查找城市名字不以元音字母开头的

select distinct(city)
from station
where city  regexp '^[^aeiou]';

8.Weather Observation Station 10

10450029-ba45674140ee2e22.png
image.png

查找不以元音字母结尾的城市名称

select distinct city
from station
where city not regexp '[aeiou]$';

not regexp(): Negation of REGEXP

9.Weather Observation Station 11

10450029-accf640acb2d6659.png
image.png

查找不以元音字母结尾或者开头的城市

SELECT DISTINCT CITY 
FROM STATION 
WHERE city regexp '^[^aeiou]'
OR city  not regexp '[aeiou]$';

10.Weather Observation Station 12

查找不以元音字母结尾和者开头的城市

SELECT DISTINCT CITY 
FROM STATION 
WHERE city regexp '^[^aeiou]'
and city  not regexp '[aeiou]$';

11.Higher Than 75 Marks

10450029-a2fdd17f68cfd1cc.png
image.png

10450029-9a41b7daa38d024b.png
image.png
select name
from students
where marks >75
order by right(name,3),id asc;

12.Type of Triangle

10450029-e63cdccbf345b7e2.png
image.png

10450029-7737983c8cd3a39b.png
image.png

Write a query identifying the type of each record in the TRIANGLES table using its three side lengths. Output one of the following statements for each record in the table:
定义三角形类型

SELECT  CASE
    WHEN A+B>C AND B+C>A AND A+C>B THEN
        CASE 
            WHEN A=B AND B=C THEN 'Equilateral'
            WHEN A=B or A=C OR B=C THEN 'Isosceles'
            ELSE 'Scalene'
        END
    ELSE 'Not A Triangle'
END
FROM TRIANGLES ;
select case
when A+B <= C or A+C <= B or B+C <= A then "Not A Triangle"
when A=B and B=C then "Equilateral"
when A=B or A=C or B=C then "Isosceles"
else "Scalene"
end 
from TRIANGLES;

ps: 答案来自stackoverflow, 要先排除不是三角形的,再去定义三角形类型?
case的逻辑是一层接着一层的,不是并列同时发生的

13.Revising Aggregations - The Count Function

10450029-9048017b89da075e.png
image.png
select count(id)
from city
where population >100000;

14.Revising Aggregations - The Sum Function

10450029-ec2c970822afcee7.png
image.png
select sum(population)
from city
where district = 'california';

15.Revising Aggregations - Averages

10450029-0aab77446660db56.png
image.png
select avg(population)
from city
where district ='california';

16.Average Population

10450029-16b5801ebbd0ac0d.png
image.png
select round(avg(population),0)
from city;

或者可以尝试用ceil()

17.Japan Population

10450029-dfdeff009da8b714.png
image.png
select sum(population)
from city
where countrycode = 'JPN';

18.Population Density Difference

10450029-651352b4f29c010d.png
image.png
select max(population)- min(population) as diff
from city;

19. The Blunder

10450029-90a28f71ae5d467c.png
image.png
10450029-d8ec9aa097530a7a.png
image.png

10450029-d85c93fb76cd4881.png
image.png
SELECT CEIL(AVG(Salary)-AVG(REPLACE(Salary,'0',''))) FROM EMPLOYEES;

答案来自讨论区,第一开始想用trim,但是trim只可以删除字符串前后的0,像是2006 这种金额,中间有0,就没有办法删除。需要用replace

https://www.jianshu.com/p/5ec5e7278110
https://dev.mysql.com/doc/refman/8.0/en/regexp.html
https://stackoverflow.com/questions/38561938/type-of-triangle-in-mysql

猜你喜欢

转载自blog.csdn.net/weixin_33958366/article/details/90892030
今日推荐