HackerRank第一趴--Basic Select

来源:https://www.cnblogs.com/ruoli-121288/archive/2022/06/02/16335207.html
-Advertisement-
Play Games

CITY表: Field Type ID number NAME VARCHAR2(17) COUNTRYCODE VARCHAR2(3) DISTRICT VARCHAR2(20) POPULATION number 1.Query all columns for all American cit ...


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.

 

 

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.

 

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.

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 鏡像下載、功能變數名稱解析、時間同步請點擊 阿裡雲開源鏡像站 1 簡介 FCN-4是一個應用於音頻自動標註的全捲積神經網路。使用該網路完成音頻標註任務時,首先需要使用python的音頻處理工具包Librosa提取音頻的時頻特征,針對mp3格式的音頻文件,Librosa讀取音頻文件的工作依賴音頻處理後端ffm ...
  • 本文例子參考《STM32單片機開發實例——基於Proteus虛擬模擬與HAL/LL庫》 源代碼:https://github.com/LanLinnet/STM33F103R6 項目要求 理解H橋電路的工作原理,結合前面幾個項目學習過的定時器中斷、EXTI、串口通訊等,要求通過7個按鈕控制步進電動機 ...
  • mysql -uroot -proot -h192.168.56.10 表示使用mysql的客戶端進行連接資料庫管理系統 -u後面是連接資料庫的用戶名,一般預設的情況下用戶名都是root -p後面是連接資料庫的密碼,在安裝mysql的時候自己設置的 -h表示資料庫管理系統所在的伺服器的ip地址,如果 ...
  • 一、 概述 compose 是用來定義和運行一個或多個容器(通常都是多個)運行和應用的工具。使用 compose 可以簡化容器鏡像的構建以及容器的運行。 compose 使用 YAML 文件來定義多容器之間的關係。一個 docker-compose up 就可以把完整的應用跑起來。 本質上,comp ...
  • job提交階段 1、準備好待處理文本。 2、客戶端submit()前,獲取待處理數據的信息,然後根據參數配置,形成一個任務分配的規劃。 3、客戶端向Yarn請求創建MrAppMaster並提交切片等相關信息:job.split、wc.jar、job.xml。Yarn調用ResourceManager ...
  • 資料庫安全,是指以保護資料庫系統、資料庫伺服器和資料庫中的數據、應用、存儲,以及相關網路連接為目的,防止資料庫系統及其數據遭到泄露、篡改或破壞的安全技術。 資料庫是企業最為核心的數據保護對象。與傳統的網路安全防護體系不同,資料庫安全技術更加註重從客戶內部的角度出發做安全,其安全要求包括了保密性、完整 ...
  • 作者:王志斌 編輯:鐘華龍 本文來自社區小伙伴 王志斌 的投稿。從小白的角度,帶你一步步實現將 RadonDB PostgreSQL 集群部署到 Kubernetes 上。文章分為上下兩部分,《第一部 Kubernetes 環境準備》已經發佈。第二部分將帶大家部署一個 RadonDB Postgre ...
  • 好消息,騰訊雲資料庫團隊智能調參CDBTune產品現已進入內測階段,歡迎資料庫愛好者、使用者、開發者前來測試。 CDBTune(cloud database tune)是基於2019至2021年間騰訊雲資料庫團隊連續發表兩篇頂級論文的研究成果,對雲資料庫進行調優的一整套解決方案,旨在充分藉助深度學習 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...