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;
相关推荐
danielli44 分钟前
如何设计通用用户、权限、菜单数据表
数据库·oracle
客院载论1 小时前
秋招——MySQL补充——MySQL是如何加行级锁
数据库·mysql
~ 小团子1 小时前
MyBatis系列三: XxxMapper.xml-SQL映射文件
xml·sql·mybatis
UDDD-1 小时前
企业化运维(5)_mysql数据库
运维·数据库·mysql
chat2tomorrow1 小时前
oceanbase数据库安装和连接实战(阿里云服务器操作)
数据库·ide·mysql·阿里云·云计算·oceanbase·sqlynx
战神刘玉栋1 小时前
《字符串杀手锏 · 正则表达式之一》
数据库·mysql·正则表达式
GoodStudyAndDayDayUp1 小时前
一个简单的spring+kafka生产者
sql·spring·kafka
起的昵称都被用过了1 小时前
mysql-sql-第十四周
数据库·sql·mysql
YoungSoulwt1 小时前
23- Redis 主从复制是怎么实现的?
数据库·redis
Serendipity1 小时前
Mybatis
java·数据库·mybatis