一. 练习用表展示
Student
![c826c0aebd69e9abe0c87bcbfafbc1c8.png](https://i-blog.csdnimg.cn/blog_migrate/cc0cee6ebdd3a7f8ba694bafdce9b268.jpeg)
Course
![b9653b0cf66b0d6778c58abaa375fbb7.png](https://i-blog.csdnimg.cn/blog_migrate/77dbc6ea78d27a182120945f1960b79b.jpeg)
Score
![9a9d8910fc0dcc6fd9f25e3dfb445a3c.png](https://i-blog.csdnimg.cn/blog_migrate/cd016cd4bd1f4bcbc3d8f233689a70b0.jpeg)
teacher
![904d3b44c46f177a8c9b14eedb16f76e.png](https://i-blog.csdnimg.cn/blog_migrate/5592f25bb0a0ade58749b3a772e3d9c8.png)
二.练习
1.select查询
展示查询语句及结果
-- 查询学生表
![ec862e9743145bb09f446663b2f40c68.png](https://i-blog.csdnimg.cn/blog_migrate/a001344c144a55abfed8c9f67a4b4ada.png)
-- 查询学生表所有信息
select *
from student;
![772cb07dce27d2b5345d34e7aac01af8.png](https://i-blog.csdnimg.cn/blog_migrate/5ee516cc60ad99e78ad8d783335bf115.jpeg)
-- 查询姓名,性别并自定义别名(as的用法)
select 姓名 as s_name,性别 as '人类性别'
from student;
![467787d4222a2be69b8e8a9fec437144.png](https://i-blog.csdnimg.cn/blog_migrate/49b1129d3cfb9160b9d6082e46f89432.png)
-- 查找不重复姓名(distinct用法)
select DISTINCT 姓名
from student;
![baca063d481072af78622e18e4fab23a.png](https://i-blog.csdnimg.cn/blog_migrate/dcdc79121177ca56ef582df6af01e6d5.png)
-- 查找多列不重复(姓名 学号)
select DISTINCT 学号,姓名
from student;
![dbe2531bdf8305fb01137ce5ff51f62c.png](https://i-blog.csdnimg.cn/blog_migrate/99a6b14f0f1a5036dbd5d7ece1505034.png)
2.算术运算符查询练习
展示查询语句及结果
-- 使用运算符查询
-- 求成绩百分比
SELECT 学号,成绩,成绩/100 as '百分比成绩'
from score;
![f1625cde5fddddad01627495ca65635d.png](https://i-blog.csdnimg.cn/blog_migrate/c2daf44e45669c9771e39f3f27d10f5e.jpeg)
-- 比较运算符号(=)
SELECT 姓名,学号
from student
where 姓名='猴子';
![ec3b3e08e9e5deedc8be596cce92ce16.png](https://i-blog.csdnimg.cn/blog_migrate/1c9f6991eed36427848f3b7ee69da21a.png)
![90b9e76bcdc8d4fc56deec9072e50903.png](https://i-blog.csdnimg.cn/blog_migrate/5328b123a8e0d81bfc0ebc1f95c339d5.png)
![6b2e950187488df5e0642a9ba7c3a90e.png](https://i-blog.csdnimg.cn/blog_migrate/59480aeabc1b9755b36e42e9c75d0de9.png)
-- 查询NULL值
select 教师号,教师姓名
from teacher
where 教师姓名 is NULL;
![12c193aaedda059d40767b2e08819cd3.png](https://i-blog.csdnimg.cn/blog_migrate/dcebea3b0c5c29a9699b87d424e0c4d5.png)
-- 查询非null值
select 教师号,教师姓名
from teacher
where 教师姓名 is not NULL;
![4bb564b6198f5e2d728d013f56b710fe.png](https://i-blog.csdnimg.cn/blog_migrate/04df5a5f3cd47271e71ee9e6cfe5ce84.png)
3.逻辑运算符查询练习
展示查询语句及结果
-- 逻辑运算符not
SELECT 学号,成绩
FROM score
where not 成绩>=60;-- 成绩小于60
![6e244d7bdc0899414fbd126553959b67.png](https://i-blog.csdnimg.cn/blog_migrate/8a88a121818b17483c66aea5a9923284.png)
-- 逻辑运算符and
select 学号,成绩
from score
where 成绩>=60 and 成绩<=90; -- 成绩 between 60 and 90;
![24969ab74095beb561db5216e047e36e.png](https://i-blog.csdnimg.cn/blog_migrate/18e1107d86d707d19fdc16380f513cbf.png)
-- 逻辑运算符or
select 姓名,性别
from student
where 性别='男'
and (姓名='马云'or 姓名= '猴子');-- 性别男,姓名为马云或者猴子
![855b4998bfae3a25ed7b0e0ec40bfe1e.png](https://i-blog.csdnimg.cn/blog_migrate/05805688be3d62b00df291cf23ea1748.png)
-- 范围查询between
select 学号,成绩
from score
where 成绩 between 60 and 90;
![66799f4ca26536a7997f2b29a8ea8d3a.png](https://i-blog.csdnimg.cn/blog_migrate/be7445de0d106969e66bac329364998d.png)
select 学号,成绩
from score
where 成绩 <60 OR 成绩 >90;-- 小于60,大于90的成绩都会被选
![f8c0c8d3d6b2eddb68edbefae038b66f.png](https://i-blog.csdnimg.cn/blog_migrate/3550bc7d895c61ad801dfb6e8c2a995c.png)
-- in 的用法
select 姓名,性别
from student
where 性别='男'
and 姓名 in ('马云', '猴子');
![457245d453628504b7cb5a89fa15a117.png](https://i-blog.csdnimg.cn/blog_migrate/fe988aa3419f5a0851f8a7c4082a32ae.png)
-- not in
select 姓名,性别
from student
where 性别='男'
and 姓名 not in ('马云', '猴子');
![3727c469bf6ab7a28b73d0d4025db6f6.png](https://i-blog.csdnimg.cn/blog_migrate/d1b5a65b6edbc4804a63d45f11bfa4b7.png)
4.模糊查询练习
展示查询语句及结果
-- 查找姓猴的学生名单
select *
from student
where 姓名 like '猴%';
![6cea9d0af76189d0a70f92c33e95128c.png](https://i-blog.csdnimg.cn/blog_migrate/ea4033ba22ade8fcbd6cd276f7889e71.jpeg)
-- 查找名字最后一个字是猴的学生名单
select *
from student
where 姓名 like '%猴';
![9442c20123d02d6104c767553b9aff2f.png](https://i-blog.csdnimg.cn/blog_migrate/f1eb0704d77274708c8d88e50ac9f543.jpeg)
-- 查找姓名中带猴的名字的学生名单
SELECT*
FROM student
WHERE 姓名 like'%猴%';
![4d5a392ef0b35588442ad50830a88ed5.png](https://i-blog.csdnimg.cn/blog_migrate/5f94ab8d44b2a0acd8ad80f71ec941c3.jpeg)