建表語句就不寫了,這個是基操吧
- 查詢平均成績大於等於 60 分的同學的學生編號和學生姓名和平均成績。
select student.*,t1.avgscore
from student inner JOIN(
select sc.SId ,AVG(sc.score)as avgscore
from sc
GROUP BY sc.SId
HAVING AVG(sc.score)>=60)as t1 on student.SId=t1.SId
- 查詢在 SC 表存在成績的學生信息。
select DISTINCT student.*
from student ,sc
where student.SId=sc.SId
- 查詢「李」姓老師的數量。
select count(*)
from teacher
where teacher.Tname like '李%
- 查詢學過「張三」老師授課的同學的信息。
select student.*
from teacher ,course ,student,sc
where teacher.Tname='張三'
and teacher.TId=course.TId
and course.CId=sc.CId
and sc.SId=student.SId
- 查詢沒有學全所有課程的同學的信息。
- 解法1
select student.*
from sc ,student
where sc.SId=student.SId
GROUP BY sc.SId
Having count(*)<(select count(*) from course)
但這種解法得出來的結果不包括什麼課都沒選的同學。
- 解法2
select DISTINCT student.*
from
(select student.SId,course.CId
from student,course ) as t1 LEFT JOIN (SELECT sc.SId,sc.CId from sc)as t2 on t1.SId=t2.SId and t1.CId=t2.CId,student
where t2.SId is null
and t1.SId=student.SId
利用笛卡爾積可以把什麼課都沒選的同學查詢出來
- 查詢至少有一門課與學號爲" 01 "的同學所學相同的同學的信息。
select DISTINCT student.* from sc ,student where sc.CId in (select CId from sc where sc.SId='01') and sc.SId=student.SId
- 查詢和" 01 "號的同學學習的課程 完全相同的其他同學的信息。
select DISTINCT student.*
from (
select student.SId,t.CId
from student ,(select sc.CId from sc where sc.SId='01') as t) as t1 LEFT JOIN sc on t1.SId=sc.SId and t1.CId=sc.CId,student
where sc.SId is null
and t1.SId=student.SId
- 查詢沒學過"張三"老師講授的任一門課程的學生姓名。
select *
from student
where student.SId not in
(
select student.SId
from student left join sc on student.SId=sc.SId
where EXISTS
(select *
from teacher ,course
where teacher.Tname='張三'
and teacher.TId=course.TId
and course.CId=sc.CId))
- 查詢兩門及其以上不及格課程的同學的學號,姓名及其平均成績。
select student.SId,student.Sname,avg(sc.score) from student ,sc where student.SId=sc.SId and sc.score<60 GROUP BY sc.SId HAVING count(*)>=2
- 統計各科成績各分數段人數:課程編號,課程名稱,[100-85],[85-70],[70-60],[60-0] 及所佔百分比
-
這個是網上找的一些,自己還不能編寫。
select t1.*,round(t1.num/t2.all_num*100,2) || '%' 百分比
from
(select m.cid , m.Cname ,
(case
when n.score >= 85 then '85-100'
when n.score >= 70 and n.score < 85 then '70-85'
when n.score >= 60 and n.score < 70 then '60-70'
else '0-60'
end) as px,
count(1) num
from Course m , sc n
where m.cid = n.cid
group by m.cid , m.Cname , (
case when n.score >= 85 then '85-100'
when n.score >= 70 and n.score < 85 then '70-85'
when n.score >= 60 and n.score < 70 then '60-70'
else '0-60'
end)
order by m.cid , m.Cname , px) t1,
(select m.cid , m.Cname ,
count(1) all_num
from Course m , sc n
where m.cid = n.cid
group by m.cid , m.Cname
order by m.cid , m.Cname) t2
where t1.cid=t2.cid
- 求每門課程的學生人數。
select sc.CId,count(*) as 學生人數 from sc GROUP BY sc.CId
- 查詢不同課程成績相同的學生的學生編號、課程編號、學生成績。
select *
from sc as t1
where exists(select * from sc as t2 where t1.SId=t2.SId and t1.CId!=t2.CId and t1.score =t2.score )
- 查詢每門功成績最好的前兩名。
select *
from sc as t1
where (select count(*) from sc as t2 where t1.CId=t2.CId and t2.score >t1.score)<2
ORDER BY t1.CId
- 檢索至少選修兩門課程的學生學號。
select DISTINCT t1.SId from sc as t1 where(select count(*) from sc where t1.SId=sc.SId)>=3
- 查詢選修了全部課程的學生信息。
select student.*
from sc ,student
where sc.SId=student.SId
GROUP BY sc.SId
HAVING count(*) = (select DISTINCT count(*) from course )
- 存儲過程及函數練習
- 創建一個存儲過程,以學生編號爲參數,輸出該學生的年齡。
create or replace procedure p_sxt1(v_empno in emp.empno%type, v_sal out emp.sal%type) is
begin
select sal into v_sal from emp where empno = v_empno;
end;
--(1)執行
declare
v_empno emp.empno%type := 7369;
v_sal emp.sal%type;
begin
p_sxt1(v_empno,v_sal);
dbms_output.put_line(v_empno || ' 員工的工資爲:' || v_sal);
end;
創建一個存儲過程,以員工號爲參數,輸出該員工的工資
--Mysql方法: DELIMITER // CREATE PROCEDURE myproc(IN id varchar(10),OUT Sage datetime) BEGIN SELECT Student .Sage INTO Sage FROM Student where Sid = id; END // DELIMITER ;
調用: SET @p_id = ‘01’; SET @p_datetime = now(); CALL myproc(@p_id, @p_datetime); SELECT @p_datetime; |
- 創建一個存儲過程,以課程編號爲參數,輸出不及格的學生信息。
create or replace procedure test60(cnumber in integer)
is
cursor student_cursor is select distinct S.* from Student S,sc where S.sid = sc.sid and sc.score < 60 and sc.cid = cnumber;
T_Student student_cursor%ROWTYPE;
//定義的是結果集中的每一行的數據類型,在後續的結果提取當中,這個會成提取出來的對象。
begin
open student_cursor;
loop
fetch student_cursor into T_Student;
exit when student_cursor%notfound;
dbms_output.put(T_Student.Sid);
dbms_output.put(T_Student.Sname);
dbms_output.put(T_Student.Sage);
dbms_output.put_line(T_Student.Ssex);
end loop;
close student_cursor;
end test60;
創建一個存儲過程,以員工號爲參數,返回該員工的工作年限(以參數形式返回)。
- 創建一個函數,以課程編號爲參數,返回該課程的平均成績。
create or replace function f_sxt6(v_deptno emp.deptno%type) return emp.sal%type is
vr_sal emp.sal%type;
begin
select avg(sal) into vr_sal from emp where deptno = v_deptno;
return vr_sal;
end;
--(6)執行
select f_sxt6(20) 部門平均工資 from dual;
創建一個函數,以部門號爲參數,返回該部門的平均工資。
--Mysql方法:
- DELIMITER //
- CREATE FUNCTION GetAvgScoreByID(cid VARCHAR(10))
- RETURNS decimal
(
18,
1)
- BEGIN
- RETURN(SELECT avg(score) FROM course WHERE CId=cid);
- END//
- DELIMITER ;
調用:
SET @cid =’01’;
SELECT GetAvgScoreByID(@cid);
- 觸發器練習
-
創建一個行級觸發器CASCADE_DEL_UPD,當刪除課程表中某門課程時,成績表中該門課程的成績也一併刪除。create or replace trigger CASCADE_DEL_UPD
before update of deptno or delete
on dept
for each row
declare
begin
if deleting then
delete from emp where deptno=:old.deptno;
end if;
end;
-
--Mysql方法:
DELIMITER ||
CREATE TRIGGER demo BEFORE DELETE
ON Course FOR EACH ROW
BEGIN
If deleting then
delete from SC where CId = OLD.CId
END
||
DELIMITER ;