select 列
from 表
where 条件
group by 条件
having 条件
order by 条件
limit
查询表中所有的数据
SELECT * FROM city;
查询表中某几列数据
SELECT Name ,Population FROM city;
查询中国所有的城市名和人口数
SELECT Name,Population FROM city WHERE countrycode='CHN';
世界上小于100人的城市名和人口数
SELECT Name,Population FROM city WHERE population<100;
查询中国人口数量大于1000w的城市名和人口
SELECT Name,Population FROM city WHERE countrycode='CHN' AND Population>8000000;
查询一下contrycode中带有CH开头,城市信息
SELECT * FROM city WHERE countrycode LIKE 'CH%';
将某列中有共同条件的数据行,分成一组,然后在进行聚合函数操作
统计中国 每个省的总人口数
SELECT district, SUM(population) FROM city WHERE countrycode='CHN' GROUP BY district ;
统计所有国家的总人口数量,将总人口数大于5000w的过滤出来,并且按照从大到小顺序排列
SELECT countrycode,SUM(population) FROM city GROUP BY countrycode HAVING SUM(population)>50000000 ORDER BY SUM(population) DESC ;
LIMIT M,N:跳过前M行,从M+1行开始显示N行
LIMIT Y OFFSET X:跳过X行,显示一共Y行
SELECT countrycode,COUNT(DISTINCT district) FROM city GROUP BY countrycode;
%:表示任意0个或多个字符。可匹配任意类型和长度的字符