sql 語句練習與答案
更新時間:2013年06月02日 12:44:30 作者:
一些對初學者非常有用的練習,及練習的答案。希望可以給初學者一些幫助
1學生表student
S#學號,sname姓名,difdate日期,班級grade
2課程表 course
c#課程號 ,名字cname
3成績單score
s#學號 c#課程號 成績score
--1統(tǒng)計每個班級有多少人
select grade,count(sname) from ze_student group by grade;
--2、2007級的各學生的平均成績 沒有成績的為0;
select a.sname,(select avg(nvl(b.score,0)) from ze_score b where b.s#=a.s#) from ze_student a where grade=2007;
--3 每科 平均成績和最高成績 最低成績 2007級 保留2位小數(shù)點 四舍五入
select b.c#,avg(b.score),max(b.score),min(nvl(b.score,0)) from ze_student a,ze_score b where b.s# = a.s# and a.grade =2007 group by b.c#;
--4 給2007級 數(shù)學加5分
update ze_score
set score=nvl(score,0)+5
where s# in (select s# from ze_student where grade=2007) and c# =(select c# from ze_course where cname='數(shù)學');
--5 90分以上的為優(yōu)秀 90到85為良好,60分 不及格 各人平均成績
select s#, c,
case
when c>=90 then '優(yōu)秀'
when c<90 and c>=60 then '及格'
else '不及格' end as jige
from (select s#,avg(nvl(score,0)) as c from ze_score group by s# ) order by jige desc;
--6 求同月出生的 人數(shù)
select to_char(difdate,'mm') as 月份,count(s#) as 出生人數(shù) from ze_student group by to_char(difdate,'mm');
--7 各科的及格率和平均成績 截取 保留2位
--及格率
select c#,avg(nvl(score,0))as 平均成績,sum(nvl(score,0))as 總成績, count(s#) as 各科人數(shù),
trunc(sum(
case when nvl(score,0)>60 then '1'
else '0' end)/count(s#),2) as 及格率
from ze_score group by c#;
--每人的及格率
select s#, avg(nvl(score,0))as 平均成績,sum(nvl(score,0))as 總成績, count(c#) as 總科目,
sum(
case when nvl(score,0)>60 then 1
else 0 end
)/count(c#) as 及格率
from ze_score group by s#;
--8刪除 姓名是張三 的大學語文 成績
select * from ze_score where s# in (select s# from ze_student where sname in '張三') and c#=(select c# from ze_course where cname ='大學語文');
--9 將數(shù)學替換成高等數(shù)學
update ze_course set cname='高等數(shù)學'where cname like '%數(shù)學%';
--10 格式化 ,顯示 將學號修改成S開頭 不足12位補0;
--查詢
select concat('S',lpad(s#,11,0)) as s# from ze_score ;
select concat('S',lpad(s#,11,0)) as s# from ze_student ;
--格式化
update ze_score set s#= concat('S',lpad(s#,9,0));
update ze_student set s#= concat('S',lpad(s#,9,0));
四個足球隊
select a.name,b.name from qiu a,qiu b where a.name<b.name;
commit
rollback
服務器類型
服務器協(xié)議
全局數(shù)據(jù)庫名稱
服務器IP地址
服務器端口號
用戶名和密碼
S#學號,sname姓名,difdate日期,班級grade
2課程表 course
c#課程號 ,名字cname
3成績單score
s#學號 c#課程號 成績score
--1統(tǒng)計每個班級有多少人
select grade,count(sname) from ze_student group by grade;
--2、2007級的各學生的平均成績 沒有成績的為0;
select a.sname,(select avg(nvl(b.score,0)) from ze_score b where b.s#=a.s#) from ze_student a where grade=2007;
--3 每科 平均成績和最高成績 最低成績 2007級 保留2位小數(shù)點 四舍五入
select b.c#,avg(b.score),max(b.score),min(nvl(b.score,0)) from ze_student a,ze_score b where b.s# = a.s# and a.grade =2007 group by b.c#;
--4 給2007級 數(shù)學加5分
update ze_score
set score=nvl(score,0)+5
where s# in (select s# from ze_student where grade=2007) and c# =(select c# from ze_course where cname='數(shù)學');
--5 90分以上的為優(yōu)秀 90到85為良好,60分 不及格 各人平均成績
select s#, c,
case
when c>=90 then '優(yōu)秀'
when c<90 and c>=60 then '及格'
else '不及格' end as jige
from (select s#,avg(nvl(score,0)) as c from ze_score group by s# ) order by jige desc;
--6 求同月出生的 人數(shù)
select to_char(difdate,'mm') as 月份,count(s#) as 出生人數(shù) from ze_student group by to_char(difdate,'mm');
--7 各科的及格率和平均成績 截取 保留2位
--及格率
select c#,avg(nvl(score,0))as 平均成績,sum(nvl(score,0))as 總成績, count(s#) as 各科人數(shù),
trunc(sum(
case when nvl(score,0)>60 then '1'
else '0' end)/count(s#),2) as 及格率
from ze_score group by c#;
--每人的及格率
select s#, avg(nvl(score,0))as 平均成績,sum(nvl(score,0))as 總成績, count(c#) as 總科目,
sum(
case when nvl(score,0)>60 then 1
else 0 end
)/count(c#) as 及格率
from ze_score group by s#;
--8刪除 姓名是張三 的大學語文 成績
select * from ze_score where s# in (select s# from ze_student where sname in '張三') and c#=(select c# from ze_course where cname ='大學語文');
--9 將數(shù)學替換成高等數(shù)學
update ze_course set cname='高等數(shù)學'where cname like '%數(shù)學%';
--10 格式化 ,顯示 將學號修改成S開頭 不足12位補0;
--查詢
select concat('S',lpad(s#,11,0)) as s# from ze_score ;
select concat('S',lpad(s#,11,0)) as s# from ze_student ;
--格式化
update ze_score set s#= concat('S',lpad(s#,9,0));
update ze_student set s#= concat('S',lpad(s#,9,0));
四個足球隊
select a.name,b.name from qiu a,qiu b where a.name<b.name;
commit
rollback
服務器類型
服務器協(xié)議
全局數(shù)據(jù)庫名稱
服務器IP地址
服務器端口號
用戶名和密碼
相關文章
將Reporting services的RDL文件拷貝到另外一臺機器時報Data at the root level i
在本機開發(fā)了一個Reporting后拷貝到服務器,然后在Sql Server Business Intelligence Development Studio中添加再打開后會報Data at the root level is invalid.錯誤2012-06-06SQL?Server使用T-SQL進階之公用表表達式(CTE)
這篇文章介紹了SQL?Server中T-SQL的公用表表達式(CTE),文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05