MySQL SQL100道基础练习题

1、客户端连接
mysql -uroot -h172.17.0.1 -P3306 -p123456
2、SQL分类
DDL,数据定义语言,用于创库表等。
DML,数据操作语言,用于增删改等。
DQL,数据查询语言,用于数据查询等。
DCL,数据控制语言,用于创建用户,权限控制等。
3、查询所有数据库
show databases;
4、查询当前数据库
select database();
5、创建一个itcast数据库, 使用数据库默认的字符集。
create database if not exists newdb;
6、创建一个itheima数据库,并且指定字符集。
create database if not exists newdb1 default charset utf8mb4;
7、删除itcast数据库。
drop database newdb1;
8、切换数据库。
use newdb;
9、查询当前数据库所有表。
show tables;
10、查看指定表结构。
desc score;
11、查询指定表的建表语句。
show create table score;
12、创建表结构
create table 表名(
    列名 类型 约束 comment '描述',
    ......
    列名 类型 约束 comment '描述'
);
13、设计一张员工信息表newtable,要求如下:
1、编号(纯数字)
2、员工工号(字符串类型,长度不超过10位)
3、员工姓名(字符串类型,长度不超过10位)
4、性别(男、女,存储一个汉字)
5、年龄(正常人年龄,不可能存储负数)
6、身份证号(二代身份证号码均为18位,身份证中有X这样的字符)
7、入职时间(取值年月日即可)
create table newtable(
    id int primary key auto_increment comment '编号',
    emp_id varchar(10) comment '员工工号',
    name varchar(10) comment '员工姓名',
    gender char(1) comment '员工性别',
    age tinyint unsigned comment '员工年龄',
    card_id char(18) comment '身份证号码',
    entrydate date comment '入职时间'
) comment '员工表';
14、为newtable表增加一个新的字段”昵称”为nickname,类型为varchar(20)新增字段,修改数据类型。
alter table newtable add nickname varchar(20);
15、将newtable表的nickname字段修改为username,类型为varchar(30)。
alter table newtable change nickname username varchar(30);
16、将newtable表的字段username删除。
alter table newtable drop username;
17、将newtable表的表名修改为 employee。
alter table newtable rename employee;
18、删除表。
drop table employee;
19、清空表的数据。用两个方法。
delete from newtable;
truncate table newtable;
20、给employee表所有的字段添加数据。
insert into newtable(id, emp_id, name, gender, age, card_id, entrydate)
values (1,'00001','张三丰','男',63,'12345678987456321X','1987-10-10'),
       (2,'00002','张翠山','男',48,'123256789874563214','2011-1-19'),
       (3,'00003','张无忌','男',26,null,'2020-1-19'),
       (4,'00004','赵敏','女',23,'123256789874563211','2012-1-19'),
       (5,'00005','郭襄','女',19,'123256789874563212','2013-1-19'),
       (6,'00006','韦一笑','男',51,'123256789874563213','2014-1-19'),
       (7,'00007','殷天正','男',47,'123256789874563215','2015-1-19'),
       (8,'00008','玄冥一','男',68,'123256789874563216','2016-1-19'),
       (9,'00009','周芷若','女',22,'123256789874563217','2017-1-19'),
       (10,'000010','灭绝','女',47,'123256789874563219','2018-1-19');
21、修改id为1的数据,将name修改为itheima。
update newtable set name = 'itheima' where id = 1;
22、修改id为1的数据, 将name修改为小昭, gender修改为 女。
update newtable set name = '小昭',gender = '女' where id = 1;
23、将所有的员工入职日期修改为 2008-01-01。
update newtable set entrydate = '2008-01-01';
24、删除gender为女的员工。
delete from newtable where gender = '女';
25、删除所有员工。
delete from newtable;
truncate table newtable;
26、查询指定字段 name, workaddress, age并返回。
select name,workaddress,age from newtable;
27、查询返回所有字段。
select * from newtable;
28、查询所有员工的工作地址,起别名。
select workaddress as '工作地址' from newtable;
29、查询公司员工的上班地址有哪些(不要重复)。
select distinct workaddress from newtable;
30、查询年龄等于30的员工信息.
select * from newtable where age <= 30;
31、查询年龄小于 20 的员工信息。
select * from newtable where age < 20;
32、查询年龄小于等于 20 的员工信息
select * from newtable where age <= 20;
33、查询没有身份证号的员工信息
select * from newtable where card_id is null;
34、查询有身份证号的员工信息
select * from newtable where card_id is not null;
35、查询年龄不等于30的员工信息
select * from newtable where age != 30;
36、查询年龄在15岁(包含) 到 20岁(包含)之间的员工信息
select * from newtable where age >= 15 and age <= 20;
select * from newtable where age between 15 and 20;
37、查询性别为 女 且年龄小于 25岁的员工信息
select * from newtable where gender = '女' and age < 25;
38、查询年龄等于18 或 20 或 40 的员工信息
select * from newtable where age = 18 or age = 20 or age =40;
select * from newtable where age in (18,20,40);
39、查询姓名为两个字的员工信息
select * from newtable where name like '__';
40、查询身份证号最后一位是X的员工信息
select * from newtable where card_id like '%x';
41、统计该企业员工数量
select count(*) from newtable;
42、统计该企业员工的平均年龄
select avg(age) from newtable;
43、统计该企业员工的最大年龄
select max(age) from newtable;
44、统计该企业员工的最小年龄
select min(age) from newtable;
45、统计西安地区员工的年龄之和
select sum(age) from newtable where workaddress = '西安';
46、根据性别分组 , 统计男性员工 和 女性员工的数量
select  gender,count(*) from newtable group by gender;
47、根据性别分组 , 统计男性员工 和 女性员工的平均年龄
select gender,avg(age) from newtable group by gender;
48、查询年龄小于45的员工 , 并根据工作地址分组 , 获取员工数量大于等于3的工作地址
select workaddress from newtable where age < 45 group by workaddress having count(workaddress) > 3;
49、统计各个工作地址上班的男性及女性员工的数量
select workaddress,gender,count(*) from newtable group by workaddress, gender;
50、根据年龄对公司的员工进行升序排序
select * from newtable order by age;
51、根据入职时间, 对员工进行降序排序
select * from newtable order by entrydate desc;
52、根据年龄对公司的员工进行升序排序 , 年龄相同 , 再按照入职时间进行降序排序
select * from newtable order by age asc ,entrydate desc ;
53、查询第1页员工数据, 每页展示10条记录
select * from newtable limit 0,10;
54、查询第2页员工数据, 每页展示10条记录 --------> (页码-1)*页展示记录数
select * from newtable limit 10,10;
55、查询年龄为20,21,22,23岁的员工信息。
select * from newtable where age in (20,21,22,23);
select * from newtable where age = 20 or age = 21 or age = 22 or age = 23;
56、查询性别为 男 ,并且年龄在 20-40 岁(含)以内的姓名为三个字的员工。
select * from newtable where gender = '男' and age between 20 and 40 and name like '___';
57、统计员工表中, 年龄小于60岁的 , 男性员工和女性员工的人数。
select gender,count(*) from newtable where age < 60 group by gender;
58、查询所有年龄小于等于35岁员工的姓名和年龄,并对查询结果按年龄升序排序,如果年龄相同按
入职时间降序排序。
select name,age from newtable where age <= 35 order by age asc,entrydate desc;
59、查询性别为男,且年龄在20-40 岁(含)以内的前5个员工信息,对查询的结果按年龄升序排序,
年龄相同按入职时间升序排序。
select * from newtable where age between 20 and 40 order by age,entrydate desc limit 5;
60、查询用户。
select * from mysql.user;
61、创建用户itcast, 只能够在当前主机localhost访问, 密码123456;
create user itcast@'localhost' identified by '123456';
62、创建用户heima, 可以在任意主机访问该数据库, 密码123456;
create user heima@'%' identified by '123456';
63、修改用户heima的访问密码为654321。
alter user heima@'%' identified by '654321';
64、删除 itcast@localhost 用户。
drop user itcast@'localhost';
65、查询 'heima'@'%' 用户的权限。
show grants for heima@'%';
66、授予 'heima'@'%' 用户itcast数据库所有表的所有操作权限。(好像只能在root@localhost权限下才能执行成功)
grant all on itcast.* to heima@'%';
67、撤销 'heima'@'%' 用户的itcast数据库的所有权限。(好像只能在root@localhost权限下才能执行成功)
revoke all on itcast.* from heima@'%';
68、字符串拼接,全部转小写,全部转大写,左填充,右填充,去除空格,截取子字符串
select lower(card_id) from newtable where card_id like '%X'; 转小写
select upper(card_id) from newtable where card_id like '%X'; 转大写
select concat(name,age) from newtable where id < 5; 拼接
69、由于业务需求变更,企业员工的工号,统一为8位数,目前不足5位数的全部在前面补0。比如: 1号员
工的工号应该为00000001。
select lpad(emp_id,8,'0') from newtable;
70、向上取整,向下取整,取模,获取随机数,四舍五入
select ceil(10.1);
select floor(10.9);
select mod(11,2);
select rand();
select round(10.59);
71、通过数据库的函数,生成一个六位数的随机验证码。
select substring(rand(),3,6);
72、当前日期,当前时间,当前日期和时间,当前年、月、日,增加指定的时间间隔,获取两个日期相差的天数。
select curdate();
select curtime();
select now();
select year(now());
select month(now());
select day(now());
select datediff(now(),'2024-07-01');
73、查询所有员工的入职天数,并根据入职天数倒序排序。
select datediff(now(),entrydate) as lift from newtable order by lift desc;
74、if,ifnull练习。
select if(card_id,'ok','bu ok') from newtable;
select ifnull('ok','bu ok') from newtable;
75、查询newtable表的员工姓名和工作地址 (北京/上海 ----> 一线城市 , 其他 ----> 二线城市)。
select name,
       case
       when workaddress = '北京' then '一线城市'
       when workaddress = '广东' then '一线城市'
       else '二线城市' end
from newtable;
76、为newtable表的dept_id字段添加外键约束,关联dept表的主键id。
alter table newtable add constraint new_dept foreign key (dept_id) references dept(id) on update cascade on delete cascade ;
77、删除newtable表的外键new_dept。
alter table newtable drop foreign key new_dept;
78、查询每一个员工的姓名,及关联的部门的名称(隐式内连接实现)。
select a.name,b.dept_name from newtable as a,dept as b where a.dept_id = b.id;
79、查询每一个员工的姓名 , 及关联的部门的名称 (显式内连接实现)。
select n.name,d.dept_name from newtable as n join dept as d on n.dept_id = d.id;
80、查询newtable表的所有数据, 和对应的部门信息,由于需求中提到,要查询emp的所有数据,所以是不能内连接查询的,需要考虑使用外连接查询。
select *,d.dept_name from newtable as n left join dept as d on n.dept_id = d.id;
81、查询dept表的所有数据, 和对应的员工信息(右外连接)
select d.*,n.* from dept as d right join newtable n on d.id = n.dept_id;
82、查询员工及其所属领导的名字
select a.name,b.name from newtable as a,newtable as b where a.manager = b.emp_id;
83、查询newtable所有员工及其领导的名字,如果员工没有领导,也需要查询出来。
select a.name,b.name from newtable as a left join newtable as b on a.manager = b.emp_id;
84、将薪资低于5000的员工,和年龄大于50岁的员工全部查询出来.联合查询,多方法.
select * from newtable where gz < 5000
union
select * from newtable where age > 50;
select * from newtable where age > 50 or gz < 5000;
85、查询 "市场部" 的所有员工信息。
select * from newtable where dept_id = (select id from dept where dept_name = '市场部');
86、查询在 "黄蓉" 入职之后的员工信息。
select * from newtable where entrydate > (select entrydate from newtable where name = '黄蓉');
87、查询 "行政部" 和 "市场部" 的所有员工信息
select * from newtable where dept_id in (select id from dept where dept_name in ('市场部','行政部'));
88、查询比 财务部 所有人工资都高的员工信息.
select * from newtable where gz > (select max(gz) from newtable where dept_id =  (select id from dept where dept_name = '财务部'));
select * from newtable where gz > all (select gz from newtable where dept_id = (select id from dept where dept_name = '财务部'));
89、查询比研发部其中任意一人工资高的员工信息
select * from newtable where gz > any (select gz from newtable where dept_id = (select id from dept where dept_name = '研发部'))
90、查询与 "张无忌" 的薪资及直属领导相同的员工信息
select * from newtable where (gz,manager) = (select gz,manager from newtable where name = '张无忌');
91、查询 "黄语焉" , "李嘉欣" 的职位和薪资
select name,(select dept_name from dept where id = dept_id) as '职位',gz from newtable where name in ('黄语焉','李嘉欣');
92、查询入职日期是 "2016-01-01" 之后的员工信息 , 及其部门信息
select *,(select dept_name from dept where id = dept_id) as '部门信息' from newtable where entrydate > '2016-01-01';
93、查询员工的姓名、年龄、部门信息 (隐式内连接)
select name,age,(select dept_name from dept where id = dept_id) as '部门信息' from newtable;
94、查询年龄小于30岁的员工的姓名、年龄、部门信息(显式内连接)
select a.name,a.age,b.dept_name from newtable as a join dept as b on a.dept_id = b.id where age < 30;
95、查询拥有员工的部门ID、部门名称
select dept_id,(select dept_name from dept where id = dept_id) as '部门' from newtable group by dept_id;
96、查询所有年龄大于40岁的员工, 及其归属的部门名称; 如果员工没有分配部门, 也需要展示出
来(外连接)
select a.name,b.dept_name from newtable as a left join dept as b on a.dept_id = b.id where a.age > 40;
97、查询所有员工的工资等级,低于5000,为普通员工,5000-9000为中层,9000以上为核心骨干
select name,
       case
        when gz < 5000 then '普通员工'
        when gz between 5000 and 9000 then '中层'
        else '核心骨干'
end  as '工资等级'
from newtable;
98、查询 "研发部" 所有员工的信息及工资等级
select name,
       case
        when gz < 5000 then '普通员工'
        when gz between 5000 and 9000 then '中层'
        else '核心骨干'
end  as '工资等级'
from newtable where dept_id = (select id from dept where dept_name = '研发部');
99、查询 "研发部" 员工的平均工资
select avg(gz) from newtable where dept_id = (select id from dept where dept_name = '研发部');
100、查询工资比 "灭绝" 高的员工信息。
select * from newtable where gz > (select gz from newtable where name = '灭绝');
101、查询比平均薪资高的员工信息
select * from newtable where gz > (select avg(gz) from newtable);
102、查询低于本部门平均工资的员工信息
select a.* from newtable as a where a.gz < (select avg(b.gz) from newtable as b where b.dept_id = a.dept_id);
103、查询所有的部门信息, 并统计部门的员工人数
select b.*,(select count(*) from newtable as a where a.dept_id = b.id) from dept as b;
104、查询所有员工的就职情况, 展示出员工名称, 年龄, 部门名称
select a.name,a.age,(select b.dept_name from dept as b where b.id = a.dept_id) as '部门名称' from newtable as a;

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

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

11.x86游戏实战-汇编指令add sub inc dec

免责声明&#xff1a;内容仅供学习参考&#xff0c;请合法利用知识&#xff0c;禁止进行违法犯罪活动&#xff01; 本次游戏没法给 内容参考于&#xff1a;微尘网络安全 上一个内容&#xff1a;10.x86游戏实战-汇编指令lea 首先双击下图红框位置 然后在下图红框位置输入0 然…

G2.【C语言】EasyX绘制颜色窗口

1.窗口 窗口&#xff1a;宽度*高度&#xff08;单位都是像素&#xff09; #include <stdio.h> #include <easyx.h> int main() {initgraph(640, 480);getchar();return 0; } 640是宽&#xff0c;480是高 2.操作窗口的三个按钮 #include <stdio.h> #incl…

数据库7.4

第二次作业 1.登陆数据库 2.创建数据库zoo 3.修改数据库zoo字符集为gbk 4.选择当前数据库为zoo 5.查看创建数据库zoo信息 6.删除数据库zoo C:\Windows\System32>mysql -uroot -p20040830Nmx mysql> create database zoo; alter database zoo character set gbk; mys…

基于Python爬虫的城市二手房数据分析可视化

基于Python爬虫的城市二手房数据分析可视化 一、前言二、数据采集(爬虫,附完整代码)三、数据可视化(附完整代码)3.1 房源面积-总价散点图3.2 各行政区均价3.3 均价最高的10个小区3.4 均价最高的10个地段3.5 户型分布3.6 词云图四、如何更换城市一、前言 二手房具有价格普…

茗鹤 | 如何借助APS高级计划排程系统提高汽车整车制造的效率

在我们做了详尽的市场调研及头部汽车制造企业排程需求沟通后&#xff0c;我们发现尽管企业有很多的业务系统做支撑&#xff0c;在计划排程领域&#xff0c;所有的汽车制造总装厂仍旧使用人工“Excel”做排产规划&#xff0c;其中少部分也会借助MRP、第三方辅助排产工具。鉴于我…

Python题解Leetcode Hot100之二叉树

1. 二叉树的中序遍历 题目描述 给定一个二叉树&#xff0c;返回它的中序遍历。解题思路 使用递归的方法对左子树进行中序遍历&#xff0c;然后访问根节点&#xff0c;最后对右子树进行中序遍历。也可以使用栈来模拟递归的过程&#xff0c;迭代地进行中序遍历。代码class Solut…

Leica Cyclone 3DR2024 一款功能强大的点云建模软件下载License获取

Leica Cyclone 3DR 2024 是一款功能强大的点云建模软件&#xff0c;使用旨在为用户提供全面的点云管理、自动化的点云分析&#xff0c;结合强大的建模&#xff0c;在一个直观友好的环境中&#xff0c;专注的完成挑战&#xff0c;提高生产力&#xff0c;轻松创建并交付专业的成果…

c++:struct和class的区别

C和C中struct的区别 (1)C中不支持成员函数&#xff08;只能通过函数指针成员变量间接支持&#xff09;&#xff0c;而C源生支持。 (2)C中不支持static成员&#xff0c;而C中支持。后面会详细讲&#xff0c;C static class是一个大知识点 (3)访问权限&#xff0c;C中默认public…

HTML5使用<mark>标签:高亮显示文本

1、<mark>标签的使用 mark 标签用于表示页面中需要突出显示或高亮的一段文本&#xff0c;这段文本对于当前用户具有参考作用。它通常在引用原文以引起读者注意时使用。<mark>标签的作用相当于使用一支荧光笔在打印的纸张上标出一些文字。它与强调不同&#xff0c;…

聊天广场(Vue+WebSocket+SpringBoot)

由于心血来潮想要做个聊天室项目 &#xff0c;但是仔细找了一下相关教程&#xff0c;却发现这么多的WebSocket教程里面&#xff0c;很多都没有介绍详细&#xff0c;代码都有所残缺&#xff0c;所以这次带来一个比较完整得使用WebSocket的项目。 目录 一、效果展示 二、准备工…

手写实现一个ORM框架

手写实现一个ORM框架 什么是ORM框架、ORM框架的作用效果演示框架设计代码细节SqlBuilderSqlExecutorStatementHandlerParameterHandlerResultSetHandler逆序生成实体类 大家好&#xff0c;本人最近写了一个ORM框架&#xff0c;想在这里分享给大家&#xff0c;让大家来学习学习。…

C++ 多态篇

文章目录 1. 多态的概念和实现1.1 概念1.2 实现1.2.1 协变1.2.2 析构函数1.2.3 子类虚函数不加virtual 2. C11 final和override3.1 final3.2 override 3. 函数重载、重写与隐藏4. 多态的原理5. 抽象类6.单继承和多继承的虚表6.1 单继承6.2 多继承 7. 菱形继承的虚表(了解)7.1 菱…

I/O多路复用

参考面试官&#xff1a;简单说一下阻塞IO、非阻塞IO、IO复用的区别 &#xff1f;_unix环境编程 阻塞io和非阻塞io-CSDN博客 同步阻塞(BIO) BIO 以流的方式处理数据 应用程序发起一个系统调用&#xff08;recvform&#xff09;&#xff0c;这个时候应用程序会一直阻塞下去&am…

Interview preparation--Https 工作流程

HTTP 传输的弊端 如上图&#xff0c;Http进行数据传输的时候是明文传输&#xff0c;导致任何人都有可能截获信息&#xff0c;篡改信息如果此时黑客冒充服务器&#xff0c;或者黑客窃取信息&#xff0c;则其可以返回任意信息给客户端&#xff0c;而且不被客户端察觉&#xff0c;…

2、图形验证码

1、图形验证码设计 1.1思路 现今&#xff0c;市面上的图形验证码付费的&#xff0c;免费的多种多样&#xff0c;主要形式有滑动拼图、文字点选、语序点选、字体识别、空间推理、智能随机等。 而处理也分为web端和sever端两部分 此处以免费的kaptcha 为例&#xff0c;进行数字图…

认识流式处理框架Apache Flink

目录 一、Apache Flink 的基础概念 1.1 Apache Flink是什么&#xff1f; 1.2 Flink的定义 二、Apache Flink 的发展史 2.1 Flink前身Stratosphere 2.2 Flink发展时间线及重大变更 三、Flink核心特性 3.1 批流一体化 3.2 同时支持高吞吐、低延迟、高性能 3.3 支持事件时…

全新UI自助图文打印系统小程序源码 PHP后端 附教程

最新自助图文打印系统和证件照云打印小程序源码PHP后端&#xff0c;为用户用户自助打印的服务&#xff0c;包括但不限于文档、图片、表格等多种格式的文件。此外&#xff0c;它们还提供了诸如美颜、换装、文档打印等功能&#xff0c;以及后台管理系统&#xff0c;方便管理员对打…

小(微)间距P1.538COB渠道现货销售将加速全面升级替换SMD产品。

COB&#xff08;Chip on Board&#xff09;技术&#xff0c;如一颗璀璨的星辰&#xff0c;在上世纪60年代的科技夜空中悄然升起。它巧妙地将LED芯片镶嵌在PCB电路板的怀抱中&#xff0c;再用特种树脂为其披上一层坚韧的外衣&#xff0c;宛如一位精心雕琢的艺术家在创作一幅完美…

实战whisper第三天:fast whisper 语音识别服务器部署,可远程访问,可商业化部署(全部代码和详细部署步骤)

Fast Whisper 是对 OpenAI 的 Whisper 模型的一个优化版本,它旨在提高音频转录和语音识别任务的速度和效率。Whisper 是一种强大的多语言和多任务语音模型,可以用于语音识别、语音翻译和语音分类等任务。 Fast Whisper 的原理 Fast Whisper 是在原始 Whisper 模型的基础上进…

从0到1:培训老师预约小程序开发笔记二

背景调研 培训老师预约小程序&#xff1a; 教师和学生可以更便捷地安排课程&#xff0c;并提升教学质量和学习效果&#xff0c;使之成为管理和提升教学效果的强大工具。培训老师可以在小程序上设置自己的可预约时间&#xff0c;学员可以根据老师的日程安排选择合适的时间进行预…