简介:本文章是我基于B站上SQL播放量第一的MySQL 基础+高级篇- 数据库 -sql -尚硅谷视频所做的笔记,方便记录自己的学习过程。

说明:

  1. 本文章是基于视频中老师讲课的案例、部分测试题。*
  2. 所需的配套资料(来自B站评论区)。附一个链接:
    B站黎曼的猜想视频配套资料提取码: k6kq

案例讲解,基础查询

select * from employees;desc departments;select * from departments;select distinct job_id from employees;select concat(first_name,',',last_name,',',job_id,',',ifnull(commission_pct,0)) as out_put
from employees;select concat(first_name,',',last_name,',',job_id,',',ifnull(commission_pct,0)) as out_put
from employees;

案例讲解,条件查询

select salary from employees;select last_name,department_id,salary*12*(1+ifnull(commission_pct,0)) as 年薪
from employees;select last_name,salary
from employees
where salary > 12000;select last_name,salary
from employees
where salary < 5000 or salary > 12000;select last_name,department_id
from employees
where department_id = 20 or department_id = 50;select last_name,job_id
from employees
where manager_id is null;select last_name
from employees
where last_name like '%a%';

测试题1讲解

select salary,last_name
from employees
where commission_pct is null and salary < 18000;select * from employees
where job_id <> 'IT' or salary = 12000;desc departments;select location_id
from departments;select * from employees
where commission_pct like '%%' and last_name like '%%';select last_name,department_id,salary*12*(1+ifnull(commission_pct,0)) as 年薪
from employees
order by 年薪 desc,last_name;

排序查询

select last_name,salary
from employees
where salary < 8000 or salary > 17000
order by salary desc;select last_name,salary
from employees
where salary not between 8000 and 17000
order by salary desc;select email from employees;select * from employees
where email like '%e%'
order by length(email) desc, department_id asc;

单行函数

#显示系统时间日期+时间
select now();select employee_id,last_name,salary,salary*(1+0.2) as 'new salary'
from employees;select length(last_name) 长度,substr(last_name,1,1) 首字符, last_name
from employees
order by last_name;selectconcat(last_name,' ','earns',' ',round(salary,0),' ','monthly but wants',' ',round(salary*3,0)) as 'Dream Salary'
from employees;select job_id as job,
case job_id
when 'AD_PRES' then 'A'
when 'ST_MAN' then 'B'
when 'IT_PROG' then 'C'
when 'SA_PRE' then 'D'
when 'ST_CLERK' then 'E'
end as Grade
from employees
where job_id = 'AD_PRES';select last_name,job_id as Job_id,
case job_id
when 'AD_PRES' then 'A'
when 'ST_MAN' then 'B'
when 'IT_PROG' then 'C'
when 'SA_PRE' then 'D'
when 'ST_CLERK' then 'E'
end as Grade
from employees
where job_id = 'AD_PRES';select last_name,job_id as Job_id,
case job_id
when 'AD_PRES' then 'A'
when 'ST_MAN' then 'B'
when 'IT_PROG' then 'C'
when 'SA_PRE' then 'D'
when 'ST_CLERK' then 'E'
end as Grade
from employees
where job_id = 'AD_PRES';

查询相隔天数

select datediff(max(hiredate),min(hiredate)) as DIFFERENCE
from employees;

分组查询

select job_id,round(max(salary),2) max_sal,round(min(salary),2) min_sal,round((salary),2) avg_sal,round(sum(salary),2) sum_sal
from employees
group by job_id
order by job_id ASC;select max(salary)-min(salary) as DIFFERENCE
from employees;select manager_id,min(salary)
from employees
where manager_id is not null
group by manager_id
having min(salary) >= 6000;select job_id,count(*)
from employees
group by job_id;select department_id,count(*),avg(salary)
from employees
group by department_id
order by avg(salary) desc;

测试题2讲解


select round(max(salary),2),round(avg(salary),2)
from employees;select employee_id,job_id,last_name
from employees
order by department_id desc, salary asc;select * from employees
where job_id like '%a%e%';select trim('' from now());

作业讲解

select last_name,e.department_id,d.department_name
from employees e
left join departments d
on e.department_id = d.department_id;select job_id,location_id
from employees e join departments d
on e.department_id = d.department_id
where e.department_id = 90;select last_name,department_name,l.location_id,city
from departments d
join employees e
on d.department_id = e.department_id
join locations l
on d.location_id = l.location_id
where e.commission_pct is not null;select last_name,job_id,d.department_id,d.department_name
from departments d
join employees e
on d.department_id = e.department_id
join locations l
on d.location_id = l.location_id
where l.city = 'Toronto';select d.department_name,j.job_title,min(e.salary)
from departments d
join employees e
on d.department_id = e.department_id
join jobs j
on e.job_id = j.job_id
group by j.job_title,d.department_name;select l.country_id,count(*) 部门个数
from departments d
join employees e
on d.department_id = e.department_id
join locations l
on d.location_id = l.location_id
group by l.country_id
having count(e.department_id)> 2;select country_id,count(*) 部门个数
from departments d,locations l
where d.location_id = l.location_id
group by country_id
having count(*) > 2; select e.last_name employees,e.employee_id 'Emp#',m.last_name manager,m.employee_id 'Mgr#'
from employees e,employees m
where e.manager_id = m.employee_id
and e.last_name = 'Kochhar';

多表查询

select be.id,be.name,bo.*
from beauty be
left outer join boys bo
on be.boyfriend_id = bo.id
where be.id > 3;select city,d.*
from locations l
left join departments d
on d.location_id = l.location_id
where d.department_id is null;select city,d.*
from departments d
right join locations l
on l.location_id = d.location_id
where d.department_id is null;select * from departments;select e.*,d.department_name,d.department_id
from employees e
left join departments d
on e.department_id = d.department_id
where d.department_name = 'SAL' or d.department_name = 'IT';select e.*,d.department_name,d.department_id
from departments d
left join employees e
on e.department_id = d.department_id
where d.department_name = 'SAL' or d.department_name = 'IT';select job_id from employees
where employee_id = 141;select salary
from employees
where employee_id = 143;select last_name,job_id,salary
from employees
where job_id = (select job_id from employeeswhere employee_id = 141
)
and salary > (select salary
from employees
where employee_id = 143
);select last_name,job_id,salary
from employees
where salary = (select min(salary)from employees
);
#查询最低工资大于50号部门最低工资的部门id,及其最低工资
#1找50号部门最低工资
select min(salary) from employees
where department_id = 50;#2每一个部门最低工资
select department_id,min(salary)
from employees
group by department_id;#筛选2,且salary > 1
select department_id,min(salary)
from employees
group by department_id
having min(salary) > (select min(salary) from employeeswhere department_id = 50
);select department_id,min(salary)
from employees;select department_id,min(salary)
from employees
group by department_id;select department_id,min(salary)
from employees
group by department_id
having min(salary) > (select min(salary)from employeeswhere department_id = 50
);#1找141号员工的job_id
select job_id from employees
where employee_id = 141;#2找143号员工的salary
select salary from employees
where employee_id = 143;select last_name,job_id,salary
from employees
where job_id = (select job_id from employeeswhere employee_id = 141
)
and salary > (select salary from employeeswhere employee_id = 143
);

子查询

#子查询#找到Zlotkey所在的部门id
select department_id
from employees
where last_name = 'Zlotkey';#1
select employee_id,last_name,salary
from employees
where department_id = (select department_idfrom employeeswhere last_name = 'Zlotkey'
);#先找公司平均工资
select avg(salary)
from employees;#2
select employee_id,last_name,salary
from employees
where salary > (select avg(salary)from employees
);#各部门平均工资
select department_id,avg(salary)
from employees
group by department_id;#3查询各个部门中工资比本部门平均工资高的员工号,姓名和工资
select e.department_id,last_name,employee_id,salary
from employees e
inner join(select department_id,avg(salary) av_salfrom employeesgroup by department_id
) as av_dep
on e.department_id = av_dep.department_id
where salary > av_dep.av_sal;#4查询姓名中包含字母u的员工号
select department_id, employee_id,last_name
from employees
where last_name like '%u%';select e.employee_id,e.last_name
from employees e
inner join (select distinct department_id, employee_id,last_namefrom employeeswhere last_name like '%u%'
) as new_tab
on e.department_id = new_tab.department_id;select last_name,employee_id
from employees
where department_id in (select  distinct department_idfrom employeeswhere last_name like '%u%');#5先查询location_id=1700 所在部门
select department_id
from departments
where location_id = 1700;select employee_id
from employees e
where department_id in (select department_idfrom departmentswhere location_id = 1700
);
#6
select * from employees
where last_name = 'K_ing';select manager_id
from employees
where last_name = 'K_ing';select last_name,salary
from employees
where manager_id in (select manager_idfrom employeeswhere last_name = 'K_ing'
);#查询管理者是K_ing的员工姓名和工资
#1先找到姓名是K_ing的员工编号
#2再找manager_id=1 的员工姓名和工资
select employee_id
from employees
where last_name = 'K_ing';select last_name,salary
from employees
where manager_id in (select distinct employee_idfrom employeeswhere last_name = 'K_ing'
);#7查询工资最高的员工姓名,要求first_name和last_name显示为一列,列名为姓.名
#1先找到工资最高的员工编号
select employee_id
from employees
where salary >= (select max(salary) from employees);#2再employee_id in 1  (concat连接)
select concat(first_name,'.',last_name) 姓名
from employees
where employee_id = (select employee_idfrom employeeswhere salary >= (select max(salary) from employees)
);#简单方法(优化)
select concat(first_name,'.',last_name) 姓名
from employees
where salary = (select max(salary)from employees
);

测试题3讲解

#1查询所有学员的邮箱用户名(@前的字符) john@126.com
select substr(email,1,instr(email,'@')-1) 用户名
from stuinfo;#2查询男生和女生个数
select count(*) 个数,sex
from stuinfo
group by sex;#3查询年龄大于18所有学生姓名和年级名
select name,gradeName
from stuinfo
join grade
on stuinfo.gradeId = grade.id
where age > 18;#4查询那个年级学生最小年龄大于二十岁
#找到每个年级的最小年龄
select min(age)
from stuinfo
group by gradeId;#5查询语句执行的先后顺序
#select\from\join\on\where\group by\having\order by\limit
#from\join\on\where\group by\having\select\order by\limit

子查询经典案例讲解

#1
select last_name,salary
from employees
where salary = (select min(salary)from employees
);#2
#最低平均工资
select department_id
from employees
group by department_id
order by avg(salary)
limit 1;select d.*
from departments d
join (select department_id
from employees
group by department_id
order by avg(salary)
limit 1
) as new_tab
on d.department_id = new_tab.department_id;#3
#先找各部门平均工资
select avg(salary)
from employees
group by department_id
order by avg(salary)
limit 1;select d.*,ag
from departments d
join (select avg(salary) ag,department_idfrom employeesgroup by department_idorder by avg(salary)limit 1
) as ag_dep
on d.department_id = ag_dep.department_id;#4找出平均工资最高的job信息
#先找出平均工资最高的job_id
select avg(salary) as ag_sal,job_id
from employees
group by job_id
order by avg(salary) desc
limit 1;select j.*,ag_sal
from jobs j
join (select avg(salary) as ag_sal,job_idfrom employeesgroup by job_idorder by avg(salary) desclimit 1
) as new_tab
on j.job_id = new_tab.job_id;#5查询公司平均工资
select avg(salary) man_ag
from employees;#查询各部门的平均工资
select avg(salary) dep_ag,department_id
from employees
group by department_id;#用各部门>公司工资
select d.department_id,d.department_name,dep_ag
from departments d
join (select avg(salary) dep_ag,department_id
from employees
group by department_id) as new_tab
on d.department_id = new_tab.department_id
where dep_ag > (select avg(salary) man_ag
from employees);#6查询公司所有manager详细信息
select distinct manager_id
from employees;
select *
from employees
where employee_id in (select distinct manager_idfrom employees
);#7各个部门中,最高工资中最低的那个部门,最低工资是多少
#先查询各个部门中最高工资
select max(salary),department_id
from employees
group by department_id;#再找到以上最低的工资及其所在部门
select department_id
from employees
group by department_id
order by max(salary)
limit 1;#查询上述部门编号所对应信息
select min(salary),department_id
from employees
where department_id = (select department_idfrom employeesgroup by department_idorder by max(salary)limit 1
);#8查询平均工资最高的部门的manager的详细信息:last_name,department_id,email,salary
#先查出各部门平均工资中最高的
select department_id
from employees
group by department_id
order by avg(salary) desc
limit 1;#找到部门id是上述id,所对应的manager_id
select distinct manager_id
from employees
where department_id = (select department_idfrom employeesgroup by department_idorder by avg(salary) desclimit 1
);#根据上述manager_id得到employee_id,找到姓名,部门号,邮箱,薪资
select last_name,department_id,email,salary
from employees
where employee_id in (
select distinct manager_id
from employees
where department_id = (select department_idfrom employeesgroup by department_idorder by avg(salary) desclimit 1
)
);

黎曼的猜想 MySQL案例练习记录相关推荐

  1. ansible 安装 mysql 案例记录

    文章目录 ansible 安装 mysql 案例记录 ansible 安装 mysql 案例记录 本次案例参考 二进制安装mysql 以下 yml 文件仍有很多不完善的地方,后续再继续修改 --- - ...

  2. MySQL 案例-教学管理信息系统

    MySQL 案例-教学管理信息系统 一.数据库设计及创建 二.向表中插入数据.修改数据 三.完成以下查询设计及SQL代码 四.完成如下数据库应用设计 一.数据库设计及创建 对教学管理信息系统,在需求分 ...

  3. php校友录毕业论文,基于WEB的同学校友录的设计(PHP,MySQL)(附答辩记录)

    基于WEB的同学校友录的设计(PHP,MySQL)(附答辩记录)(选题审批表,任务书,开题报告,中期报告,毕业论文7700字,文献综述,答辩记录,成绩评定册) 摘 要:通过提供便捷的校友录平台和人性化 ...

  4. [MySQL实践] 实践记录

    [MySQL实践] 实践记录 版权2019.5.17更新 MySQL MySQL各版本区别 一.选择的版本1. MySQL Community Server 社区版本,开源免费,但不提供官方技术支持. ...

  5. centos7安装mysql日志空白_centos7安装Mysql爬坑记录 - G

    centos7安装Mysql爬坑记录 查看是否已安装 使用下列命令查看是否已经安装过mysql/mariadb/PostgreSQL 如果未安装,不返回任何结果(ECS的centos镜像默认未安装任何 ...

  6. mysql查询死锁的次数_一次神奇的MySQL死锁排查记录

    一次神奇的MySQL死锁排查记录 发布时间:2020-08-29 00:50:26 来源:脚本之家 阅读:135 作者:咖啡拿铁 背景 说起Mysql死锁,之前写过一次有关Mysql加锁的基本介绍,对 ...

  7. MySQL学习足迹记录01--SOURCE,SHOW

    MySQL学习足迹记录01--SOURCE,SHOW MySQL学习足迹记录02--SELECT MySQL学习足迹记录03--ORDER BY,DESC MySQL学习足迹记录04--数据过滤--W ...

  8. mysql外建名是随机的吗_创建角色随机名字(mysql抽取随机记录)和mysql游标的使用_MySQL...

    最近在开发中遇到了一些问题,在此记录一下解决的方法,已作备忘. 1.现在创建游戏角色的时候,基本上都是支持角色名字随机的,以前此功能在客户端用代码实现,然后向服务器请求并验证,后来发现有时候连续几次都 ...

  9. Neo4j从mysql读取数据_[bigdata-086] python3+neo4j 从mysql数据库读取记录然后创建节点和关系写入到neo4j...

    1. 测试 1.1web界面  http://tz211:7474/browser/ 在这里执行 MATCH (n) RETURN n,能看到节点和相互关系 一共是3个节点,6个相互关系 1.2 在2 ...

最新文章

  1. 超赞!2021年最惊艳的38篇AI论文!
  2. 万字长文:深度解读最新发布的《国家数据安全法》 by 傅一平
  3. 训练 AI 学会通过复制来构建 AI 系统
  4. openjdk(HOTSPOT)垃圾回收源码解读
  5. SAP Spartacus Table cell显示数据类型的Component决定逻辑
  6. oracle xml中cdata,XML CDATA的作用
  7. Linux学习总结(12)——Linux必须学会的60个命令
  8. MySQL复习值代码知识点(2)
  9. 阿里达摩院420集python_阿里达摩院推荐的420集的python教程,入门到精通简直不要太简单...
  10. error: Microsoft Visual C++ 14.0 or greater is required. Get it with Microsoft C++ Build Tools终极方案
  11. linux kernel pwn学习之堆漏洞利用+bypass smap、smep
  12. 【Al TIME】博硕论坛064期 | 清华计算机系大佬齐论Al
  13. C#操作Excel文件(读取Excel,写入Excel)
  14. 使用 Python 生成类数字字母混合验证码图片
  15. JavaScript弹出模式窗口
  16. 国防科大计算机科学与技术专业排名,国防科技大学王牌专业排名
  17. javascript_outline
  18. 西班牙语基础必备词汇(动物和食物)
  19. 在服务器上一按l键自动退出,利用 SysRq 键排除和诊断系统故障
  20. QEMU零知识学习2 —— QEMU源码下载

热门文章

  1. linux打开xml文件,查看 XML 文件
  2. 稳压二极管和TVS二极管的区别
  3. 一家国营老化工厂的数字化三级跳|案例解析
  4. 自动快捷使用数据采集器采集某网站数据---后裔采集器
  5. IDEA括起选中的选中的内容
  6. 【转】String求求你别秀了
  7. 在线vr高清3d展示线上三维展示平台
  8. 要想文章好,图片少不了,22个技能助您获得美图(带字幕视频)
  9. 【计算机视觉算法岗面经】“吐血”整理:2019秋招面经
  10. 6 电脑键盘控制机器人