select * from 表名 where 条件
条件 : and , between and , or , is null , is not null, distinct(不重复),unique,<,>= ,等等
子查询: where exit /not exit ( select 列 from 另一个表 where 条件),where 列 in ()
group by 列 : 分组语句在条件语句(where)order by 后面出现 asc 升序排序 desc 降序排序
having 为分组语句的限制条件 ,对分组进行限制,
eg : 查询成绩为空的学号,姓名 所在系和年龄按学好分组并放到一个新表
select sno ,age,name,sdept from into 新表名 student where exit (select * from sc where grade is null )group by sno desc
字符匹配条件: 关键词 like + 匹配串。 "_"匹配任意一个字符 ,"%" 匹配0 个或多个字符,[ ]:匹配[ ]中任意一个字符
select * from reader where book like "_[大小]%"
//表示匹配book第二个字是大或者小的书---"_ _"则为匹配第三个字符
top 8 返回前8行
计算函数
计算函数位于select 后面 不是条件
count(*): 统计表中的元组个数
count( distinct 列):统计列值的个数
sum(列):统计列值得总和
avg(列):统计某列的平均值
max(列):求出最大值 min(列)
表连接
内连接:将两个不同的表连接起来 关键字 表1 join 表2 on 连接条件(两个表的列都要满足连接条件)
自连接:相互连接的两个表物理上为同一张表 ,
外连接:只有一张表满足连接条件,可分为左右连接,左表一满足连接条件,右表二满足连接条件
全连接:左右外连接都满足,保留了两表不匹配的记录
交叉连接:关键字 cross join ,笛卡尔积--->第一个表的行数乘以第二个表的行数
集合查询
并关键字 union,交运算 ,intersect, 差运算,except
数据的操纵 插入和删除
将一个新记录插入表 insert into 表 (l列) value(列)
将子查询插入表,插入 insert into 表+ 子查询,列与值要对应,如下:
create table dep
(sdept char(15),
avgage smallint
)
insert into dep (sdept,avage)
select sdept ,avg(sage) from student group by sdept
删除数据使用delete 删除表用drop, 如下删除表中 计算机系不及格的记录