一、使用sql的基本查询语句完成下面的练习:
![cb2e5104e0de51ddbb5b79510d364f8a.png](https://i-blog.csdnimg.cn/blog_migrate/95649f02ce7e08f4935cb54c4d5ef7c7.jpeg)
- 从学生表中查询出“姓名”、“性别”两列;
- 查询学生表中的全部列;
- 从学生表中查询出“姓名”、“性别”两列,并将这两列的别名分别设定为:“s_name”、“人类性别”;
- 查询学生表中的“姓名”,并剔除重复的数据;
- 查询学生表中的“学号”和“姓名”,并剔除重复的数据。
右击“查询”,点击“新建查询”,在弹出的“查询编辑器”中输入下面的sql语言,再点击“运行”:
![d34a6bd5520620c4ba07a60241853690.png](https://i-blog.csdnimg.cn/blog_migrate/232e5af5f9b06e9e3ddd2ae5d2f1dca4.jpeg)
二、理解sql的运行顺序
案例:
![fc73a434d4d02b54f82ed8bd791d98d4.png](https://i-blog.csdnimg.cn/blog_migrate/f8112d3ec30af596f58ba6a54e35fc07.jpeg)
在上面这个案例中,sql语句的运行顺序为:
![06fe0cd60e24ee12459e964a97c5c43c.png](https://i-blog.csdnimg.cn/blog_migrate/656af26f2f18a64e26669a7fefe4511c.jpeg)
所以我们应该这样理解sql的运行顺序:
![c783f5aba34606c2983f40bdaee23868.png](https://i-blog.csdnimg.cn/blog_migrate/e55450342673d3ddbd427ed2ff7ac67c.jpeg)
三、学习sql中的算数运算符和比较运算符,完成下面的练习:
![ad42fdcc24a66a50ec4b0c4b46823667.png](https://i-blog.csdnimg.cn/blog_migrate/f31bc7ba9a16f84025f49229166d0c89.jpeg)
- 查询成绩表score中的“学号”、“成绩”、“成绩/100”,并将“成绩/100”这一列设置别名为“百分比成绩”;
- 查询学生表student中“姓名”、“学号”,要求“姓名”为“猴子”;
- 查询成绩表score中的“学号”、“成绩”,要求“成绩”小于60;
- 查询学生表student中的“姓名”、“出生日期”,要求“出生日期”在“1990-01-01”之前;
- 查询教师表teacher中的“教师号”、“教师姓名”,要求“教师姓名”为空;
- 查询教师表teacher中的“教师号”、“教师姓名”,要求“教师姓名”非空。
![52f59f29bf6960fbda15442437d92520.png](https://i-blog.csdnimg.cn/blog_migrate/fc1241f78da288b43fbecd4f6a79069a.jpeg)
右击“查询”,点击“新建查询”,在弹出的“查询编辑器”中输入下面的sql语言,再点击“运行”:
![d05e5f4819e34f3dcf24e5d22d5f95dc.png](https://i-blog.csdnimg.cn/blog_migrate/50f72ef9e5ca60dc466883984c690ef0.jpeg)
![36c7ad163fde05196ca702d0a3470f98.png](https://i-blog.csdnimg.cn/blog_migrate/56245a11b110506254061ce306c3abdd.jpeg)
四、学习sql中的逻辑运算符,完成下面的练习:
![535319f0a844f0a0c246e6062b2a020c.png](https://i-blog.csdnimg.cn/blog_migrate/27bc18f99bfe7ce97857dcb8d9af46ec.jpeg)
- 查询成绩表score中的“学号”、“成绩”,要求“成绩”大于等于60且小于等于90;
- 查询成绩表score中的“学号”、“成绩”,要求“成绩”小于60或者大于90;
- 查询学生表student中的“姓名”、“性别”,要求“性别”是“男”,并且“姓名”是“猴子”或“马云”;
- 查询学生表student中的“姓名”、“性别”,要求“性别”是“男”,并且“姓名”不是“猴子”也不是“马云”。
![52f59f29bf6960fbda15442437d92520.png](https://i-blog.csdnimg.cn/blog_migrate/fc1241f78da288b43fbecd4f6a79069a.jpeg)
右击“查询”,点击“新建查询”,在弹出的“查询编辑器”中输入下面的sql语言,再点击“运行”:
![45cb8022ab2c66d67f69a006a0e1b6df.png](https://i-blog.csdnimg.cn/blog_migrate/675bc53fc2b0e7f059215c57a899bcdd.jpeg)
![4c867086f48a3337e79d9778b23f889a.png](https://i-blog.csdnimg.cn/blog_migrate/a68b2ccdf1a9a6c44af9a26894ed4c86.png)
注:练习1和练习3使用了两种查询方法。
五、学习sql的字符串模糊查询—like,完成下面的练习:
- 查询学生表student中姓“猴”的学生名单;
- 查询学生表student中姓名最后一个字是“猴”的学生名单;
- 查询学生表student中姓名中带“猴”的学生名单。
![cb2e5104e0de51ddbb5b79510d364f8a.png](https://i-blog.csdnimg.cn/blog_migrate/95649f02ce7e08f4935cb54c4d5ef7c7.jpeg)
右击“查询”,点击“新建查询”,在弹出的“查询编辑器”中输入下面的sql语言,再点击“运行”:
![418c68cca95528f32782ff072abb13a8.png](https://i-blog.csdnimg.cn/blog_migrate/fbc23c481b5deb5f05f58623f6aa142f.jpeg)
六、完成sqlzoo平台select basics中的练习题1-4
练习题网址为:SELECT basics/zh
-- 1.显示德国Germany的人口
select population
from world
where name='Germany';
-- 2.查询面积为5,000,000以上平方公里的国家,对每个国家显示她的名字和人均国内生产总值(gdp/population)
select name,gdp/population
from world
where area>5000000;
-- 3.显示“爱尔兰(Ireland)”,“冰岛(Iceland)”,“丹麦(Denmark)”的国家名称和人口
select name,population
from world
where name in ('Ireland','Iceland','Denmark');
-- 4.显示面积为200,000及250,000之间的国家名称和该国面积
select name,area
from world
where area between 200000 and 250000;
七、完成sqlzoo平台select from world中的练习题1-8
练习题网址为:SQLZOO:SELECT from WORLD Tutorial/zh
-- 1.显示所有国家,所在州名称和国家人口
select name,continent,population
from world;
-- 2.显示人口至少为2亿的国家/地区的名称
select name
from world
where population>=200000000;
-- 3.找出人口大于2亿的国家名字和人均GDP
select name,gdp/population
from world
where population>200000000;
-- 4.查找属于南美洲(South America)的国家名称,并将人口除以100万,以获得数百万人口数
select name,population/1000000
from world
where continent='South America';
-- 5.查找法国,德国,意大利(France, Germany, Italy)的国家名称和人口
select name,population
from world
where name in ('France','Germany','Italy');
-- 6.查找国家名称中包含“United”的国家
select name
from world
where name like '%United%';
-- 7.找出大国的名称,人口和面积。(注:大国是指一个国家面积超过300万平方公里,或者人口超过2.5亿)
select name,population,area
from world
where area>3000000 or population>250000000;
/*
8.找出符合下面条件国家的名称,人口和面积。
条件:人口(大于250000000)或者面积(大于3000000)大的国家,排除同时面积大而且人口大的国家
*/
select name,population,area
from world
where (population>250000000 and area<=3000000)
or (population<=250000000 and area>3000000);
八、完成sqlzoo平台select names中的练习题1-11
练习题网址为:SELECT names/zh - SQLZOO
-- 1.找出以 Y 字母开头的国家名称
select name
from world
where name like 'Y%';
-- 2.找出以 Y 字母结尾的国家名称
select name
from world
where name like '%Y';
-- 3.找出所有国家名字中包括字母x的国家名称
select name
from world
where name like '%x%';
-- 4.找出所有国家,其名字以 land 作結尾
select name
from world
where name like '%land';
-- 5.找出所有国家,其名字以 C 开始,以 ia 结尾
select name
from world
where name like 'C%ia';
-- 6.找出所有国家,其名字包括字母oo
select name
from world
where name like '%oo%';
-- 7.找出所有国家,其名字包括三个或以上的a
select name
from world
where name like '%a%a%a%';
-- 8.找出所有国家,其名字以t作为第二个字母
select name
from world
where name like '_t%';
-- 9.找出所有国家,其名字都有两个字母o,被另外两个字母相隔着
select name
from world
where name like '%o__o%';
-- 10.找出所有国家,其名字都是 4 个字母的
select name
from world
where name like '____';
-- 11.查找所有国家的名字,其首都和国家名字是相同的
select name
from world
where name=capital;