HackerRank数据库题目练习(2)

本文分享了在HackerRank上解决SQL查询挑战的经验,包括筛选不以元音开头的城市名、查询美国大城市人口数据等,提供了多种解决方案并附带详细的SQL代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:

Station.jpg

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:

CITY.jpg

-- 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:
CITY.jpg

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:
Station.jpg

-- 使用最短和最长的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;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值