31.select sname,ssex,sbirthday from student
union
select tname,tsex,tbirthday from teacher數據庫
32.select sname,ssex,sbirthday from student where ssex='女'
union
select tname,tsex,tbirthday from teacher where tsex='女'ide
33. select * from score a where degree<(select avg(degree) from score b where b.cno=a.cno)函數
34.select tname,depart from teacher where tno in(select tno from course where cno in (select distinct cno from score))排序
35.select tname,depart from teacher where tno not in(select tno from course where cno in (select distinct cno from score))get
36.select class from student where ssex='男' group by class having count(*) >1it
37.select * from student where sname not like '王%'io
38.select sname,YEAR(getdate())-YEAR(sbirthday) from studenttable
39.select max(sbirthday),min(sbirthday) from studentclass
40.select * from student order by class desc,sbirthday asc擴展
41.select cname from course where tno in(select tno from teacher where tsex='男')
select tname,cname from course,teacher where course.tno = teacher.tno and teacher.tsex='男'
42. select * from score where degree = (select max(degree) from score)
select top 1 * from score order by degree desc
43.select sname from student where ssex=(select ssex from student where sname='李軍')
44.select sname from student where ssex=(select ssex from student where sname='李軍') and class = (select class from student where sname='李軍')
45.select * from score where sno in(select sno from student where ssex='男') and cno in(select cno from course where cname='計算機導論')
1.數據庫操做
create database 數據庫名稱
drop database 數據庫名稱
use 數據庫名稱
go 兩條SQL語句之間分隔
2.表操做
create table 表名( 列名 類型 其它,列名 id類型 其它 )
primary key 主鍵
identity 自增加列
not null 非空
unique 惟一
references 外鍵 references 主表名(主表主鍵列)
drop table 表名
3.數據操做
增長:
insert into 表名 values(每一列的值)
insert into 表名(列名) values(值)
修改:
update 表名 set 列名=值,列名=值 where 篩選條件
刪除:
delete from 表名 where 篩選條件
查詢:
1.簡單查詢
select * from 表名
select 列名 from 表名
select 列名 as 別名 from 表名
2.條件查詢 where or and
select * from 表名 where 條件1
select * from 表名 where 條件1 or 條件2
select * from 表名 where 條件1 and 條件2
3.範圍查詢 between and
select * from 表名 where 列名 between 值1 and 值2
4.離散查詢 in not in
select * from 表名 where 列名 in(數據列表)
5.模糊查詢 like %任意多個字符 _任意一個字符
select * from 表名 where 列名 like ‘%_’
6.排序查詢 order by desc降序 asc升序
select * from 表名 order by 列名
7.分組查詢 group by having
select * from 表名 group by 列名 having 條件
8.分頁查詢 top n 取前n個值
select top n * from 表名
9.去重查詢 distinct
select distinct 列名 from 表名
10.聚合函數(統計函數)
select count(*) from 表名
select sum(列名) from 表名
select avg(列名) from 表名
select max(列名) from 表名
select min(列名) from 表名
高級查詢:
1.鏈接查詢
列的擴展 join on
2.聯合查詢
行的擴展 union
3.子查詢無關子查詢 子查詢和父查詢沒有必定的關係,子查詢能夠單獨執行相關子查詢子查詢執行的時候必須使用父查詢的內容做爲條件