mysql学习day04-02
TASK1
查询没有提成的雇员信息,列出其工号、姓名、薪水、岗位。
select userid 工号,concat(last_name,first_name) 姓名,salary 薪水,title 岗位 from s_emp where commission_pct is null or commission_pct = 0; +----------+---------------------+---------+--------------------+ | 工号 | 姓名 | 薪水 | 岗位 | +----------+---------------------+---------+--------------------+ | cvelasqu | VelasquezCarmen | 2500.00 | President | | lngao | NgaoLaDoris | 1450.00 | VP, Operations | | mnagayam | NagayamaMidori | 1400.00 | VP, Sales | | mquickto | Quick-To-SeeMark | 1450.00 | VP, Finance | | aropebur | RopeburnAudry | 1550.00 | VP, Administration | | murguhar | UrguhartMolly | 1200.00 | Warehouse Manager | | rmenchu | MenchuRoberta | 1250.00 | Warehouse Manager | | bbiri | BiriBen | 1100.00 | Warehouse Manager | | acatchpo | CatchpoleAntoinette | 1300.00 | Warehouse Manager | | mhavel | HavelMarta | 1307.00 | Warehouse Manager | | emaduro | MaduroElena | 1400.00 | Stock Clerk | | gsmith | SmithGeorge | 940.00 | Stock Clerk | | anozaki | NozakiAkira | 1200.00 | Stock Clerk | | vpatel | PatelVikram | 795.00 | Stock Clerk | | cnewman | NewmanChad | 750.00 | Stock Clerk | | amarkari | MarkarianAlexander | 850.00 | Stock Clerk | | echang | ChangEddie | 800.00 | Stock Clerk | | rpatel | PatelRadha | 795.00 | Stock Clerk | | bdancs | DancsBela | 860.00 | Stock Clerk | | sschwart | SchwartzSylvie | 1100.00 | Stock Clerk | +----------+---------------------+---------+--------------------+
查询last_name中第三个字母是a的员工工号、姓、名。
select userid 工号,last_name 姓,first_name 名 from s_emp where last_name like '__a%'; +--------+-------+---------+ | 工号 | 姓 | 名 | +--------+-------+---------+ | lngao | Ngao | LaDoris | | echang | Chang | Eddie | +--------+-------+---------+
统计每个部门的最高工资、最低工资、工资总和、平均工资,保留到整数位。
select dept_id 部门编号,round(max(salary)) 最高工资,round(min(salary)) 最低工资,round(sum(salary)),round(avg(salary)) 平均工资 from s_emp,s_dept where s_dept.id =s_emp.dept_id group by dept_id; +--------------+--------------+--------------+--------------------+--------------+ | 部门编号 | 最高工资 | 最低工资 | round(sum(salary)) | 平均工资 | +--------------+--------------+--------------+--------------------+--------------+ | 10 | 1450 | 1450 | 1450 | 1450 | | 31 | 1400 | 1400 | 2800 | 1400 | | 41 | 1450 | 940 | 4990 | 1248 | | 50 | 2500 | 1550 | 4050 | 2025 | | 32 | 1490 | 1490 | 1490 | 1490 | | 42 | 1250 | 795 | 3245 | 1082 | | 33 | 1515 | 1515 | 1515 | 1515 | | 43 | 1100 | 750 | 2700 | 900 | | 34 | 1525 | 795 | 2320 | 1160 | | 44 | 1300 | 800 | 2100 | 1050 | | 35 | 1450 | 1450 | 1450 | 1450 | | 45 | 1307 | 860 | 3267 | 1089 | +--------------+--------------+--------------+--------------------+--------------+
统计平均工资高于2000的部门,列出部门编号、部门名称以及该部门平均工资(保留到整数位)。
select dept_id 部门编号,name 部门名称,round(avg(salary)) 平均工资 from s_emp,s_dept where s_dept.id = s_emp.dept_id group by dept_id,name having 平均工资>2000; +--------------+----------------+--------------+ | 部门编号 | 部门名称 | 平均工资 | +--------------+----------------+--------------+ | 1 | Administration | 2025 | +--------------+----------------+--------------+
统计员工工号、姓名及其直接上司的工号、姓名
select e1.id 上司编号,e1.userid 上司工号,concat(e1.first_name,e1.last_name) 上司姓名,e2.id 员工编号,e2.userid 员工工号,concat(e2.first_name,e2.last_name) 员工姓名 from s_emp e1 join s_emp e2 wheree2.manager_id=e1.id; +--------------+--------------+---------------------+--------------+--------------+---------------------+ | 上司编号 | 上司工号 | 上司姓名 | 员工编号 | 员工工号 | 员工姓名 | +--------------+--------------+---------------------+--------------+--------------+---------------------+ | 1 | cvelasqu | CarmenVelasquez | 2 | lngao |LaDorisNgao | | 1 | cvelasqu | CarmenVelasquez | 3 | mnagayam |MidoriNagayama | | 1 | cvelasqu | CarmenVelasquez | 4 | mquickto |MarkQuick-To-See | | 1 | cvelasqu | CarmenVelasquez | 5 | aropebur |AudryRopeburn | | 2 | lngao | LaDorisNgao | 6 | murguhar |MollyUrguhart | | 2 | lngao | LaDorisNgao | 7 | rmenchu |RobertaMenchu | | 2 | lngao | LaDorisNgao | 8 | bbiri |BenBiri | | 2 | lngao | LaDorisNgao | 9 | acatchpo |AntoinetteCatchpole | | 2 | lngao | LaDorisNgao | 10 | mhavel |MartaHavel | | 3 | mnagayam | MidoriNagayama | 11 | cmagee |ColinMagee | | 3 | mnagayam | MidoriNagayama | 12 | hgiljum |HenryGiljum | | 3 | mnagayam | MidoriNagayama | 13 | ysedeghi |YasminSedeghi | | 3 | mnagayam | MidoriNagayama | 14 | mnguyen |MaiNguyen | | 3 | mnagayam | MidoriNagayama | 15 | adumas |AndreDumas | | 6 | murguhar | MollyUrguhart | 16 | emaduro |ElenaMaduro | | 6 | murguhar | MollyUrguhart | 17 | gsmith |GeorgeSmith | | 7 | rmenchu | RobertaMenchu | 18 | anozaki |AkiraNozaki | | 7 | rmenchu | RobertaMenchu | 19 | vpatel |VikramPatel | | 8 | bbiri | BenBiri | 20 | cnewman |ChadNewman | | 8 | bbiri | BenBiri | 21 | amarkari |AlexanderMarkarian | | 9 | acatchpo | AntoinetteCatchpole | 22 | echang |EddieChang | | 9 | acatchpo | AntoinetteCatchpole | 23 | rpatel |RadhaPatel | | 10 | mhavel | MartaHavel | 24 | bdancs |BelaDancs | | 10 | mhavel | MartaHavel | 25 | sschwart |SylvieSchwartz | +--------------+--------------+---------------------+--------------+--------------+---------------------+
查询没有提成的雇员信息,列出其工号、姓名、薪水、岗位。
select s_e.userid 员工工号,concat(s_e.last_name,s_e.first_name) 姓名,s_e.dept_id 部门编号,s_d.name 部门名称,s_r.name 部门所在地区 from s_emp s_e,s_dept s_d,s_region s_r wheres_e.dept_id = s_d.id and s_d.region_id = s_r.id; +--------------+---------------------+--------------+----------------+----------------------+ | 员工工号 | 姓名 | 部门编号 | 部门名称 | 部门所在地区 | +--------------+---------------------+--------------+----------------+----------------------+ | mquickto | Quick-To-SeeMark | 10 | Finance | North America | | mnagayam | NagayamaMidori | 31 | Sales | North America | | cmagee | MageeColin | 31 | Sales | North America | | lngao | NgaoLaDoris | 41 | Operations | North America | | murguhar | UrguhartMolly | 41 | Operations | North America | | emaduro | MaduroElena | 41 | Operations | North America | | gsmith | SmithGeorge | 41 | Operations | North America | | cvelasqu | VelasquezCarmen | 50 | Administration | North America | | aropebur | RopeburnAudry | 50 | Administration | North America | | hgiljum | GiljumHenry | 32 | Sales | South America | | rmenchu | MenchuRoberta | 42 | Operations | South America | | anozaki | NozakiAkira | 42 | Operations | South America | | vpatel | PatelVikram | 42 | Operations | South America | | ysedeghi | SedeghiYasmin | 33 | Sales | Africa / Middle East | | bbiri | BiriBen | 43 | Operations | Africa / Middle East | | cnewman | NewmanChad | 43 | Operations | Africa / Middle East | | amarkari | MarkarianAlexander | 43 | Operations | Africa / Middle East | | mnguyen | NguyenMai | 34 | Sales | Asia | | rpatel | PatelRadha | 34 | Sales | Asia | | acatchpo | CatchpoleAntoinette | 44 | Operations | Asia | | echang | ChangEddie | 44 | Operations | Asia | | adumas | DumasAndre | 35 | Sales | Europe | | mhavel | HavelMarta | 45 | Operations | Europe | | bdancs | DancsBela | 45 | Operations | Europe | | sschwart | SchwartzSylvie | 45 | Operations | Europe | +--------------+---------------------+--------------+----------------+----------------------+
TASK2
create table t_students (id int auto_increment comment '学生编号',name varchar(20)comment '学生姓名' not null ,gender varchar(10) comment '学生性别',primary key(id)); create table t_courses (id int auto_increment comment '课程编号',name varchar(20)comment '课程名称' not null unique, credit varchar(20) not null comment '学分',primarykey (id)); create table t_scores(sid int comment '学生编号',cid int comment '课程编号',exam_time date not null comment '考试时间',score float comment '考试成绩'); alter table t_scores add constraint foreign key fk_scores_students (sid) referencest_students(id);alter table t_scores add constraint foreign key fk_scores_courses (cid) referencest_courses(id);
insert into t_students (id,name,gender) values (101,'张无忌','男'),(102,'赵敏','女'),(103,'小昭','女'),(104,'周芷若','女'),(105,'常遇春','男'),(106,'徐达','男'); insert into t_courses (id,name,credit) values (1601,'C++',3),(1602,'Java',3),(1603,'Oracle',2),(1604,'HTML/CSS',3),(1605,'数据结构',3); insert into t_scores (sid,cid,exam_time,score) values (101,1601,'2021-07-11',99),(101,1601,'2021-07-15',92),(101,1602,'2021-07-15',92),(102,1603,'2021-07-10',88),(103,1602,'2021-07-15',90),(104,1601,'2021-07-11',97),(104,1604,'2021-07-12',100);
查询每个学生每门课程的成绩,列出学生姓名、课程名称、考试时间、考试成绩。
select st.name 学生姓名,co.name 课程名称,sc.exam_time 考试时间,sc.score 考试成绩 fromt_students st , t_course s co,t_scores sc where sc.sid = st.id and sc.cid = co.id; +--------------+--------------+--------------+--------------+ | 学生姓名 | 课程名称 | 考试时间 | 考试成绩 | +--------------+--------------+--------------+--------------+ | 张无忌 | C++ | 2021-07-11 | 99 | | 张无忌 | C++ | 2021-07-15 | 92 | | 周芷若 | C++ | 2021-07-11 | 97 | | 周芷若 | HTML/CSS | 2021-07-12 | 100 | | 张无忌 | Java | 2021-07-15 | 92 | | 小昭 | Java | 2021-07-15 | 90 | | 赵敏 | Oracle | 2021-07-10 | 88 | +--------------+--------------+--------------+--------------+
统计所有男生每门课程的平均成绩,列出课程名称、平均成绩。
select co.name 课程名称,avg(score) 平均成绩 , st.gender 性别 from t_students st ,t_courses co,t_scores sc where sc.sid = st.id and sc.cid = co.id and st.gender='男' group by 课程名称,性别; +--------------+--------------+--------+ | 课程名称 | 平均成绩 | 性别 | +--------------+--------------+--------+ | C++ | 95.5 | 男 | | Java | 92 | 男 | +--------------+--------------+--------+
查询没有学生选秀的课程信息,列出其编号、名称、学分。
select id 课程编号,name 课程名,credit 学分 from t_courses where id not in (select cid from t_scores); +--------------+--------------+--------+ | 课程编号 | 课程名 | 学分 | +--------------+--------------+--------+ | 1605 | 数据结构 | 3 | +--------------+--------------+--------+
mysql学习day04-02相关推荐
- MySQL学习笔记02【SQL基本概念与通用语法、数据库的CRUD操作】
MySQL 文档-黑马程序员(腾讯微云):https://share.weiyun.com/RaCdIwas 1-MySQL基础.pdf.2-MySQL约束与设计.pdf.3-MySQL多表查询与事务 ...
- MySQL学习笔记 02、MySQL基础(覆盖基本知识点)
文章目录 前言 一.Mysql介绍 1.1.了解Mysql 1.2.认识Socket 1.3.查看设置变量(以及全局) 二.SQL 2.1.DCL(数据控制语言) 2.2.DDL(数据定义语言)重要 ...
- mysql学习day04
任务1:执行SQL脚本创建表.插入数据 create database company mysql -u root -p company < /Users/caizhenzu/Desktop ...
- mysql 学习笔记 02创建表
表结构的创建 比如: create table userinfo (id int unsigned comment 'id号'name varchar(60) comment '用户名'passwor ...
- mysql学习day03——02
查询 使用DISTINCT剔除重复行(DISTINCT必须紧跟在select之后) select distinct deptno from emp; select distinct deptno,jo ...
- MySQL学习笔记07【事务、用户管理和权限管理】
MySQL 文档-黑马程序员(腾讯微云):https://share.weiyun.com/RaCdIwas 1-MySQL基础.pdf.2-MySQL约束与设计.pdf.3-MySQL多表查询与事务 ...
- MySQL学习笔记06【多表查询、子查询、多表查询练习】
MySQL 文档-黑马程序员(腾讯微云):https://share.weiyun.com/RaCdIwas 1-MySQL基础.pdf.2-MySQL约束与设计.pdf.3-MySQL多表查询与事务 ...
- MySQL学习笔记05【多表操作、三大范式、数据库的备份和还原】
MySQL 文档-黑马程序员(腾讯微云):https://share.weiyun.com/RaCdIwas 1-MySQL基础.pdf.2-MySQL约束与设计.pdf.3-MySQL多表查询与事务 ...
- MySQL学习笔记04【数据库的查询操作、今日内容、表的约束】
MySQL 文档-黑马程序员(腾讯微云):https://share.weiyun.com/RaCdIwas 1-MySQL基础.pdf.2-MySQL约束与设计.pdf.3-MySQL多表查询与事务 ...
- MySQL学习笔记03【数据库表的CRUD操作、数据库表中记录的基本操作、客户端图形化界面工具SQLyog】
MySQL 文档-黑马程序员(腾讯微云):https://share.weiyun.com/RaCdIwas 1-MySQL基础.pdf.2-MySQL约束与设计.pdf.3-MySQL多表查询与事务 ...
最新文章
- PHP一些十分严重的缺陷
- 基于视觉的在线地图:一种Transformer网络方法
- mapper里面select id 后面一直红名_YTG晨晨改ID“进厂找班上了”,如此自嘲,心态还挺好...
- [渝粤教育] 西南科技大学 高级语言程序设计(Java) 在线考试复习资料
- 织梦cms响应式影视传媒模板
- mysql数据库 主主的搭建
- flow time 是什么
- selenium firebug firePath xpath问题
- php 获取xlsx,PHP Excel Reader读取xlsx文件
- QQ分析和密码获取,阿里旺旺分析和密码获取,新浪UC分析和密码获取,以及所有的UC端...请不要乱用,我可不负责
- B站学习法之深度学习笔记一
- python大鱼吃小鱼程序代码_pygame大鱼吃小鱼源代码
- 爱签:如何在线签订电子合同
- 商城购物APP——YiGo
- 零基础学HTML5的学习路线完整版
- 新版标准日本语高级_第7课
- 【ChatGPT|AI 应用】ChatGPT + MindShow 快速制作 PPT
- 用计算机弹起风了歌词,《起风了买辣椒也用券 抖音热歌起风了》的歌词 LRC完整歌词...
- 基于人工智能的图像处理技术:利用Opencv实现
- 开机后黑屏看不到桌面_开机后黑屏看不到桌面怎么解决
热门文章
- 使用Monit监控进程与系统状态
- 异常总结2013-04
- SCCM 2012安装部署三:客户端管理
- 华为机试HJ9:提取不重复的整数
- activiti 条件表达式json报错_json模块和pickle模块
- chrome 打印布局_在打印预览模式下使用Chrome的Element Inspector?
- linux内核按键驱动,嵌入式Linux按键驱动框架
- java list_java中的list集合
- linux编译c文件for循环,Linux C 循环队列的实现
- access vba 常量数组赋值_09c语言数组详解