2024年 经典sql命令面试题汇总 (学习篇一)

不说废话,直接上干货!
1. 首先打开要保证本地已经装了mysql, 然后打开终端,启动mysql  如下:

 2. 然后创建一个库,在库里面创建对应的表结构和数据(也可自行修改表结构和数据),

CREATE TABLE students (
studentNo int(10) primary key auto_increment,
name varchar(10),
sex varchar(1),
hometown varchar(20),
age int(4),
class varchar(10),
card varchar(20)
);
INSERT INTO students (name, sex, hometown, age, class, card)
VALUES (' 王昭君 ', ' ', ' 北京 ', '20', '1 ', '340322199001247654'),
(' 诸葛亮 ', ' ', ' 上海 ', '18', '2 ', '340322199002242354'),
( ' 张飞 ', ' ', ' 南京 ', '24', '3 ', '340322199003247654'),
( ' 白起 ', ' ', ' 安徽 ', '22', '4 ', '3403221 99005247654'),
( ' 大乔 ', ' ', ' 天津 ', '19', '3 ', '340322199004247654'),
(' 孙尚香 ', ' ', ' 河北 ', '18', '1 ', '340322199006247654'),
( ' 百里玄策 ', ' ', ' 山西 ', '20', '2 ', '340322199007247654'),
( ' 小乔 ', ' ', ' 河南 ', '15', '3 ', NULL),
( ' 百里守约 ', ' ', ' 湖南 ', '21', '1 ', ''),
( ' 妲己 ', ' ', ' 广东 ', '26', '2 ', '340322199607247654'),
( ' 李白 ', ' ', ' 北京 ', '30', '4 ', '340322199005267754'),
( ' 孙膑 ', ' ', ' 新疆 ', '26', '3 ', '340322199000297655');
CREATE TABLE courses (
courseNo int(10) PRIMARY KEY AUTO_INCREMENT,
name varchar(10)
);  
INSERT INTO courses
VALUES ('1', ' 数据库 '),
('2', 'qtp'),
('3', 'linux'),
('4', ' 系统测试 '),
('5', ' 单元测试 '),
('6', ' 测试过程 ');
CREATE TABLE scores (
id int(10) PRIMARY KEY AUTO_INCREMENT,
courseNo int(10),
studentNo int(10),
score int(4)
);
INSERT INTO scores
VALUES ('1', '1', 1, '90'),
('2', '1', 2, '75'),
('3', '2', 2, '98'),
('4', '3', 1, '86'),

1、查询学生"百里守约"的基本信息

select * from students where name='百里守约' ;

2、查询学生"百里守约"或”百里玄策”的基本信息

select * from students where name=' 百里守约 ' or name=' 百里玄策 ' ;

3、查询姓"张"学生的姓名,年龄,班级

select name,age,class from students where name like '%' ;

4、查询姓名中含有"约"字的学生的基本信息

select * from students where name like '%%' ;

5、查询姓名长度为三个字,姓“孙”的学生的学号,姓名,年龄,班级,身份证号

select studentNo,name,age,class,card from students where name like '__' ;

6、查询姓"百"或者姓”孙”的学生的基本信息

select * from students where name like '%' or name like '%' ;

7、查询姓"百"并且家乡是"山西"的学生信

select * from students where name like '%' and hometown='山西' ;

8、查询家乡是"北京"、”新疆”、”山东”或者"上海"的学生的信息

select * from students where hometown in('北京','新疆','山东','上海') ;

9、查询姓"孙",但是家乡不是"河北"的学生信息

select * from students where name like '%' and hometown!='河北' ;

10、查询家乡不是"北京"、"新疆"、"山东"、"上海"的学生的信息

select * from students where hometown not in('北京','新疆','山东','上海');

11、查询全部学生信息,并按照“性别”排序

select * from students order by sex ;

12、查询现有学生都来自于哪些不同的省份

select hometown from students group by hometown ;

13、查询所有男生,并按年龄升序排序

select * from students where sex='' order by age ;

14、统计共有多少个学生

select count(*) from students ;

15、统计年龄大于20岁的学生有多少个

select count(*) from students where age>20 ;

16、统计男生的平均年龄

select avg(age) from students where sex='';

17、查询1班学生中的最大年龄是多少

select max(age) from students where class='1';

18、统计2班男女生各有多少人

select sex,count(*) from students where class='2' group by sex ;

19、统计每个班级中每种性别的学生人数,并按照班级升序排序

select class,sex,count(*) from students group by class,sex order by class;

  • 22
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞⑧风暴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值