Mysql——查询sql语句练习

news2025/7/15 14:58:18

一、单表查询


素材: 表名:worker-- 表中字段均为中文,比如 部门号 工资 职工号 参加工作 等

1、显示所有职工的基本信息。
select * from worker;
2、查询所有职工所属部门的部门号,不显示重复的部门号。
select distinct 部门号 from worker;
3、求出所有职工的人数。
select count("职工号") as 人数 from worker;
4、列出最高工和最低工资。
select min(工资),max(工资) from worker;
5、列出职工的平均工资和总工资。
 select avg(工资) 平均工资,sum(工资) 总工资 from worker;
6、创建一个只有职工号、姓名和参加工作的新表,名为工作日期表。 
create table work_day(
    职工号 int(11) not null,
    姓名 varchar(20) not null,
    参加工作 text not null,
    primary key(职工号)
);
mysql> insert into work_day(职工号,姓名,参加工作,性别) values(1001,'张三','会计','男');
mysql> insert into work_day(职工号,姓名,参加工作,性别) values(1002,'李四','Java工程师','男');
mysql> insert into work_day(职工号,姓名,参加工作,性别) values(1003,'王亮','Java工程师','男');
mysql> insert into work_day(职工号,姓名,参加工作,性别) values(1004,'赵六','网络工程师','男');
mysql> insert into work_day(职工号,姓名,参加工作,性别) values(1005,'钱七','会计','女');
mysql> insert into work_day(职工号,姓名,参加工作,性别) values(1006,'孙八','运维工程师','女');
7、显示所有女职工的年龄。
 select worker.姓名,year(now())-year(出生日期) as 年龄 from worker inner join work_day on(worker.职工号=work_day.职工号) where 性别='女';
8、列出所有姓张的职工的职工号、姓名和出生日期。
select 职工号,姓名,工作时间 from worker where 姓名 like '张%';
 select 职工号,姓名,工作时间 from worker where left(姓名,1)='张';
9、列出1960年以前出生s职工的姓名、参加工作日期。
 select 姓名,工作时间 from worker where year(出生日期)<1960;
10、列出工资在1000-2000之间的所有职工姓名。
 select 姓名 from worker where 工资>1000 and 工资<2000;
11、列出所有陈姓和李姓的职工姓名。
select 职工号,姓名,工作时间 from worker where left(姓名,1)='陈'or left(姓名,1)='李';
 select 职工号,姓名,工作时间 from worker where 姓名 like '李%' or '陈%';
12、列出所有部门号为102和103的职工号、姓名、党员否。
 select 职工号,姓名,政治面貌 from worker where 部门号=102 or 部门号=103;
13、将职工表worker中的职工按出生的先后顺序排序。
select * from worker order by 出生日期 asc;
14、显示工资最高的前3名职工的职工号和姓名。 
select 职工号,姓名 from worker order by 工资 desc limit 3;
15、求出各部门党员的人数。
 select distinct(部门号),count(政治面貌) as 党员人数 from worker where 政治面貌='党员' group by 部门号;
16、统计各部门的工资和平均工资
 select distinct(部门号),sum(工资) as 工资,avg(工资) as 平均工资 from worker group by  部门号;
17、列出总人数大于4的部门号和总人数。
select distinct(部门号),count(职工号) as 总人数 from worker group by 部门号 having count(职工号)>=4;

二、多表查询


1.创建student和score表
CREATE  TABLE student (
id  INT(10)  NOT NULL  UNIQUE  PRIMARY KEY ,
name  VARCHAR(20)  NOT NULL ,
sex  VARCHAR(4) ,
birth  YEAR,
department  VARCHAR(20) ,
address  VARCHAR(50)
);
创建score表。SQL代码如下:
CREATE  TABLE score (
id  INT(10)  NOT NULL  UNIQUE  PRIMARY KEY  AUTO_INCREMENT ,
stu_id  INT(10)  NOT NULL ,
c_name  VARCHAR(20) ,
grade  INT(10)
);
2.为student表和score表增加记录
向student表插入记录的INSERT语句如下:
INSERT INTO student VALUES( 901,'张老大', '男',1985,'计算机系', '北京市海淀区');
INSERT INTO student VALUES( 902,'张老二', '男',1986,'中文系', '北京市昌平区');
INSERT INTO student VALUES( 903,'张三', '女',1990,'中文系', '湖南省永州市');
INSERT INTO student VALUES( 904,'李四', '男',1990,'英语系', '辽宁省阜新市');
INSERT INTO student VALUES( 905,'王五', '女',1991,'英语系', '福建省厦门市');
INSERT INTO student VALUES( 906,'王六', '男',1988,'计算机系', '湖南省衡阳市');
向score表插入记录的INSERT语句如下:
INSERT INTO score VALUES(NULL,901, '计算机',98);
INSERT INTO score VALUES(NULL,901, '英语', 80);
INSERT INTO score VALUES(NULL,902, '计算机',65);
INSERT INTO score VALUES(NULL,902, '中文',88);
INSERT INTO score VALUES(NULL,903, '中文',95);
INSERT INTO score VALUES(NULL,904, '计算机',70);
INSERT INTO score VALUES(NULL,904, '英语',92);
INSERT INTO score VALUES(NULL,905, '英语',94);
INSERT INTO score VALUES(NULL,906, '计算机',90);
INSERT INTO score VALUES(NULL,906, '英语',85);

3.查询student表的所有记录
select * from student;
4.查询student表的第2条到4条记录
select * from student limit 1,3;
5.从student表查询所有学生的学号(id)、姓名(name)和院系(department)的信息
 select id,name,department from student;
6.从student表中查询计算机系和英语系的学生的信息
 select * from student where department='计算机系' or department='英语系';
7.从student表中查询年龄18~22岁的学生信息
 select * from student where (year(now())-birth)>18 and (year(now())-birth)<22;
8.从student表中查询每个院系有多少人
select distinct(department),count(id) as 人数 from student group by department;
9.从score表中查询每个科目的最高分
select c_name ,max(grade) as 最高分 from score group by c_name;
10.查询李四的考试科目(c_name)和考试成绩(grade)
> select score.c_name,score.grade from score inner join student on(student.id=score.stu_id) where student.name='李四';
11.用连接的方式查询所有学生的信息和考试信息
 select * from score inner join student on(student.id=score.stu_id);
12.计算每个学生的总成绩select student.name,sum(score.grade) as 总成绩 from score inner join student on(student.id=score.stu_id) group by student.name;
13.计算每个考试科目的平均成绩
select c_name,avg(grade) as average from score group by c_name;
14.查询计算机成绩低于95的学生信息
select * from score inner join student on(score.stu_id=student.id) where score.c_name='计算机'and score.grade<95;
15.查询同时参加计算机和英语考试的学生的信息
select * from student,(select * from score where c_name='英语') as s1 inner join (select * from score where c_name='计算机') as s2 on s1.stu_id=s2.stu_id where student.id=s1.stu_id;
16.将计算机考试成绩按从高到低进行排序
select grade from score where c_name='计算机' order by grade desc;
17.从student表和score表中查询出学生的学号,然后合并查询结果
select * from score inner join student on(student.id=score.stu_id);
18.查询姓张或者姓王的同学的姓名、院系和考试科目及成绩
 select student.name,student.department,score.c_name,score.grade from score inner join student on(student.id=score.stu_id) where student.name like '张%' or student.name like '王%';
19.查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩
select student.name,student.birth,student.department,score.c_name,score.grade from score inner join student on(student.id=score.stu_id) where student.address like '湖南%';
 
select student.name,student.birth,student.department,score.c_name,score.grade from score inner join student on(student.id=score.stu_id) where left(student.address,2)= '湖南';

 三.面试题

create table student2(
    s_id int(10) not null unique primary key   comment '学号',
    s_name varchar(20) not null comment '姓名',
    s_birth date not null comment '生日',
    s_sex enum("男", "女") default "男" comment '性别'
)comment ='学生表';
INSERT INTO student2 (s_id,s_name,s_birth,s_sex) VALUES (001, '潘帅', '2002-9-14', '男');
INSERT INTO student2 (s_id,s_name,s_birth,s_sex) VALUES (002, '王美丽', '2004-1-14', '女');
INSERT INTO student2 (s_id,s_name,s_birth,s_sex) VALUES (003, '张帅', '2012-5-4', '男');
INSERT INTO student2 (s_id,s_name,s_birth,s_sex) VALUES (004, '李帅', '2002-5-4', '男');
INSERT INTO student2 (s_id,s_name,s_birth,s_sex) VALUES (005, '吴美', '1999-4-10', '女');
INSERT INTO student2 (s_id,s_name,s_birth,s_sex) VALUES (006, '李七', '2006-7-14', '男');
INSERT INTO student2 (s_id,s_name,s_birth,s_sex) VALUES (007, '张六', '2001-9-14', '男');
INSERT INTO student2 (s_id,s_name,s_birth,s_sex) VALUES (008,'吴雪', '2008-1-24', '女');

create table course(
    c_id int(10) not null unique primary key AUTO_INCREMENT comment '课程编号',
    c_name varchar(20) not null comment '课程名称',
    t_id int(10) not null comment '教师编号',
    foreign key(t_id) references teacher(t_id)
)comment ='课程表';
INSERT INTO course(c_id,c_name,t_id) VALUES (null, '计算机',01 );
INSERT INTO course(c_id,c_name,t_id) VALUES (null, '英语',02 );
INSERT INTO course(c_id,c_name,t_id) VALUES (null, '数学',03 );

create table teacher(
    t_id int(10) not null unique primary key comment '教师编号',
    t_name varchar(20) not null comment '姓名'
)comment='教师表';
insert into teacher values('01' , '张三');
insert into teacher values('02' , '李四');
insert into teacher values('03' , '王五');

create table score2(
    s_id int(10) comment '学号',
    c_id int(10) comment '课程编号',
    s_score decimal(18,1) comment '成绩'
)comment='成绩表';

insert into score2 values('001' , '01' , 80);
insert into score2 values('001' , '02' , 90);
insert into score2 values('001' , '03' , 99);
insert into score2 values('002' , '01' , 70);
insert into score2 values('002' , '02' , 60);
insert into score2 values('002' , '03' , 80);
insert into score2 values('003' , '01' , 80);
insert into score2 values('003' , '02' , 80);
insert into score2 values('003' , '03' , 80);
insert into score2 values('004' , '01' , 50);
insert into score2 values('004' , '02' , 30);
insert into score2 values('004' , '03' , 20);
insert into score2 values('005' , '01' , 76);
insert into score2 values('005' , '02' , 87);
insert into score2 values('006' , '01' , 31);
insert into score2 values('006' , '03' , 34);
insert into score2 values('007' , '02' , 89);
insert into score2 values('007' , '03' , 98);
insert into score2 values('008' , '02' , 79);
insert into score2 values('008' , '03' , 68);


查询所有学生的学号、姓名、选课数、总成绩。 
select student2.s_id,student2.s_name,r.选课数,r.总成绩 from student2,(select score2.s_id,sum(score2.s_score) as 总成绩,count(score2.s_score) as 选课数 from score2 group by score2.s_id) as r where student2.s_id=r.s_id;

查询学过"张三"老师所教的所有课程的同学的学号和姓名。
 select student2.s_id,student2.s_name from student2,score2,teacher,course where student2.s_id=score2.s_id and course.t_id=teacher.t_id and score2.c_id=course.c_id and teacher.t_name='张三';

查询和'02'号同学学习的课程完全相同的其他同学学号和姓名。
不会写

使用分段[100-85],[85-70],[70-60],[<60]来统计各科成绩,分别统计各分数段的人数:课程ID和课程名称
select course.c_name, course.c_id,
sum(case when score2.s_score<=100 and score2.s_score>85 then 1 else 0 end) as "[100-85]",
sum(case when score2.s_score<=85 and score2.s_score>70 then 1 else 0 end) as "[85-70]",
sum(case when score2.s_score<=70 and score2.s_score>60 then 1 else 0 end) as "[70-60]",
sum(case when score2.s_score<=60 and score2.s_score>0 then 1 else 0 end) as "[60-0]"
from score2 left join course
on score2.c_id = course.c_id
group by score2.c_id;

四.


create table departments(
    dept_no varchar(10) not null,
    dept_name varchar(10) not null
);
insert into departments values('d001','Marketing');
insert into departments values('d002','Finance');

create table dept_emp(
    emp_no int(20) not null,
    dept_no varchar(10) not null,
    from_date date not null,
    to_date date not null
);
insert into dept_emp values(10001,'d001','2001-06-22','9999-01-01');
insert into dept_emp values(10002,'d001','1996-08-03','9999-01-01');
insert into dept_emp values(10003,'d002','1996-08-03','9999-01-01');

create table salaries(
    emp_no int(20) not null,
    salary int(20) not null,
    from_date date not null,
    to_date date not null
);
insert into salaries values(10003,85097,'2001-06-22','2002-06-22');
insert into salaries values(10001,88958,'1996-08-03','9999-01-01');
insert into salaries values(10002,72527,'1996-08-03','9999-01-01');
insert into salaries values(10003,32323,'1996-08-03','9999-01-01');

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1102921.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

开发万岳互联网医院APP:技术要点和关键挑战

随着移动技术和互联网的飞速发展&#xff0c;互联网医院APP成为医疗领域的一项重要创新。这些应用程序为患者和医生提供了更多便利和互动性&#xff0c;但开发互联网医院APP也伴随着一系列的技术要点和关键挑战。本文将探讨互联网医院APP的技术要点以及在开发过程中需要面对的挑…

AFL模糊测试+GCOV覆盖率分析

安全之安全(security)博客目录导读 覆盖率分析汇总 目录 一、代码示例 二、afl-cov工具下载 三、编译带覆盖率的版本并启动afl-cov 四、AFL编译插桩并运行afl-fuzz 五、结果查看 AFL相关详见AFL安全漏洞挖掘 GCOV相关详见GCOV覆盖率分析 现将两者结合&#xff0c;即进…

2023 编程资料合集汇总

资源合集 名称链接Rabbitmq精讲&#xff0c;项目驱动落地&#xff0c;分布式事务拔高资料https://www.aliyundrive.com/s/5VwmhTCPBNa程序员书籍大全https://www.aliyundrive.com/s/Kz5UiijQB7i后端Java教程&#xff08;学完直接去BAT&#xff09;https://www.aliyundrive.com…

STM32Cube高效开发教程<基础篇>(六)----FSMC连接TFT-LCD屏

声明:本人水平有限,博客可能存在部分错误的地方,请广大读者谅解并向本人反馈错误。    本专栏博客参考《STM32Cube高效开发教程(基础篇)》,有意向的读者可以购买正版书籍辅助学习,本书籍由王维波老师、鄢志丹老师、王钊老师倾力打造,书籍内容干货满满。 一、 FSMC连接…

【算法训练-回溯算法 一】【经典模版】全排列

废话不多说&#xff0c;喊一句号子鼓励自己&#xff1a;程序员永不失业&#xff0c;程序员走向架构&#xff01;本篇Blog的主题是【回溯算法】&#xff0c;使用【数组】这个基本的数据结构来实现&#xff0c;这个高频题的站点是&#xff1a;CodeTop&#xff0c;筛选条件为&…

支持语音与视频即时通讯项目杂记(一)

第一部分解释服务端的实现。 &#xff08;服务端结构&#xff09; 下面一个用于实现TCP服务器的代码&#xff0c;包括消息服务器&#xff08;TcpMsgServer&#xff09;和文件中转服务器&#xff08;TcpFileServer&#xff09;。 首先&#xff0c;TcpServer是TcpMsgServer和Tcp…

回文子串00

题目链接 回文子串 题目描述 注意点 s 由小写英文字母组成s 由小写英文字母组成1 < s.length < 1000具有不同开始位置或结束位置的子串&#xff0c;即使是由相同的字符组成&#xff0c;也会被视作不同的子串 解答思路 最初穷举所有的子串判断每个子串是否是回文子串…

C++标准库算法整理

目录 1、数值操作 1.1、std::accumulate 1.2、std::inner_product 1.3、std::partial_sum 1.4、std::exclusive_scan 1.5、std::inclusive_scan 1.6、std::reduce 2、相邻元素 2.1、std::adjacent_difference 2.2、std::adjacent_find 2.3、std::unique 2.4、std::u…

阿里云2023年双十一优惠活动整理

随着双十一的临近&#xff0c;阿里云也为大家准备了一系列优惠活动。作为国内知名的云服务提供商&#xff0c;阿里云在双十一期间推出了多种优惠政策和福利&#xff0c;让用户在享受优质云服务的同时&#xff0c;也能节省一些费用。本文将对阿里云双十一优惠活动进行详细整理&a…

合伙企业的执行事务合伙人委派代表是什么样的存在

当合伙企业的执行事务合伙人为法人或非法人组织时&#xff0c;通常会委派自然人代表其执行合伙事务&#xff0c;特别是各类投资基金、信托、资产证券化等合伙企业类型的SPV中&#xff0c;由法人执行事务合伙人委派代表执行合伙企业事务比较常见&#xff0c;由此可能出现合伙企业…

AFL安全漏洞挖掘

安全之安全(security)博客目录导读 ATF(TF-A)/OPTEE之FUZZ安全漏洞挖掘汇总 目录 一、AFL简介 二、AFL的安装 三、代码示例及种子语料库 四、AFL插桩编译 五、AFL运行及测试 六、AFL结果分析 一、AFL简介 模糊测试&#xff08;Fuzzing&#xff09;技术作为漏洞挖掘最有…

Compose 实战之为下拉刷新添加自定义指示器

前言 在安卓开发中&#xff0c;下拉刷新是一个非常常用的功能&#xff0c;几乎只要是涉及到列表展示数据的界面都会用到它。 而 Compose 却直到 2022年10月份才在 compose.material:1.3.0 中添加了对下拉刷新的支持&#xff1a;Modifier.pullRefresh 。 在此之前&#xff0c…

SpringBoot整合Activiti

SpringBoot集成Activiti7 SpringBoot版本使用2.7.16 <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.16</version><relativePath/> <!-- lookup…

高并发场景下常见的限流算法及方案介绍

应用场景 现代互联网很多业务场景&#xff0c;比如秒杀、下单、查询商品详情&#xff0c;最大特点就是高并发&#xff0c;而往往我们的系统不能承受这么大的流量&#xff0c;继而产生了很多的应对措施&#xff1a;CDN、消息队列、多级缓存、异地多活。 但是无论如何优化&…

element ui 中 el-button重新渲染后disabled属性失效

调试发现:disabled绑定的值和显示没有保持一致&#xff0c;发现是disabled属性失效 解决方式&#xff1a; 给标签添加key 比如&#xff1a;key“isOldVersion” <el-form-item><el-button type"primary" style"margin-left: 100px;" click"…

TX Text Control .NET Server for ASP.NET 32.0 Crack

TX Text Control .NET Server for ASP.NET 是VISUAL STUDIO 2022、ASP.NET CORE .NET 6 和 .NET 7 支持&#xff0c;将文档处理集成到 Web 应用程序中&#xff0c;为您的 ASP.NET Core、ASP.NET 和 Angular 应用程序添加强大的文档处理功能。 客户端用户界面 文档编辑器 将功能…

手撕 视觉slam14讲 ch7 / pose_estimation_3d2d.cpp (2)

上一篇文章中: 手撕ch7/pose_estimation_3d2d&#xff08;1&#xff09;&#xff0c;我们调用了epnp的方法进行位姿估计&#xff0c;这里我们使用非线性优化的方法来求解位姿&#xff0c;使用g2o进行BA优化 首先介绍g2o&#xff1a;可参考&#xff1a;g2o详细介绍 1.构建g2o图…

解决 MyBatis 一对多查询中,出现每组元素只有一个,总组数与元素数总数相等的问题

文章目录 问题简述场景描述问题描述问题原因解决办法 问题简述 笔者在使用 MyBatis 进行一对多查询的时候遇到一个奇怪的问题。对于笔者的一对多的查询结果&#xff0c;出现了这样的一个现象&#xff1a;原来每个组里有多个元素&#xff0c;查询目标是查询所查的组&#xff0c;…

【数据结构】线性表(一)线性表的定义及其基本操作(顺序表插入、删除、查找、修改)

目录 一、线性表 1. 线性表的定义 2. 线性表的要素 二、线性表的基本操作 三、线性表的顺序存储结构 1. 定义 2. 顺序表的操作 a. 插入操作 b. 删除操作 c. 查找操作 d. 修改操作 e. 代码实例 一、线性表 1. 线性表的定义 一个线性表是由零个或多个具有相同…

TCP/IP网络分层模型

TCP/IP当初的设计者真的是非常聪明&#xff0c;创造性地提出了“分层”的概念&#xff0c;把复杂的网络通信划分出多个层次&#xff0c;再给每一个层次分配不同的职责&#xff0c;层次内只专心做自己的事情就好&#xff0c;用“分而治之”的思想把一个“大麻烦”拆分成了数个“…