高频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;
相关推荐
筑梦之路6 小时前
harbor数据库报错权限异常如何处理——筑梦之路
数据库·harbor
czlczl200209257 小时前
理解 MySQL 行锁:两阶段锁协议与热点更新优化
数据库·mysql
AllData公司负责人7 小时前
通过Postgresql同步到Doris,全视角演示AllData数据中台核心功能效果,涵盖:数据入湖仓,数据同步,数据处理,数据服务,BI可视化驾驶舱
java·大数据·数据库·数据仓库·人工智能·python·postgresql
哆啦A梦15887 小时前
20, Springboot3+vue3实现前台轮播图和详情页的设计
javascript·数据库·spring boot·mybatis·vue3
渣渣盟8 小时前
Mysql入门到精通全集(SQL99)包含关系运算,软考数据库工程师复习首选
数据库·mysql·oracle
dishugj9 小时前
HANA 数据库的核心进程架构
数据库
2301_782040459 小时前
CSS Flex布局中如何实现导航栏与Logo的左右分布_利用justify-content- space-between
jvm·数据库·python
.柒宇.9 小时前
Redis主从复制集群搭建详解
数据库·redis·缓存·主从复制
2301_808414389 小时前
MySQL中的函数
数据库·mysql
Mahir089 小时前
MySQL 数据一致性的基石:三大日志( redo log/undo log/binlog)与两阶段提交(Prepare 阶段和Commit 阶段)深度解密
数据库·后端·mysql·面试