6-14题连接 - 高频 SQL 50 题基础版

目录

  • [1. 相关知识点](#1. 相关知识点)
  • [2. 例子](#2. 例子)
    • [2.6. 使用唯一标识码替换员工ID](#2.6. 使用唯一标识码替换员工ID)
    • [2.7- 产品销售分析 I](#2.7- 产品销售分析 I)
    • [2.8 - 进店却未进行过交易的顾客](#2.8 - 进店却未进行过交易的顾客)
    • [2.9 - 上升的温度](#2.9 - 上升的温度)
    • [2.10 - 每台机器的进程平均运行时间](#2.10 - 每台机器的进程平均运行时间)
    • [2.11- 员工奖金](#2.11- 员工奖金)
    • 2.12-学生们参加各科测试的次数
    • 2.13-至少有5名直接下属的经理
    • [2.14 - 确认率](#2.14 - 确认率)

1. 相关知识点

  • left join

    • 以左表为基准,返回左表中所有的行,同时返回右表中与左表匹配的行。
    • 如果右表中没有匹配的行,则用NULL填充。
  • join和left join的区别

    • 如果是join则右侧的数据有的就插,没的就啥也不干,交白卷,也不留null
    • 但是left join让右侧数据在没有对应数据时补上了null
  • CROSS JOIN产生了一个结果集,该结果集是两个关联表的行的乘积

    • 2行表,与3行表使用cross join,得到2*3=6行数据
  • 相关函数

    函数 例子 含义
    DATEDIFF(前,后) DATEDIFF('2007-12-31','2007-12-30'); # 1 两个日期的差,前-后
    sum() sum(salary) 根据分组求和
    if (判断条件,符合赋值,不符合赋值) if (salary>1000,1,0) 根据if条件语句取值
    sum(if( )) sum( if (salary>1000,1,0)) 根据if条件语句赋值再根据分组求和
    avg(if( )) avg( if (salary>1000,1,0)) 根据if条件语句赋值再根据分组求均值
    round(,n) round(salary,3) 保留n位小数

2. 例子

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



sql 复制代码
select 
    unique_id,name 
from 
	Employees e left join EmployeeUNI e1 
on e.id=e1.id;

2.7- 产品销售分析 I



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

2.8 - 进店却未进行过交易的顾客



sql 复制代码
-- 顾客可能光顾了购物中心但没有进行交易,一个顾客可能光顾多次,需用顾客id分组
-- 使用COUNT(*)可以输出GROUP BY后每个分组中的数据数量
-- 左连表,右表没有的数据赋值为null,即没有交易的transaction_id 为null

select 
    v.customer_id,count(*) as count_no_trans
from 
    Visits v left join Transactions t on v.visit_id=t.visit_id
where 
    t.transaction_id is null group by v.customer_id;

2.9 - 上升的温度


sql 复制代码
-- 找出与之前(昨天的)日期相比温度更高的所有日期的 id
-- DATEDIFF('2007-12-31','2007-12-30');   # 1
-- DATEDIFF('2010-12-30','2010-12-31');   # -1

select 
    w1.id 
from 
    Weather w1, Weather w2
where
    datediff(w1.recordDate,w2.recordDate)=1 and w1.temperature>w2.temperature;

2.10 - 每台机器的进程平均运行时间



sql 复制代码
-- sum(if(activity_type = 'end',timestamp ,-timestamp ))
-- 如果activity_type为"end",值为timestamp,为"start" 为-timestamp,所有数相加=end-start
-- count(distinct process_id),获取同一机器有几个进行id

select 
    machine_id , round(sum(if(activity_type = 'end',timestamp ,-timestamp ))/count(distinct process_id),3) as processing_time 
from 
    Activity 
group by 
    machine_id;


-- AVG(IF(activity_type = 'start', -timestamp, timestamp))
-- 如果activity_type为"end",值为timestamp,为"start" 为-timestamp,所有数相加=end-start
-- 将所有数求平均,avg(1,2,3,4)/4,多除了2倍

SELECT 
    machine_id, ROUND(AVG(IF(activity_type = 'start', -timestamp, timestamp))*2,3) AS processing_time 
FROM 
    Activity 
GROUP BY 
    machine_id;

2.11- 员工奖金



sql 复制代码
-- join和left join的区别
-- 如果是join则右侧的数据有的就插,没的就啥也不干,交白卷,也不留null
-- 但是left join让右侧数据在没有对应数据时补上了null
select 
    e.name,b.bonus
from 
    Employee e left join bonus b
on 
    e.empId=b.empId
where 
    b.bonus <1000 or b.bonus is null;

2.12-学生们参加各科测试的次数





sql 复制代码
-- 学生表中,id是唯一的,将他作为主表
--  CROSS JOIN产生了一个结果集,该结果集是两个关联表的行的乘积
-- 2行表,与3行表使用cross join,得到2*3=6行数据
select 
    st.student_id, st.student_name,su.subject_name,count(e.subject_name) AS attended_exams 
from 
    Students st 
cross join 
    Subjects su 
left join 
    Examinations e 
on 
    e.student_id=st.student_id and e.subject_name=su.subject_name
group by 
    st.student_id, st.student_name,su.subject_name 
order by 
    st.student_id,st.student_name;

2.13-至少有5名直接下属的经理


sql 复制代码
select 
    name
from 
    Employee 
where id in (
    select 
        managerId  -- 查找大于5的经理id
    from 
        Employee
    group by 
        managerId  -- 根据id分组
    having 
        count(*)>=5); -- 根据分组的数据进行求个数

2.14 - 确认率




sql 复制代码
-- s为注册表,有所有用户的信息,即为主表
select 
    s.user_id,round(sum(if(action="confirmed",1,0))/count(s.user_id),2) confirmation_rate 
from 
    Signups s 
left join 
    Confirmations c 
on 
    s.user_id =c.user_id 
group by 
    s.user_id;
相关推荐
jiayou6417 小时前
KingbaseES 实战:深度解析数据库对象访问权限管理
数据库
李广坤2 天前
MySQL 大表字段变更实践(改名 + 改类型 + 改长度)
数据库
爱可生开源社区3 天前
2026 年,优秀的 DBA 需要具备哪些素质?
数据库·人工智能·dba
随逸1773 天前
《从零搭建NestJS项目》
数据库·typescript
加号33 天前
windows系统下mysql多源数据库同步部署
数据库·windows·mysql
シ風箏3 天前
MySQL【部署 04】Docker部署 MySQL8.0.32 版本(网盘镜像及启动命令分享)
数据库·mysql·docker
李慕婉学姐3 天前
Springboot智慧社区系统设计与开发6n99s526(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·后端
百锦再3 天前
Django实现接口token检测的实现方案
数据库·python·django·sqlite·flask·fastapi·pip
tryCbest3 天前
数据库SQL学习
数据库·sql
jnrjian3 天前
ORA-01017 查找机器名 用户名 以及library cache lock 参数含义
数据库·oracle