HackerRank
最近在疯狂刷题,推荐一个刷题网站https://www.hackerrank.com/dashboard
题目1、
Query the list of CITY names from STATION that do not start with vowels. Your result cannot contain duplicates.
Input Format
The STATION table is described as follows:
where LAT_N is the northern latitude and LONG_W is the western longitude.
-- Query the list of *CITY* names from **STATION** that *do not start* with vowels. Your result cannot contain duplicates.
方法一:
select
distinct city
from
station
where
city not like 'A%' and
city not like 'O%' and
city not like 'E%' and
city not like 'I%' and
city not like 'U%'
方法二:
-- 将^放在封闭的括号中意味着与将其放在括号之外完全不同。将其放在方括号内可使其匹配所有字符,但括号内的字符除外。因此,我们可以写[^ aeiou]而不是写[bcdfghjklmnpqrstvwxyz]
SELECT DISTINCT CITY FROM STATION
WHERE CITY REGEXP '^[^aeiou]';
方法三:
select DISTINCT city FROM station WHERE substr(city, 1, 1) NOT IN ('a','e','i','o','u');
题目2、
Query all columns for all American cities in the CITY table with populations larger than 100000. The CountryCode for America is USA.
The CITY table is described as follows:
-- Query all columns for all American cities in the CITY table with populations larger than 100000. The CountryCode for America is USA.
select
*
from
CITY
where
countrycode='USA' and population>100000
题目3、
Query the NAME field for all American cities in the CITY table with populations larger than 120000
. The CountryCode for America is USA
.
The CITY table is described as follows:
Current Buffer (saved locally, editable)
-- Query the **NAME** field for all American cities in the **CITY** table with populations larger than `120000`. The *CountryCode* for America is `USA`.
select `NAME`
from CITY
where
CountryCode='USA' and population>120000
题目4、
Query the two cities in STATION with the shortest and longest CITY names, as well as their respective lengths (i.e.: number of characters in the name). If there is more than one smallest or largest city, choose the one that comes first when ordered alphabetically.
The STATION table is described as follows:
-- 使用最短和最长的CITY名称以及它们各自的长度(即名称中的字符数)查询STATION中的两个城市。如果最小或最大城市不止一个,请按字母顺序选择最先出现的城市。
select city, length(city) from station
order by length(city),city asc
limit 1;
select city, length(city) from station
order by length(city) desc
limit 1;