高频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;
相关推荐
sone121382 小时前
Oracle 12c实验3:实验步骤的SQL语句
数据库·sql·oracle
程序员卷卷狗3 小时前
为什么MySQL默认使用可重复读RR?深入解析binlog与隔离级别的关系
数据库·mysql
此生只爱蛋3 小时前
【Redis】String 字符串
java·数据库·redis
瀚高PG实验室3 小时前
拼接符“II”在Oracle和HGDB中使用的差异
数据库·oracle·瀚高数据库
心态还需努力呀3 小时前
当时序数据不再“只是时间”:金仓数据库如何在复杂场景中拉开与 InfluxDB 的差距
数据库
宇灬宇3 小时前
oracle误drop表,通过回收站恢复
数据库·oracle
Albert Tan3 小时前
Oracle EBS 12.2/12.1 开放本地或远程访问Weblogic
数据库·oracle
一个处女座的程序猿O(∩_∩)O3 小时前
从InfluxDB到金仓:时序数据库性能拐点已至?
数据库·时序数据库
数据和云3 小时前
Oracle没有退路
数据库·oracle·vr