自测题:
1.从student表中查询姓名和性别,并且姓名改成别名name, 性别改成别名男或女
2.如何删除重复值?例如如何只显示姓名的唯一值?如何显示姓名和学号的唯一值?
3.运行顺序是怎样的?select 姓名,成绩 from student where 姓名='猴子';
4.单行注释方法以及多行注释方法
5.5+Null等于什么
6.显示world表中的国家,人口,gdp和人均gdp
7.为什么查询结果会出现10<2?如何解决?
8.如何查询出空值?如何查询出非空值?
例如从student中查询姓名和成绩,查询出的成绩为空值;查询出的成绩不为空值。
9.如何查询以猴开头的名字、以猴结尾的名字、包含猴的名字、以猴开头的三个字的名字、以猴开头的两个字的名字
10.查询姓名为猴子或马云的姓名和成绩(两种写法)
11.查找出性别是男并且姓名是猴子或者马云的人名和成绩
12.查找出成绩大于等于60,小于等于90的学生(两种写法)
13.查询面积大的国家或者人口多的国家,但是不能同时是面积大且人口多。
面积大的定义:more than 3 million ,人口多的定义:more than 250 million
显示国家名字,人口,面积。
14.如何对小数点后的位数进行四舍五入
查询'South America' 中的国家,人口以百万的形式显示,gdp以十亿的形式显示
并且对显示的值进行四舍五入,保留小数点后两位。
(Show the name and population in millions and the GDP in billions for the countries of the continent 'South America'. Use the ROUND function to show the values to two decimal places.Divide by 1000000 (6 zeros) for millions. Divide by 1000000000 (9 zeros) for billions.)
15.如何对千位进行四舍五入
Show the name and per-capita GDP for those countries with a GDP of at least one trillion (1000000000000; that is 12 zeros). Round this value to the nearest 1000.
Show per-capita GDP for the trillion dollar countries to the nearest $1000.
16.如何计算字符数
Show the name and capital where the name and the capital have the same number of characters.
17.如何将字符串的首个字符提取出来
Show the name and the capital where the first letters of each match. Don't include countries where the name and the capital are the same word.
自测题答案:
1.从student表中查询姓名和性别,并且姓名改成别名name, 性别改成别名男或女
改成别名用as
select 姓名 as name,性别 as '男或女' from student;
2.如何删除重复值?例如如何只显示姓名的唯一值?如何显示姓名和学号的唯一值?
删除重复值可通过在列名前加distinct。若是两个列名组成的唯一值,只能在第一个列名前加distinct.
select distinct 姓名 from student;
Select distinct 姓名,学号 from students;
3.运行顺序是怎样的?select 姓名,成绩 from student where 姓名='猴子';
select子句最后运行,其他子句的运行顺序按照书写顺序进行运行
From student :找到student这个表
Where 姓名='猴子':在这个表中把姓名叫猴子的行全部找出来
Select 姓名,成绩 :在这些姓名叫猴子的行中,只显示出姓名列和姓名对应的成绩列
4.单行注释方法以及多行注释方法
-- select * from student; (单行注释)
/* select * from student;
Select * from teacher; */
5.5+Null等于什么
5+Null=Null
所有含有空值的运算,结果为空值
6.显示world表中的国家,人口,gdp和人均gdp
select name, population, gdp, gdp/population from world
7.为什么查询结果会出现10<2?如何解决?
可能原因是,数据类型设置成了字符串类型而非数值类型
10这个字符串是以1字符串开头,1字符串小于2, 所以会返回10<2
比较运算符可以和所有类型的值进行比较
8.如何查询出空值?如何查询出非空值?
Is null/is not null
例如从student中查询姓名和成绩,查询出的成绩为空值;查询出的成绩不为空值。
select 姓名,成绩 from student where 成绩 is null;
select 姓名,成绩 from student where 成绩 is not null;
9.如何查询以猴开头的名字、以猴结尾的名字、包含猴的名字、以猴开头的三个字的名字、以猴开头的两个字的名字
%可代表任意个数的字符
_ 仅可代表1个字符
like表示模糊查询
select 姓名 from student where 姓名 like '猴%';
select 姓名 from student where 姓名 like '%猴';
select 姓名 from student where 姓名 like '%猴%';
select 姓名 from student where 姓名 like '猴__';
select 姓名 from student where 姓名 like '猴_';
10.查询姓名为猴子或马云的姓名和成绩(两种写法)
select 姓名,成绩 from student where 姓名 = '猴子' or 姓名 = '马云';
select 姓名,成绩 from student where 姓名 in ('猴子','马云');
11.查找出性别是男并且姓名是猴子或者马云的人名和成绩
() 括号扩起来的是优先运算
Select 姓名,成绩 from student
Where 性别='男' and (姓名='猴子' or 姓名='马云');
12.查找出成绩大于等于60,小于等于90的学生 (两种写法)
Between....and 包括两边的边界值
Select 姓名,成绩 from student where 成绩 >= 60 and 成绩<= 90;
Select 姓名,成绩 from student where 成绩 between 60 and 90;
13.查询面积大的国家或者人口多的国家,但是不能同时是面积大且人口多。
面积大的定义:more than 3 million, 人口多的定义:(more than 250 million
显示国家名字,人口,面积。
Not 对条件进行否定
Select name, population, area from world
where (area > 3000000 or population >250000000)
and not (area > 3000000 and population >250000000);
14.如何对小数点后的位数进行四舍五入
用round函数
查询'South America' 中的国家,人口以百万的形式显示,gdp以十亿的形式显示
并且对显示的值进行四舍五入,保留小数点后两位。
(Show the name and population in millions and the GDP in billions for the countries of the continent 'South America'. Use the ROUND function to show the values to two decimal places.Divide by 1000000 (6 zeros) for millions. Divide by 1000000000 (9 zeros) for billions.)
Select name, round(population/1000000,2), round(gdp/1000000000, 2)
from world
where continent='South America' ;
15.如何对千位进行四舍五入
用round函数,第二参数大于0是对小数点后的位数进行四舍五入,第二参数小于0是对小数点前的位数进行四舍五入
Show the name and per-capita GDP for those countries with a GDP of at least one trillion (1000000000000; that is 12 zeros). Round this value to the nearest 1000.
Show per-capita GDP for the trillion dollar countries to the nearest $1000.
Select name, round(gdp/population, -3)
from world
where gdp >=1000000000000;
16.如何计算字符数
用Length函数
Show the name and capital where the name and the capital have the same number of characters.
Select name, capital
from world
where length(name)=length(capital);
17.如何将字符串的首个字符提取出来
用left函数
Show the name and the capital where the first letters of each match. Don't include countries where the name and the capital are the same word.
Select name, capital
from world
where left(name,1)=left(capital,1)
and name<>capital;
sqlzoo题目的截图















