CITY表:

Field Type
ID number
NAME VARCHAR2(17)
COUNTRYCODE VARCHAR2(3)
DISTRICT VARCHAR2(20)
POPULATION number

1.Revising the Select Query I

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 POPULATION>=100000 and COUNTRYCODE='USA';

2.Revising the Select Query II

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;

3.Select All

Query all columns (attributes) for every row in the CITY table.

select * from CITY;

4.Select By ID

Query all columns for a city in CITY with the ID 1661.

select * from CITY
where ID =1661;

5.Japanese Cities' Attribute

Query all attributes of every Japanese city in the CITY table. The COUNTRYCODE for Japan is JPN.

select * from CITY
where COUNTRYCODE='JPN';

6.Japanese Cities' Name

Query the names of all the Japanese cities in the CITY table. The COUNTRYCODE for Japan is JPN.

select NAME
from CITY
where COUNTRYCODE='JPN'

7.

Query a count of the number of cities in CITY having a Population larger than 10,000.

select count(NAME)
from CITY
where POPULATION>100000;

8.Query the total population of all cities in CITY where District is California.

select sum(POPULATION)
from CITY
where DISTRICT ='California';

9.Query the average population of all cities in CITY where District is California.

select avg(POPULATION)
from CITY
where DISTRICT='California';

10.Query the average population for all cities in CITY, rounded down to the nearest integer.

select ROUND(avg(POPULATION),0)
from CITY;

11.Query the sum of the populations for all Japanese cities in CITY. The COUNTRYCODE for Japan is JPN.

select sum(POPULATION)
from CITY
where COUNTRYCODE='JPN';

12.Query the difference between the maximum and minimum populations in CITY.

select max(POPULATION)-min(POPULATION)
from CITY;

13.Given the CITY and COUNTRY tables, query the sum of the populations of all cities where the CONTINENT is 'Asia'.

Note: CITY.CountryCode and COUNTRY.Code are matching key columns.

select sum(c.POPULATION) from CITY c
left join COUNTRY c1
on c.COUNTRYCODE=c1.CODE
where c1.CONTINENT ='Asia';

14.Given the CITY and COUNTRY tables, query the names of all cities where the CONTINENT is 'Africa'.

Note: CITY.CountryCode and COUNTRY.Code are matching key columns.

select c.NAME
from CITY c
left join COUNTRY c1
on c.COUNTRYCODE=c1.CODE
where c1.CONTINENT='Africa';

15.

STUDENTS表:

Field Type
ID INTEGER
Name String
Maks INTEGER

1.Query the Name of any student in STUDENTS who scored higher than  Marks. Order your output by the last three characters of each name. If two or more students both have names ending in the same last three characters (i.e.: Bobby, Robby, etc.), secondary sort them by ascending ID.

查询 STUDENTS 中任何得分高于 75 的学生的姓名。 按每个名称的最后三个字符对输出进行排序。 如果两个或多个学生的名字都以相同的最后三个字符结尾(即:Bobby.Robby 等),则按 ID 升序对他们进行二次排序。

select Name from STUDENTS
where Marks>75
order by right(Name,3),ID;

解题思路,按照每个名字的最后三个字符排序,order by 和right函数结合使用。

Employee表:

Field Type
employee_id INTEGER
name String
months INTEGER
salary INTEGER

1.Write a query that prints a list of employee names (i.e.: the name attribute) from the Employee table in alphabetical order.

select name
from Employee
order by name;

2.Write a query that prints a list of employee names (i.e.: the name attribute) for employees in Employee having a salary greater than  per month who have been employees for less than  months. Sort your result by ascending employee_id.

编写一个查询,打印员工姓名列表(即:名称属性),用于 Employee 中薪水大于每月且工作时间少于几个月的员工。 按employee_id 升序对结果进行排序。

select name
from Employee
where salary>2000 and months<10
order by employee_id;

3.We define an employee's total earnings to be their monthly salary*months  worked, and the maximum total earnings to be the maximum total earnings for any employee in the Employee table. Write a query to find the maximum total earnings for all employees as well as the total number of employees who have maximum total earnings. Then print these values as  2 space-separated integers.

我们将员工的总收入定义为他们的月薪*工作月数,最大总收入定义为 Employee 表中任何员工的最大总收入。 编写查询以查找所有员工的最大总收入以及拥有最大总收入的员工总数。 然后将这些值打印为 2 个空格分隔的整数。

select salary*months as earning,count(name)
from Employee
group by earning
order by earning desc
limit 1;

4.

------------恢复内容开始------------

CITY表:

Field Type
ID number
NAME VARCHAR2(17)
COUNTRYCODE VARCHAR2(3)
DISTRICT VARCHAR2(20)
POPULATION number

1.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 POPULATION>=100000 and COUNTRYCODE='USA';

2.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;

3.Query all columns (attributes) for every row in the CITY table.

select * from CITY;

4.Query all columns for a city in CITY with the ID 1661.

select * from CITY
where ID =1661;

5.Query all attributes of every Japanese city in the CITY table. The COUNTRYCODE for Japan is JPN.

select * from CITY
where COUNTRYCODE='JPN';

6.Query the names of all the Japanese cities in the CITY table. The COUNTRYCODE for Japan is JPN.

select NAME
from CITY
where COUNTRYCODE='JPN'

7.Query a count of the number of cities in CITY having a Population larger than 10,000.

select count(NAME)
from CITY
where POPULATION>100000;

8.Query the total population of all cities in CITY where District is California.

select sum(POPULATION)
from CITY
where DISTRICT ='California';

9.Query the average population of all cities in CITY where District is California.

select avg(POPULATION)
from CITY
where DISTRICT='California';

10.Query the average population for all cities in CITY, rounded down to the nearest integer.

select ROUND(avg(POPULATION),0)
from CITY;

11.Query the sum of the populations for all Japanese cities in CITY. The COUNTRYCODE for Japan is JPN.

select sum(POPULATION)
from CITY
where COUNTRYCODE='JPN';

12.Query the difference between the maximum and minimum populations in CITY.

select max(POPULATION)-min(POPULATION)
from CITY;

13.Given the CITY and COUNTRY tables, query the sum of the populations of all cities where the CONTINENT is 'Asia'.

Note: CITY.CountryCode and COUNTRY.Code are matching key columns.

select sum(c.POPULATION) from CITY c
left join COUNTRY c1
on c.COUNTRYCODE=c1.CODE
where c1.CONTINENT ='Asia';

14.Given the CITY and COUNTRY tables, query the names of all cities where the CONTINENT is 'Africa'.

Note: CITY.CountryCode and COUNTRY.Code are matching key columns.

select c.NAME
from CITY c
left join COUNTRY c1
on c.COUNTRYCODE=c1.CODE
where c1.CONTINENT='Africa';

15.Given the CITY and COUNTRY tables, query the names of all the continents (COUNTRY.Continent) and their respective average city populations (CITY.Population) rounded down to the nearest integer.

给定 CITY 和 COUNTRY 表,查询所有大洲的名称 (COUNTRY.Continent) 及其各自的平均城市人口 (CITY.Population),四舍五入到最接近的整数。

Note: CITY.CountryCode and COUNTRY.Code are matching key columns.

select c.Continent,FLOOR(avg(c1.population))
from COUNTRY c
join CITY c1
on c.CODE=c1.CountryCode
group by 1;

STATION表:

Field Type
ID number
CITY VARCHAR2(21)
STATE VARCHAR2(2)
LAT_N number
LONG_W number

1.Query a list of CITY and STATE from the STATION table.

select CITY,STATE
from STATION;

2.Query a list of CITY names from STATION for cities that have an even ID number. Print the results in any order, but exclude duplicates from the answer.

从 STATION 查询具有偶数 ID 号的城市的 CITY 名称列表。 以任意顺序打印结果,但从答案中排除重复项。

select DISTINCT(CITY)
from STATION
where mod(ID,2)=0;

3.Find the difference between the total number of CITY entries in the table and the number of distinct CITY entries in the table.

求表中 CITY 条目总数与表中不同 CITY 条目数之间的差值。

select COUNT(CITY)-COUNT(DISTINCT(CITY))
from STATION;

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.

用最短和最长的 CITY 名称查询 STATION 中的两个城市,以及它们各自的长度(即:名称中的字符数)。 如果有多个最小或最大的城市,请选择按字母顺序排列的第一个城市。

方法1: where子句+limit

select CITY,length(CITY) from STATION
where length(CITY)>=ALL(select length(CITY) from STATION) or length(CITY)<=ALL(select length(CITY) from STATION)
order by 2 desc,1
limit 2;

解题思路:

方法2:union+limit

(select CITY,length(CITY) from STATION
order by 2 desc,1 desc
limit 1)
union
(select CITY,length(CITY) from STATION
order by 2,1
limit 1);

解题思路:两个表组合。

5.Query the list of CITY names starting with vowels (i.e., aeio, or u) from STATION. Your result cannot contain duplicates.

从 STATION 查询以元音开头的 CITY 名称列表(即 a.e.i.o 或 u)。 您的结果不能包含重复项。

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%';

6.Query the list of CITY names ending with vowels (a, e, i, o, u) from STATION. Your result cannot contain duplicates.

从 STATION 查询以元音结尾的 CITY 名称列表(即 a.e.i.o 或 u)。 您的结果不能包含重复项。

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';

7.Query the list of CITY names from STATION which have vowels (i.e., a, e, i, o, and u) as both their first and last characters. Your result cannot contain duplicates.

从 STATION 查询以元音开头和结尾的 CITY 名称列表(即 a.e.i.o 或 u)。 您的结果不能包含重复项。

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') and (CITY LIKE'a%' or CITY LIKE'e%' or CITY LIKE'i%' or CITY LIKE'o%' or CITY LIKE'u%');

8.Query the list of CITY names from STATION that do not start with vowels. Your result cannot contain duplicates.

从 STATION 查询不能以元音开头的 CITY 名称列表(即 a.e.i.o 或 u)。 您的结果不能包含重复项。

select DISTINCT(CITY)
from STATION
where CITY NOT LIKE 'a%' and CITY NOT LIKE 'e%' and CITY NOT LIKE 'i%' and CITY NOT LIKE 'o%' and CITY NOT LIKE 'u%';

9.Query the list of CITY names from STATION that do not end with vowels. Your result cannot contain duplicates.

从 STATION 查询不能以元音结尾的 CITY 名称列表(即 a.e.i.o 或 u)。 您的结果不能包含重复项。

select DISTINCT(CITY)
from STATION
where CITY NOT LIKE '%a' and CITY NOT LIKE '%e' and CITY NOT LIKE '%i' and CITY NOT LIKE '%o' and CITY NOT LIKE '%u';

10.Query the list of CITY names from STATION that either do not start with vowels or do not end with vowels. Your result cannot contain duplicates.

从 STATION 查询不以元音开头或不以元音结尾的 CITY 名称列表。 您的结果不能包含重复项。

select DISTINCT(CITY)
from STATION
where (CITY NOT LIKE 'a%' and CITY NOT LIKE 'e%' and CITY NOT LIKE 'i%' and CITY NOT LIKE 'o%' and CITY NOT LIKE 'u%') or (CITY NOT LIKE '%a' and CITY NOT LIKE '%e' and CITY NOT LIKE '%i' and CITY NOT LIKE '%o' and CITY NOT LIKE '%u');

11.Query the list of CITY names from STATION that do not start with vowels and do not end with vowels. Your result cannot contain duplicates.

从 STATION 查询不以元音开头和不以元音结尾的 CITY 名称列表。 您的结果不能包含重复项。

select DISTINCT(CITY)
from STATION
where (CITY NOT LIKE 'a%' and CITY NOT LIKE 'e%' and CITY NOT LIKE 'i%' and CITY NOT LIKE 'o%' and CITY NOT LIKE 'u%') and (CITY NOT LIKE '%a' and CITY NOT LIKE '%e' and CITY NOT LIKE '%i' and CITY NOT LIKE '%o' and CITY NOT LIKE '%u');

12.Query the following two values from the STATION table:

The sum of all values in LAT_N rounded to a scale of 2 decimal places.
The sum of all values in LONG_W rounded to a scale of 2 decimal places.

select ROUND(sum(LAT_N),2),ROUND(sum(LONG_W),2)
from STATION;

13.Query the sum of Northern Latitudes (LAT_N) from STATION having values greater than 38.7880  and less than 137.2345. Truncate your answer to 4 decimal places.

select ROUND(sum(LAT_N),4)
from STATION
where LAT_N>38.7880 and LAT_N<137.2345;

14.Query the greatest value of the Northern Latitudes (LAT_N) from STATION that is less than137.2345 . Truncate your answer to 4 decimal places.

select ROUND(max(LAT_N),4)
from STATION
where LAT_N<137.2345;

15.Query the Western Longitude (LONG_W) for the largest Northern Latitude (LAT_N) in STATION that is less than 137.3245. Round your answer to 4 decimal places.

select ROUND(LONG_W,4)
from STATION
where LAT_N<137.2345
order by LAT_N desc
limit 1;

16.Query the smallest Northern Latitude (LAT_N) from STATION that is greater than 38.7880. Round your answer to 4 decimal places.

select ROUND(LAT_N,4)
from STATION
where LAT_N>38.7880
order by 1
limit 1;

17.

Query the Western Longitude (LONG_W)where the smallest Northern Latitude (LAT_N) in STATION is greater than 38.7880. Round your answer to  decimal places.

select ROUND(LONG_W,4)
from STATION
where LAT_N>38.7880
order by LAT_N
limit 1;

18.Weather Observation Station 18

Consider p1(a,b)  and p2(c,d) to be two points on a 2D plane.

  • a happens to equal the minimum value in Northern Latitude (LAT_N in STATION).
  • b happens to equal the minimum value in Western Longitude (LONG_W in STATION).
  • c happens to equal the maximum value in Northern Latitude (LAT_N in STATION).
  • d happens to equal the maximum value in Western Longitude (LONG_W in STATION).

Query the Manhattan Distance between points  and  and round it to a scale of 4 decimal places.

select ROUND(max(LAT_N)-min(LAT_N)+max(LONG_W)-min(LONG_W),4)
from STATION;

19.Weather Observation Station 19

Consider p1 and p2 to be two points on a 2D plane where a,c are the respective minimum and maximum values of Northern Latitude (LAT_N) and b,d are the respective minimum and maximum values of Western Longitude (LONG_W) in STATION.

将 p1 和 p2 视为 2D 平面上的两个点,其中 a,c 是北纬 (LAT_N) 的相应最小值和最大值,b,d 是 STATION 中西经 (LONG_W) 的相应最小值和最大值。

Query the following two values from the STATION table:

1.The sum of all values in LAT_N rounded to a scale of 2 decimal places.

2.The sum of all values in LONG_W rounded to a scale of 2 decimal places.

select ROUND(sum(LAT_N),2),ROUND(sum(LONG_W),2)
from STATION;

19.

STUDENTS表:

Field Type
ID INTEGER
Name String
Maks INTEGER

1.Query the Name of any student in STUDENTS who scored higher than  Marks. Order your output by the last three characters of each name. If two or more students both have names ending in the same last three characters (i.e.: Bobby, Robby, etc.), secondary sort them by ascending ID.

查询 STUDENTS 中任何得分高于 75 的学生的姓名。 按每个名称的最后三个字符对输出进行排序。 如果两个或多个学生的名字都以相同的最后三个字符结尾(即:Bobby.Robby 等),则按 ID 升序对他们进行二次排序。

select Name from STUDENTS
where Marks>75
order by right(Name,3),ID;

解题思路,按照每个名字的最后三个字符排序,order by 和right函数结合使用。

Employee表:

Field Type
employee_id INTEGER
name String
months INTEGER
salary INTEGER

1.Write a query that prints a list of employee names (i.e.: the name attribute) from the Employee table in alphabetical order.

select name
from Employee
order by name;

2.Write a query that prints a list of employee names (i.e.: the name attribute) for employees in Employee having a salary greater than  per month who have been employees for less than  months. Sort your result by ascending employee_id.

编写一个查询,打印员工姓名列表(即:名称属性),用于 Employee 中薪水大于每月且工作时间少于几个月的员工。 按employee_id 升序对结果进行排序。

select name
from Employee
where salary>2000 and months<10
order by employee_id;

3.We define an employee's total earnings to be their monthly salary*months  worked, and the maximum total earnings to be the maximum total earnings for any employee in the Employee table. Write a query to find the maximum total earnings for all employees as well as the total number of employees who have maximum total earnings. Then print these values as  2 space-separated integers.

我们将员工的总收入定义为他们的月薪*工作月数,最大总收入定义为 Employee 表中任何员工的最大总收入。 编写查询以查找所有员工的最大总收入以及拥有最大总收入的员工总数。 然后将这些值打印为 2 个空格分隔的整数。

select salary*months as earning,count(name)
from Employee
group by earning
order by earning desc
limit 1;

4.

------------恢复内容结束------------

HackerRank第一趴--Basic Select的更多相关文章

  1. SQL Fundamentals: Basic SELECT statement基本的select语句(控制操作的现实列)(FROM-SELECT)

    SQL Fundamentals || Oracle SQL语言 Capabilities of the SELECT Statement(SELECT语句的功能) Data retrieval fr ...

  2. 牛客SQL刷题第一趴——非技术入门基础篇

    user_profile表: id device_id gender age university province 1 2138 male 21 北京大学 Beijing 2 3214 male   ...

  3. PHP基础知识第一趴

    今天来贴一贴我的一张部分php基础知识的思维导图.未完,待续......慢慢'补枪'(为了让引号内的期望输出内容<strong>变成</strong>现实,应该使用双引号?那就 ...

  4. oracle(sql)基础篇系列(一)——基础select语句、常用sql函数、组函数、分组函数

        花点时间整理下sql基础,温故而知新.文章的demo来自oracle自带的dept,emp,salgrade三张表.解锁scott用户,使用scott用户登录就可以看到自带的表. #使用ora ...

  5. 使用SQL语句查询日期(当月天数,当月第一天,当月最后一天,本年最后一天,当月第一个星期) 日期转字符串

    取某月天数:,) --当月天数 ,DATEADD(m, DATEDIFF(m,,getdate())+,))) ---当月第一天 ,getdate()) ---当月最后一天 ,dateadd(m,,d ...

  6. SQL查询一个月第一天/最后一天及日期格式化

    1.一个月第一天的Select DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) 2.本周的星期一Select DATEADD(wk, DATEDIFF(wk,0,ge ...

  7. SQLSERVER取当前月第一天和最后一天

    --本月第一天: select   dateadd(dd,-day(getdate())+1,getdate()) --本月最后一天: SELECT dateadd(ms,-3,DATEADD(mm, ...

  8. select中无法使用click的处理

    今天工作用到了select,想要给option添加click点击事件,可是却没有任何效果,百度了才发现,原来竟然是不支持呀! 众所周知(其实我才知道哎),在IE里, select的option是不支持 ...

  9. java 和 mysql 获取周 星期 的第一天 最后一天 或者 月的 日期(字符串转日期,日期转字符串,日期加减)

    获取周的第一天,最后一天 System.out.println(getStartEndDate("2016-05-01", 1)); 获取星期的第一天和最后一天 System.ou ...

随机推荐

  1. ucore lab5 用户进程管理 学习笔记

    近几日睡眠质量不佳,脑袋一困就没法干活,今天总算时补完了.LAB5难度比LAB4要高,想要理解所有细节时比较困难.但毕竟咱不是要真去写一个OS,所以一些个实现细节就当成黑箱略过了. 这节加上了用户进程 ...

  2. C++基础-4-封装(构造函数与析构函数,深拷贝与浅拷贝,静态成员,this,友元,const修饰成员函数)

    4. 封装 4.1.1 封装的意义 1 #include<iostream> 2 #include<string> 3 using namespace std; 4 5 con ...

  3. 一文看懂:网址,URL,域名,IP地址,DNS,域名解析

    一个执着于技术的公众号 前言 今天给大家梳理一篇关于网址.URL.IP地址.域名.DNS.域名解析的白话长文,并以简单的提问-解答 形式让读者更加深刻理解,希望有助于读者的学习,面试和工作! 1.一个 ...

  4. 离谱的 CSS!从表盘刻度到艺术剪纸

    某日,群里有这样一个问题,如何实现这样的表盘刻度: 这其实是个挺有意思的问题,方法也有很多. 单标签,使用 conic-gradient 实现表盘刻度 最简单便捷的方式,就是利用角向渐变的方式 con ...

  5. CentOS7安装部署Mongodb

    1.下载安装包 打开官网,跳转至下载界面,选择对应版本的安装包,拷贝其链接,这里是手动安装,所以下载tgz安装包,如果要自动化安装,选择server的rpm自动安装包 https://www.mong ...

  6. 1903021121—刘明伟—Java第四周作业—java分支语句学习

    项目 内容 课程班级博客链接 19信计班(本) 作业要求链接 第四周作业 要求 每道题要有题目,代码(使用插入代码,不会插入代码的自己查资料解决,不要直接截图代码!!),截图(只截运行结果). 扩展阅 ...

  7. 五、C++运算符重载,使面向对象编程更方便

    复数类CComplex 编译器做对象运算的时候,会调用对象的运算符重载函数(优先调用成员方法):如果没有成员方法,就砸全局作用域找合适的运算符重载函数 ++和--运算符是单目运算符,在参数列表里放上一 ...

  8. windows 存储和切换 ip 配置

    我的虚拟机用的是桥接模式,在公司使用时设置的是静态 ip,但网段和家里面的不一样,就导致在公司和家里,我需要频繁修改 ipv4 的配置以适应不同的网络环境 Simple-IP-Config 工具解决了 ...

  9. 性能测试:tcpcopy

    简介 TCPCopy是一种请求复制(所有基于tcp的packets)工具,可以把在线流量导入到测试系统中去. 曾经应用于网易的广告投放系统,urs系统,nginx hmux协议等系统,避免了上线带来的 ...

  10. MySQL执行计划explain

    一.简介 分析查询慢的原因,在查询语句前加explain即可.如: 二.输出格式 2.0 测试数据 # 表user_info CREATE TABLE `user_info` ( `id` bigin ...