高频SQL50题(基础版)解析(I)

题单:高频 SQL 50 题(基础版) - 学习计划 - 力扣(LeetCode)全球极客挚爱的技术成长平台

查询:

1757. 可回收且低脂的产品

'且'条件查询:

sql 复制代码
select product_id from Products where low_fats='Y' and recyclable='Y';

584. 寻找用户推荐人

空值判定:

sql 复制代码
SELECT name FROM Customer where  referee_id !=2 OR referee_id is NULL;

595. 大的国家

'或'条件查询

sql 复制代码
select name,population,area from World where area>=3000000 or population>=25000000;

1148. 文章浏览 I - 力扣(LeetCode)

查询视图列别名、排序

sql 复制代码
select distinct(author_id) as 'id' from Views where author_id=viewer_id order by author_id asc;

1683. 无效的推文 - 力扣(LeetCode)

字段长度

sql 复制代码
select tweet_id from tweets where length(content)>15;

连接:

1378. 使用唯一标识码替换员工ID

两表左值连接:

sql 复制代码
select unique_id,name from employees left join employeeUNI on employees.id=employeeUNI.id;

1068. 产品销售分析 I

两表内连接(两表字段组合):

此题也可用左值连接,因为两表存在外键约束,不会出现null

sql 复制代码
select Product.product_name, Sales.year, Sales.price
fromSales
join Product
on Sales.product_id = Product.product_id;

⭐1581. 进店却未进行过交易的顾客

两表左值连接后在子视图中查询,并根据customer_id分组,最后计数

sql 复制代码
select customer_id,count(customer_id) as count_no_trans
from Visits
left join transactions using(visit_id)
where transactions_id is null
group by customer_id;

197. 上升的温度

单表内连接(笛卡尔积)

sql 复制代码
select a.id
from weather as a
inner join weather as b
on datediff(a.recordDate, b.recordDate) = 1
where a.Temperature > b.Temperature;

1661. 每台机器的进程平均运行时间

单表内连接、聚合函数

1.先内连接找到同一组机器的同一个进程,并按机器分组聚合

2.然后使用聚合函数avg求每组的平均数,并起别名

sql 复制代码
select t1.machine_id,round(avg(t2.timestamp-t1.timestamp),3) as processing_time 
from activity as t1,activity as t2
where t1.machine_id=t2.machine_id
      and t1.process_id=t2.process_id
      and t1.activity_type='start'
      and t2.activity_type='end'
group by t1.machine_id

577. 员工奖金

两表左外连接(需要左表全部数据)

复制代码
select Employee.name,Bonus.bonus from
Employee left join Bonus using(empId)
where Bonus.bonus<1000 or Bonus.bonus is null;

1280. 学生们参加各科测试的次数

三表笛卡尔积、左外连接、聚合函数

注意:非聚合列(出现在查询语句中,但未被聚合函数处理)必须出现在group by语句中

sql 复制代码
select
    Students.student_id, 
    Students.student_name, 
    Subjects.subject_name,
    COUNT(Examinations.subject_name) as attended_exams 
from 
    Students cross join Subjects left join Examinations 
on 
    Students.student_id = Examinations.student_id 
    and Subjects.subject_name = Examinations.subject_name 
group by 
    Students.student_id, 
    Students.student_name, 
    Subjects.subject_name 
order by 
    Students.student_id, 
    Subjects.subject_name;

570. 至少有5名直接下属的经理

单表左值连接,使用HAVING在数据已经被分组后,对分组的结果进行筛选。

sql 复制代码
select a.name 
from
    Employee as a left join Employee as b
on 
    a.id=b.managerId
group by a.id
having count(b.id)>=5;

1934. 确认率

round函数,IFNULL函数,avg聚合函数。

sql 复制代码
# Write your MySQL query statement below
select 
    s.user_id, round(IFNULL(AVG(c.action='confirmed'), 0), 2) AS confirmation_rate
from 
    Signups as s left join Confirmations as c
on s.user_id=c.user_id
group by s.user_id;

620. 有趣的电影

mod聚合函数

sql 复制代码
select * from cinema
where description != 'boring' and mod(id,2)=1
order by rating desc;
相关推荐
琳少_2 分钟前
java程序开发笔记
java·开发语言·数据库·笔记
Pocker_Spades_A3 小时前
工业物联网时序数据库选型:一份能直接抄走的技术规格书
数据库·物联网·时序数据库
苜蓿muxu3 小时前
缓存三大问题:穿透、击穿、雪崩
数据库·redis
正在走向自律5 小时前
MySQL兼容性深度实践:建表、增删改查与迁移避坑全记录
数据库·sql·mysql·信创·数据库迁移
隔窗听雨眠5 小时前
openGauss开源五年的内核演进与生态工程
数据库·opengauss·国产数据库
Decay__6 小时前
PostgreSQL 体系架构全解 —— 进程模型、共享内存、MVCC 与 WAL 预写日志深入
运维·数据库·sql·mysql·架构
沐知全栈开发7 小时前
基于ClinicalTrials公开数据的临床试验分析系统-毕业设计
数据库·课程设计
半杯咖啡半行码7 小时前
Qt 隐式共享(Implicit Sharing)深度解析:机制、陷阱与 Qt6迁移
开发语言·数据库·qt
Nturmoils8 小时前
空值、日期、字符串:业务 SQL 里的函数先跑一遍
数据库
倔强的石头_11 小时前
数据库选型四步法:我把候选从十几个缩到两个
数据库