1-sql练习

1

Query the list of CITY names ending with vowels (a, e, i, o, u) from STATION. 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.

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';
SELECT DISTINCT CITY FROM STATION WHERE CITY RLIKE '[A,E,I,O,U]$'
类似
select distinct city from station where city regexp '[aeiou]$'

要点:ending with vowels (a, e, i, o, u) cannot* contain duplicates.

所以 DISTINCT 去重,结尾 ‘%x’

mysql rlike函数_MySQL SELECT LIKE like的用法 - CodeAntenna

2

Generate the following two result sets:

  1. 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), and ASingerName(S).

  2. Query the number of ocurrences of each occupation in OCCUPATIONS. Sort the occurrences in ascending order, and output them in the following format:

    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.

Note: There will be at least two entries in the table for each type of occupation.

Input Format

The OCCUPATIONS table is described as follows: img Occupation will only contain one of the following values: Doctor, Professor, Singer or Actor.

Sample Input

An OCCUPATIONS table that contains the following records:

img

Sample Output

Ashely(P)
Christeen(P)
Jane(A)
Jenny(D)
Julia(A)
Ketty(P)
Maria(A)
Meera(S)
Priya(S)
Samantha(D)
There are a total of 2 doctors.
There are a total of 2 singers.
There are a total of 3 actors.
There are a total of 3 professors.
select concat(name,'(',case when occupation like '%Doctor%' then 'D' when occupation like '%Actor%' then 'A' when occupation like '%Singer%' then 'S' when occupation like '%Professor%' then 'P' else '' end,')' ) from occupations order by name;


select concat('There are a total of ', count(occupation),' ',lower(Occupation),'s.') as result from occupations group by Occupation order by result

要点 concat ,case when ,lower ,group by

(39条消息) sql concat()函数_crazyK.的博客-CSDN博客

(39条消息) sql语句之case when的用法_学习要疯狂的博客-CSDN博客

(39条消息) SQL中Group By的使用_sql group by_泡泡茶壶Wending的博客-CSDN博客

3

旋转职业中的职业列,以便每个名称按字母顺序排序并显示在其相应的职业下方。输出列标题应分别为博士教授歌手演员

注意:当没有更多与职业对应的名称时打印NULL。

输入格式

职业表描述如下:

img

职业只能包含以下值之一:医生、教授、歌手或演员。

样本输入

img

样本输出

Jenny    Ashley     Meera  Jane
Samantha Christeen  Priya  Julia
NULL     Ketty      NULL   Maria

说明

第一列是按字母顺序排列的医生姓名列表。
第二列是按字母顺序排列的教授名单。
第三列是按字母顺序排列的歌手姓名列表。
第四列是按字母顺序排列的Actor名称列表。
每个职业名称数小于最大值的列(在本例中为教授列和演员列)的空单元格数据填充为NULL值。

SELECT
    MAX(CASE WHEN OCCUPATION = 'Doctor' THEN NAME END) AS Doctor,
    MAX(CASE WHEN OCCUPATION = 'Professor' THEN NAME END) AS Professor,
    MAX(CASE WHEN OCCUPATION = 'Singer' THEN NAME END) AS Singer,
    MAX(CASE WHEN OCCUPATION = 'Actor' THEN NAME END) AS Actor
FROM (
  SELECT
    OCCUPATION,
    NAME,
    (@rn := CASE WHEN @prev_occupation = OCCUPATION THEN @rn + 1 ELSE 1 END) AS rn,
    @prev_occupation := OCCUPATION
  FROM OCCUPATIONS, (SELECT @rn := 0, @prev_occupation := '') AS r
  ORDER BY OCCUPATION, NAME
) AS subquery
GROUP BY rn;

SELECT 
	MAX(CASE WHEN Occupation = 'Doctor' THEN Name END) AS Doctor, 
	MAX(CASE WHEN Occupation = 'Professor' THEN Name END) AS Professor, 
	MAX(CASE WHEN Occupation = 'Singer' THEN Name END) AS Singer, 
	MAX(CASE WHEN Occupation = 'Actor' THEN Name END) AS Actor 
FROM ( 
    SELECT Name, Occupation, ROW_NUMBER() OVER (PARTITION BY Occupation ORDER BY Name) AS rn FROM OCCUPATIONS ) AS pivot_table 	
GROUP BY rn;

要点 行转列 (39条消息) SQL 行转列 列转行 case when 写法_case when 行转列_每天一积累的博客-CSDN博客

ROW_NUMBER(), (39条消息) SQL Server中row_number函数用法介绍_sql的rownumber_格子衫111的博客-CSDN博客

最内层的SELECT语句用于从OCCUPATIONS表中获取姓名,职业和按职业和姓名排序的行号。
ROW_NUMBER()函数用于为每条记录在每个职业中分配唯一的行号。
最外层的SELECT语句使用CASE语句将记录按行号分组,并返回每组(即每个职业)中的最大姓名。
AS关键字用于给输出列提供描述性名称(Doctor,Professor,Singer,Actor)。

4

Amber的企业集团刚刚收购了一些新公司。每家公司都遵循以下等级制度:img

给定下面的表模式,编写一个查询来打印company_code、创始人姓名、首席经理总数、高级经理总数、经理总数和员工总数。按company_code升序排列输出。

注:

  • 这些表可能包含重复的记录。
  • company_code是字符串,因此排序不应该是数字。例如,如果company_codesC_1、C_2和C_10,则升序company_codes将C_1、C_10和C_2。

输入格式

The following tables contain company data:

  • Company: The company_code is the code of the company and founder is the founder of the company. img
  • Lead_Manager:lead_manager_code是首席经理的代码,而company_code是工作公司的代码。img
  • Senior_Manager:senior_manager_code是高级经理的代码,是lead_manager_code是其首席经理的代码,而company_code是工作公司的代码。img
  • 经理:manager_code是经理的代码,是senior_manager_code是其高级经理的代码lead_manager_code是其首席经理的代码,而company_code是工作公司的代码。img
  • 员工:employee_code是员工的代码,是manager_code是其经理的代码,即senior_manager_code是其高级经理的代码lead_manager_code是其首席经理的代码,而company_code是工作公司的代码。img

样本输入

公司表:img Lead_Manager表:img Senior_Manager表:img 经理表:img 员工表:img

样本输出

C1 Monika 1 2 1 2
C2 Samantha 1 1 2 2

说明

在C1公司,唯一的领导经理是LM1。LM1下有两名高级经理,SM1和SM2。高级经理SM1下有一名经理,M1下有两名员工,E1和E2。

在C2公司,唯一的领导经理是LM2。LM2下有一名高级经理,SM3。高级经理SM3下有两名经理,M2和M3。经理M2下有一名员工,E3,经理M3下有另一名员工,E4。

/*
Enter your query here.
*/



Select C.Company_code, founder, Count(Distinct lead_manager_code), Count(Distinct Senior_manager_code), Count(Distinct manager_code), Count(Distinct employee_code)from Employee E Join Company C on E.Company_code=C.Company_code Group by Company_code, founder Order by Company_code asc;


select c.company_code,
    founder,
    count(distinct l.lead_manager_code),
    count(distinct s.senior_manager_code),
    count(distinct m.manager_code),
    count(distinct e.employee_code)
from company c,
    lead_manager l,
    senior_manager s,
    manager m,
    employee e
where c.company_code = l.company_code
    and l.lead_manager_code = s.lead_manager_code 
    and s.senior_manager_code = m.senior_manager_code
    and m.manager_code = e.manager_code
group by c.company_code, founder
order by company_code;



SELECT C.company_code, C.founder,
COUNT(DISTINCT LM.lead_manager_code) AS lead_manager_count, COUNT(DISTINCT SM.senior_manager_code) AS senior_manager_count, COUNT(DISTINCT M.manager_code) AS manager_count, 
COUNT(DISTINCT E.employee_code) AS employee_count 
FROM Company C 
LEFT JOIN Lead_Manager LM ON C.company_code = LM.company_code 
LEFT JOIN Senior_Manager SM ON LM.lead_manager_code = SM.lead_manager_code 
LEFT JOIN Manager M ON SM.senior_manager_code = M.senior_manager_code 
LEFT JOIN Employee E ON M.manager_code = E.manager_code 
GROUP BY
C.company_code, C.founder ORDER BY C.company_code

要点 join (39条消息) 一文让你彻底理解SQL中的join_sql join_独影月下酌酒的博客-CSDN博客

GROUP BY 语句用于对查询结果进行分组,将具有相同值的行归为一组。在以下情况下,你需要使用 GROUP BY 语句:

当你需要对查询结果按照某个列进行分组时,比如按照部门、产品类别、地区等对数据进行汇总。
当你需要对查询结果计算聚合函数时,比如求和、平均值、最大值、最小值等,你需要使用 GROUP by 语句将结果分组。
当你需要根据分组结果进行排序时,你可以使用 ORDER BY 语句对分组结果进行排序。
需要注意的是, GROUP by 语句必须与聚合函数一起使用,否则无法发挥功效。此外,使用 GROUP by 语句时,你需要确保所有的非聚合列都出现在 GROUP BY 子句中,或者在 SELECT 语句中使用聚合函数。

5

给定一个表BST,其中包含两列:N和P,其中N表示二叉树中节点的值,P是N的父级。

img

编写查询以查找按节点值排序的二叉树的节点类型。为每个节点输出以下内容之一:

  • 根:如果节点是根节点。
  • 叶:如果节点是叶节点。
  • 内部:如果节点既不是根节点也不是叶节点。

样本输入

img

Sample Output

1 Leaf
2 Inner
3 Leaf
5 Root
6 Leaf
8 Inner
9 Leaf

说明

下面的二叉树说明了示例:

img

/*
Enter your query here.
*/

SELECT B.N, 
CASE 
    WHEN B.P IS NULL THEN 'Root' 
    WHEN(SELECT COUNT(*) FROM BST B2 WHERE B2.P = B.N) > 0 THEN 'Inner'
    ELSE 'Leaf'
END
FROM BST B
ORDER BY N;

要点case when 里面能写select

9 Leaf



**说明**

下面的二叉树说明了示例:

[外链图片转存中...(img-zi0rgvvA-1690000853052)]





```sql
/*
Enter your query here.
*/

SELECT B.N, 
CASE 
    WHEN B.P IS NULL THEN 'Root' 
    WHEN(SELECT COUNT(*) FROM BST B2 WHERE B2.P = B.N) > 0 THEN 'Inner'
    ELSE 'Leaf'
END
FROM BST B
ORDER BY N;

要点case when 里面能写select

COUNT() 是一个聚合函数,用于计算查询结果集中行的数量。它的作用是返回指定列中非空的行数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值