sql 語(yǔ)句練習(xí)與答案
更新時(shí)間:2013年06月02日 12:44:30 作者:
一些對(duì)初學(xué)者非常有用的練習(xí),及練習(xí)的答案。希望可以給初學(xué)者一些幫助
1學(xué)生表student
S#學(xué)號(hào),sname姓名,difdate日期,班級(jí)grade
2課程表 course
c#課程號(hào) ,名字cname
3成績(jī)單score
s#學(xué)號(hào) c#課程號(hào) 成績(jī)score
--1統(tǒng)計(jì)每個(gè)班級(jí)有多少人
select grade,count(sname) from ze_student group by grade;
--2、2007級(jí)的各學(xué)生的平均成績(jī) 沒(méi)有成績(jī)的為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 每科 平均成績(jī)和最高成績(jī) 最低成績(jī) 2007級(jí) 保留2位小數(shù)點(diǎn) 四舍五入
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級(jí) 數(shù)學(xué)加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ù)學(xué)');
--5 90分以上的為優(yōu)秀 90到85為良好,60分 不及格 各人平均成績(jī)
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 各科的及格率和平均成績(jī) 截取 保留2位
--及格率
select c#,avg(nvl(score,0))as 平均成績(jī),sum(nvl(score,0))as 總成績(jī), 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 平均成績(jī),sum(nvl(score,0))as 總成績(jī), 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刪除 姓名是張三 的大學(xué)語(yǔ)文 成績(jī)
select * from ze_score where s# in (select s# from ze_student where sname in '張三') and c#=(select c# from ze_course where cname ='大學(xué)語(yǔ)文');
--9 將數(shù)學(xué)替換成高等數(shù)學(xué)
update ze_course set cname='高等數(shù)學(xué)'where cname like '%數(shù)學(xué)%';
--10 格式化 ,顯示 將學(xué)號(hào)修改成S開(kāi)頭 不足12位補(bǔ)0;
--查詢(xún)
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));
四個(gè)足球隊(duì)
select a.name,b.name from qiu a,qiu b where a.name<b.name;
commit
rollback
服務(wù)器類(lèi)型
服務(wù)器協(xié)議
全局?jǐn)?shù)據(jù)庫(kù)名稱(chēng)
服務(wù)器IP地址
服務(wù)器端口號(hào)
用戶(hù)名和密碼
S#學(xué)號(hào),sname姓名,difdate日期,班級(jí)grade
2課程表 course
c#課程號(hào) ,名字cname
3成績(jī)單score
s#學(xué)號(hào) c#課程號(hào) 成績(jī)score
--1統(tǒng)計(jì)每個(gè)班級(jí)有多少人
select grade,count(sname) from ze_student group by grade;
--2、2007級(jí)的各學(xué)生的平均成績(jī) 沒(méi)有成績(jī)的為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 每科 平均成績(jī)和最高成績(jī) 最低成績(jī) 2007級(jí) 保留2位小數(shù)點(diǎn) 四舍五入
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級(jí) 數(shù)學(xué)加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ù)學(xué)');
--5 90分以上的為優(yōu)秀 90到85為良好,60分 不及格 各人平均成績(jī)
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 各科的及格率和平均成績(jī) 截取 保留2位
--及格率
select c#,avg(nvl(score,0))as 平均成績(jī),sum(nvl(score,0))as 總成績(jī), 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 平均成績(jī),sum(nvl(score,0))as 總成績(jī), 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刪除 姓名是張三 的大學(xué)語(yǔ)文 成績(jī)
select * from ze_score where s# in (select s# from ze_student where sname in '張三') and c#=(select c# from ze_course where cname ='大學(xué)語(yǔ)文');
--9 將數(shù)學(xué)替換成高等數(shù)學(xué)
update ze_course set cname='高等數(shù)學(xué)'where cname like '%數(shù)學(xué)%';
--10 格式化 ,顯示 將學(xué)號(hào)修改成S開(kāi)頭 不足12位補(bǔ)0;
--查詢(xún)
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));
四個(gè)足球隊(duì)
select a.name,b.name from qiu a,qiu b where a.name<b.name;
commit
rollback
服務(wù)器類(lèi)型
服務(wù)器協(xié)議
全局?jǐn)?shù)據(jù)庫(kù)名稱(chēng)
服務(wù)器IP地址
服務(wù)器端口號(hào)
用戶(hù)名和密碼
相關(guān)文章
將Reporting services的RDL文件拷貝到另外一臺(tái)機(jī)器時(shí)報(bào)Data at the root level i
在本機(jī)開(kāi)發(fā)了一個(gè)Reporting后拷貝到服務(wù)器,然后在Sql Server Business Intelligence Development Studio中添加再打開(kāi)后會(huì)報(bào)Data at the root level is invalid.錯(cuò)誤2012-06-06SQL Server存儲(chǔ)過(guò)程入門(mén)學(xué)習(xí)
存儲(chǔ)過(guò)程(Stored Procedure),是一組為了完成特定功能的SQL 語(yǔ)句,集經(jīng)編譯后存儲(chǔ)在數(shù)據(jù)庫(kù)中,用戶(hù)通過(guò)指定存儲(chǔ)過(guò)程的名字并給出參數(shù),如果該存儲(chǔ)過(guò)程帶有參數(shù)來(lái)執(zhí)行2013-08-08SQL?Server使用T-SQL進(jìn)階之公用表表達(dá)式(CTE)
這篇文章介紹了SQL?Server中T-SQL的公用表表達(dá)式(CTE),文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05SQLServer數(shù)據(jù)庫(kù)的各種管理方法
這篇文章主要介紹了SQLServer數(shù)據(jù)庫(kù)的各種管理方法,需要的朋友可以參考下2015-10-10