NO Easy
The PADS
题目
Generate the following two result sets:
-
Query an alphabetically ordered list of all names in OCCUPATIONS, immediately followed by the first letter of each profession as a parenthetical (i.e.: enclosed in parentheses). For example:
AnActorName(A)
,ADoctorName(D)
,AProfessorName(P)
, andASingerName(S)
. -
Query the number of ocurrences of each occupation in OCCUPATIONS. Sort the occurrences in ascending order, and output them in the following format:
1
There are a total of [occupation_count] [occupation]s.
where
[occupation_count]
is the number of occurrences of an occupation in OCCUPATIONS and[occupation]
is the lowercase occupation name. If more than one Occupation has the same[occupation_count]
, they should be ordered alphabetically.
解题
1 | -- in OCCUPATIONS |
- 考察点:字符串拼接 & 分组 + 聚合 + 字符串拼接
- 注意点:不要使用 UNION / UNION ALL ,因为排序,不要问我为什么知道
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 P1 and P2 and round it to a scale of decimal places.
解题
1 | -- Query the Manhattan Distance between points P1 and P2 |
- 考察点:ABS 函数 + ROUND 函数
Binary Tree Nodes
题目
You are given a table, BST, containing two columns: N and P, where N represents the value of a node in Binary Tree, and P is the parent of N.
Write a query to find the node type of Binary Tree ordered by the value of the node. Output one of the following for each node:
- Root: If node is root node.
- Leaf: If node is leaf node.
- Inner: If node is neither root nor leaf node.
解题
1 | -- where N represents the value of a node in Binary Tree, and P is the parent of N. |
- 考察点:CASE WHEN + 逻辑判断
- Root 节点:没有父节点
- Inner 节点:既有父节点也有子节点
- Leaf 节点:ELSE / 没有子节点的
- CONCAT 基于题目要求 OUTPUT 一列还是两列
New Companies
题目
Amber’s conglomerate corporation just acquired some new companies. Each of the companies follows this hierarchy:Founder -> Lead Manager -> Senior Manager -> Manager -> Employee
Given the table schemas below, write a query to print the company_code, founder name, total number of lead managers, total number of senior managers, total number of managers, and total number of employees. Order your output by ascending company_code.
Note:
- The tables may contain duplicate records.
- The company_code is string, so the sorting should not be numeric. For example, if the company_codes are C_1, C_2, and C_10, then the ascending company_codes will be C_1, C_10, and C_2.
解题
1 | -- Founder -> Lead Manager -> Senior Manager -> Manager -> Employee |
- 考察点:多表联结 + 逻辑
- 问题一:为什么要五张表都联结,而不是直接Company联结Employee?
- 如果存在 Lead_Manager手下没有Senior_Manager,或者Senior_Manager没有Manager,所以还是对数据的准确性的判断,当然如果能确保每一级都存在下一级的数据,且保证数据收录在表中是可以尝试两表
- 拓展:排序
- 题中表明排序字段为字符串,所以排序顺序为C_1, C_10, and C_2,如果需要C_1, C_2, and C_10 ,思路为:截取 C_ 右边的字符,并转换为数字类型,再排序
*Weather Observation Station 20
题目
A median is defined as a number separating the higher half of a data set from the lower half. Query the median of the Northern Latitudes (LAT_N) from STATION and round your answer to decimal places.
解题
1 | -- from STATION |
- 考察点:中位数概念 & 子查询 + ROUND + COUNT
- Oracle 有 MEDIAN 函数
- 中位数概念:指将数据按大小顺序排列起来,形成一个数列,居于数列中间位置的那个数据
- 数据行数为奇数:排序后,该列位于(行数+1)/2
- 数据行数为偶数:排序后,行数/2和行数/2+1两数的平均值
- 中位数概念:按顺序排列的一组数据中居于中间位置的数,代表一个样本、种群或概率分布中的一个数值,其可将数值集合划分为相等的上下两部分
The Report
题目
Ketty gives Eve a task to generate a report containing three columns: Name, Grade and Mark. Ketty doesn’t want the NAMES of those students who received a grade lower than 8. The report must be in descending order by grade – i.e. higher grades are entered first. If there is more than one student with the same grade (8-10) assigned to them, order those particular students by their name alphabetically. Finally, if the grade is lower than 8, use “NULL” as their name and list them by their grades in descending order. If there is more than one student with the same grade (1-7) assigned to them, order those particular students by their marks in ascending order.
Write a query to help Eve.
Students:
Grades:
解题
1 | -- report containing three columns: Name, Grade and Mark |
- 考察点:IF 函数 & 表联结 + 排序逻辑
- 判断 grade < 8 ,可以使用 CASE WHEN
Top Competitors
题目
Julia just finished conducting a coding contest, and she needs your help assembling the leaderboard! Write a query to print the respective hacker_id and name of hackers who achieved full scores for more than one challenge. Order your output in descending order by the total number of challenges in which the hacker earned a full score. If more than one hacker received full scores in same number of challenges, then sort them by ascending hacker_id.
解题
1 | -- print the respective hacker_id and name of hackers |
- 考察点:表联结 & 分组聚合
- 多注意题目限制:who achieved full scores for more than one challenge
Ollivander’s Inventory
题目
Harry Potter and his friends are at Ollivander’s with Ron, finally replacing Charlie’s old broken wand.
Hermione decides the best way to choose is by determining the minimum number of gold galleons needed to buy each non-evil wand of high power and age. Write a query to print the id, age, coins_needed, and power of the wands that Ron’s interested in, sorted in order of descending power. If more than one wand has same power, sort the result in order of descending age.
解题
1 | -- choose is by determining the minimum number of gold galleons |
-
考察点:GROUP BY & 多表联结
-
重点还是读题
Hermione decides the best way to choose is by determining the minimum number of gold galleons needed to buy each non-evil wand of high power and age.
表达的意思是:以年龄和威力进行分组,找到最少购买金币,且邪恶值为0
-
Challenges
题目
Julia asked her students to create some coding challenges. Write a query to print the hacker_id, name, and the total number of challenges created by each student. Sort your results by the total number of challenges in descending order. If more than one student created the same number of challenges, then sort the result by hacker_id. If more than one student created the same number of challenges and the count is less than the maximum number of challenges created, then exclude those students from the result.
解题
1 | -- print the hacker_id, name, and the total number of challenges created by each student. |
-
考察点:HAVING & 子查询
-
逻辑分解
If more than one student created the same number of challenges and the count is less than the maximum number of challenges created, then exclude those students from the result.
原义为:如果多个学生创建了相同数量的挑战并且计数小于创建的最大挑战数,则将这些学生排除在结果之外。
等同于:创建的最大挑战数的所有学生id + 创建挑战数的数量为 1 的学生 id
-