高级查询在数据库中用得是最频繁的,也是应用最广泛、最普遍的。
Ø 基本常用查询
-- all 查询所有,几乎从来不用 all 关键字,因为是默认关键字
select all sex from student;
-- distinct 过滤重复 (常用语检查一列数据是否有异常值)
select distinct sex from student;
-- count 统计
select count(distinct sex) from student;
-- top 取前N条记录
select top 3 * from student;
-- column 列运算
select (age + id) col from student;
select s.name + '-' + c.name from classes c, student s where s.cid = c.id;
-- having 分组过滤条件
-- 按照年龄分组,过滤年龄为空的数据,并且统计分组的条数和现实年龄信息
select count(*), age from student group by age having age is not null;
-- 按照年龄和cid组合分组,过滤条件是cid大于1的记录
select count(*), cid, sex from student group by cid, sex having cid > 1;
-- 按照年龄分组,过滤条件是分组后的记录条数大于等于2
select count(*), age from student group by age having count(age) >= 2;
-- 按照cid和性别组合分组,过滤条件是cid大于1,cid的最大值大于2
select count(*), cid, sex from student group by cid, sex having cid > 1 and max(cid) > 2;