1. 程式人生 > 其它 >SQL server高階子查詢

SQL server高階子查詢

-- 高階查詢

-- 聚合函式
-- sum求和,count計數,avg平均值,max最大值,min最小值

-- 分組函式 (group by)
-- where寫在 group by 之前
-- order by 寫在 group by 後面
-- select 後面只能出現分組的依據和聚合函式

-- 聯表查詢 A,B
-- 左連線 left join
-- 右連線 right join
-- 全連線 full join
-- 內連線 inner join
-- select * from A inner join B
-- on A.sid=B.stu_id

------------------------------------------

create table stuInfo --學生表
(
stuNo varchar(6) not null primary key,
stuName varchar(10) not null,
stuSex varchar(2) not null,
stuAge int not null,
stuSeat int not null identity (1, 1),
strAddress varchar(255) default ('地址不詳')
);

create table stuMarks --成績表
(
ExamNo varchar(7) not null primary key,
stuNo varchar(6) not null references stuInfo (stuNo),
writtenExam int null,
LabExam int null
);

-- 外來鍵約束
-- 外來鍵的值必須是主鍵已經有的
-- 刪除主鍵的時候,外來鍵對應的資料會全部刪除
-- stuInfo(主表),stuMarks(從表)

insert into stuInfo(stuNo, stuName, stuSex, stuAge, strAddress)
select 's25301', '張秋麗', '男', 18, '北京海淀'
union
select 's25303', '李斯文', '女', 22, '河陽洛陽'
union
select 's25302', '李文才', '男', 85, '地址不詳'
union
select 's25304', '歐陽俊雄', '男', 28, '新疆'
union
select 's25318', '梅超風', '女', 23, '地址不詳';

insert into stuMarks(ExamNo, stuNo, writtenExam, LabExam)
select 's271811', 's25303', 93, 59
union
select 's271813', 's25302', 63, 91
union
select 's271816', 's25301', 90, 83
union
select 's271817', 's25318', 63, 53;

select * from stuinfo;
select * from stuMarks;

-- 聚合函式的使用

-- 學生表的男性的總人數
select count(*) as '男性人數' from stuInfo
where stuSex='男';

-- 男女的人數
select stuSex,count(*) from stuInfo group by stuSex;

-- 表關聯操作
select * from stuInfo a inner join stuMarks b
on a.stuNo=b.stuNo;

-- 將一個sql語句的結果作為條件來判斷:子查詢
-- 子查詢的語句,查詢的列只允許一個
-- 子查詢的語句,如果使用=,>,<,<=,>=結果必須只有一行
-- 如果使用in,那麼可以存在多行

-- 檢視年齡比“李斯文”大的學員
select * from stuinfo
where stuAge>
(select stuAge from stuInfo where stuName='李斯文');

-- 查詢出來李斯文的年齡
select stuAge from stuInfo where stuName='李斯文';

-- 檢視性別和“李斯文”一致的學員
select * from stuInfo where stuSex = (
select stuSex from stuInfo where stuName='李斯文'
);

-- 查詢“李斯文”的性別
select * from stuInfo where stuName='李斯文';

-- 刪除性別和“李斯文”一致的學員
delete from stuInfo where stuSex = (
select stuSex from stuInfo where stuName='李斯文'
);

-- 查詢年齡最大的學生資訊

-- 排序取第一
select top 1 * from stuinfo order by stuAge desc;

-- 查詢最大的年齡
select max(stuAge) from stuInfo;

-- 查詢年齡和最大年齡一致的
select * from stuInfo where stuAge=(
select max(stuAge) from stuInfo
);

--查詢年齡最小的學生資訊
select * from stuInfo where stuAge=(
select min(stuAge) from stuInfo
);

--查詢筆試成績成績最高的學生

-- 先查詢筆試成績最高的人是誰?
select top 1 stuNo from stuMarks order by writtenExam desc;

-- 查詢學生表,學生表的學號要和成績最高的那個學號相同
select * from stuInfo where stuNo=(
select top 1 stuNo from stuMarks order by writtenExam desc
);

-- 聯表
select top 1
a.stuNo,
b.stuNo,
stuName,
writtenExam
from stuInfo a inner join stuMarks b
on a.stuNo = b.stuNo order by writtenExam desc;

-- 查詢筆試成績大於全班筆試平均成績的學生記錄
-- 聚合函式不能用在where裡面
select * from stuInfo a inner join stuMarks b
on a.stuNo = b.stuNo
where writtenExam>(
select avg(writtenExam) from stuMarks
);

-- 檢視那些人沒有考試

-- 子查詢
select * from stuInfo where stuNo not in(
select stuNo from stuMarks
);

--聯表
select * from stuInfo a left join stuMarks b
on a.stuNo = b.stuNo where ExamNo is null;

select * from stuInfo;
select * from stuMarks;