當前位置:首頁 » 編程語言 » sql練習題

sql練習題

發布時間: 2022-06-14 16:01:40

sql資料庫練習題

1.DISTINCT、top
2.convert
3.查詢、更新、管理
4.主鍵、外鍵
5.ROLLBACK TRAN、COMMIT TRAN
6.sp_renamedb
8.identity
9.插入數據的列數必須和表中列數相等
10.空
12.truncate
14.原子性、一致性、隔離性、永久性
16.count、avg、len、substring
17.cast
18.windows
19.物理數據表
20.<>、!=

⑵ SQL練習題

一 學生 – 課程資料庫
1 查詢 7號課程沒有考試成績的學生學號
select sno from sc where cno=』7』 and grade is not null
2 查詢 7號課程成績在90分以上或60分以下的學生學號
select sno from sc where grade>90 or grade<60
3 查詢課程名以「數據」兩個字開頭的所有課程的課程號和課程名。
Select cno,cname from c where cname like 『數據%』
4 查詢每個學生所有課程的平均成績,輸出學生學號、平均成績
select sno,avg(grade) from sc group by sno
5 查詢每門課程的選修人數,輸出課程號、選修人數。
Select cno,count(*) from sc group by cno
6 查詢選修 7號課程的學生的學號、姓名、性別。
Select s.sno, sname,ssex from s , sc where s.sno=sc.sno and cno = 『7』
7 查詢選修7號課程學生的平均年齡。
Select avg(sage) from s , sc where s.sno=sc.sno and cno = 『7』
8 查詢由30名以上學生選修的課程號。
Select sno from sc group by cno having count(*)>30
9 查詢至今沒有考試不及格的學生學號
a: select sno from s where sno not in ( select sno from sc where grade<60 )
b: select sno from sc group by sno having min(grade)>=60

1 找出選修課程號為 C2 的學生學號與成績。
Select sno,grade from sc where cno=』C2』
2 找出選修課程號為C4 的學生學號與姓名。
Select s.sno , sname from s,sc where s.sno=sc.sno and cno=』C4』
3 找出選修課程名為 Maths 的學生學號與姓名。
Select s.sno ,sname from s,sc,c
where s.sno=sc.sno and c.cno=sc.cno and cname = 『Maths』
4找出選修課程號為C2或C4 的學生學號。
Select distinct sno from sc where cno in (『C2』,』C4』)
或: Select distinct sno from sc where cno=』C2』 or cno =』C4』
5找出選修課程號為C2和C4 的學生學號。
Select sno from sc where cno =』C2』 and sno in (
select sno from sc where cno = 『C4』 )
6 找出不學C2課程的學生姓名和年齡
select sname , sage from s where sno not in ( select sno from sc where cno=』C2』 )
或:
select sname , sage from s where not exists ( select * from sc where sc.sno=s.sno and cno=』C2』 )
7 找出選修了資料庫課程的所有學生姓名。(與3同)
Select s.sno ,sname from s,sc,c
where s.sno=sc.sno and c.cno=sc.cno and cname = 『資料庫』

8 找出資料庫課程不及格的女生姓名
嵌套:
select sname from s where ssex = 『女』 and sno in ( select sno from sc where grade<60 and cno in ( select cno from c where cname=』資料庫』) )
連接:
Select sname from s,sc,c
where s.sno=sc.sno and c.cno=sc.cno and ssex=』女』 and cname = 『資料庫』 and grade<60
9 找出各門課程的平均成績,輸出課程名和平均成績
select cname , avg(grade) from sc , c where c.cno =sc.cno group by sc.cno
10找出各個學生的平均成績,輸出學生姓名和平均成績
select sname , avg(grade) from s , sc where s.sno=sc.sno group by sc.sno
11 找出至少有30個學生選修的課程名
select cname from c where cno in ( select cno from sc group by cno having count(*)>=30 )
12 找出選修了不少於3門課程的學生姓名。
Select sname from s where sno in ( select sno from sc group by sno having count(*)>=3)
13 找出各門課程的成績均不低於90分的學生姓名。
Select sname from s where sno not in ( select sno from sc where grade<90)
14* 找出資料庫課程成績不低於該門課程平均分的學生姓名。
Select sname from s where sno in (
Select sno from sc , c where sc.cno=c.cno and cname=』資料庫』 and
Grade > (Select avg(grade) from sc , c where sc.cno=c.cno and cname=』資料庫』 ) )
15 找出各個系科男女學生的平均年齡和人數。
Select sdept,ssex , avg(sage) , count(*) from s
Group by sdept , ssex
16 找出計算機系(JSJ)課程平均分最高的學生學號和姓名。
Select sc.sno , sname from s, sc where s.sno=sc.sno and sdept=』JSJ』
Group by sc.sno Having avg(grade) =
( Select top 1 avg(grade) from sc, s where s.sno=sc.sno and sdept=』JSJ』
group by sc.sno order by avg(grade) DESC )
三 客戶 – 商品資料庫中包括3按各表:KH,FP,YWY
1 查詢工資在 1000 到3000 元之間的男性業務員的姓名和辦公室編號。
Select Yname , Ono from YWY where salary between 1000 and 3000 and Ysex=』男』
2 查詢各個辦公室的業務員人數,輸出辦公室編號和對應的人數。
Select Ono , count(*) from YWY group by Ono
3 查詢每個客戶在2002年5月購買的總金額,輸出客戶號和相應的總金額。
Select Kno,sum(Fmoney) from FP where fdate between 『2002.5.1』 and 『2002.5.31』
Group by Kno
4 查詢2002年5月購買次數超過5次的所有客戶號,且按客戶號升序排序。
Select Kno from FP where fdate between 『2002.5.1』 and 『2002.5.31』
Group by Kno having count(*)>5
Order by Kno ASC
5 查詢各辦公室男性和女性業務員的平均工資。
Select Ono,Ysex ,avg(salary) from YWY group by Ono , Ysex
6 查詢2002年5月曾經在王海亮業務員手中購買過商品的客戶號、客戶姓名、聯系電話。
Select Kno,Kname,phone from KH where Kno in (
Select kno from FP where fdate between 『2002.5.1』 and 『2002.5.31』 and
Yno=(select Yno from YWY where Yname = 『王海亮』 )
7 查詢所有工資比1538號業務員高的業務員的編號、姓名、工資。
Select yno ,Yname, salary from YWY where salary >
( Select salary from YWY where Yno=』1538』 )
8 查詢所有與1538號業務員在同一個辦公室的其他業務員的編號、姓名。
Select Yno , Yname from YWY where Yno<>』1538』 and Ono in (
Select Ono from YWY where Yno=』1538』 )
9 查詢銷售總金額最高的業務員的編號。
Select Yno from FP Group By Yno Having sum(Fmoney) =
(Select top 1 sum(Fmoney) from FP group by Yno ORDER BY sum(Fmoney) DESC)
10 查詢所有業務員的編號、姓名、工資以及工資比他高的其他業務員的平均工資。
利用自身連接
Select y1.Yno ,y1.Yname ,y1.salary , avg( y2. salary) from YWY y1 , YWY y2
Where y1.Yno<>y2.Yno and y1.salary < y2.salary
Group by y1.Yno
Sno salary sno salary
1 100 1 100
2 120 2 120
3 90 3 90
4 110 4 110
四 某中學資料庫中由一張表:
學生選課表:由板及代碼、班內學號、姓名、科目、成績五個屬性組成,關系模式為
SC(BJDM,BNXH,XSXM,KM,CJ) ,其中(BJDM,BNXH)為主碼。
說明:每個學生每門科目存放一個記錄,科目有「語文」、「數學」、「外語」三門。
1 找出每個班級的班級代碼、學生人數、平均成績。
Select BJDM,count(*) ,avg(CJ) from SC group by BJDM
2 找出每個學生的班級代碼、學生姓名、考試科目數、總成績。
Select BJDM,XSXM,count(*) , sum(CJ) from SC
Group by BNXH
3 輸出一張表格,每位學生對應一條記錄,包括:班級代碼、姓名、語文成績、數學成績、外語成績。
方法一:利用視圖
create view v1 (bjdm,xsxm, yw,sx,wy ) AS
select bjdm , xsxm , cj , 0,0 from sc where km=』語文』
union
select bjdm , xsxm , 0 , cj,0 from sc where km=』數學』
union
select bjdm , xsxm , 0,0,cj from sc where km=』外語』

select bjdm, xsxm , sum(yw) as 語文, sum(sx) as 數學, sum(wy) as 外語 from v1 group by bjdm, xsxm
方法二:自身連接
select a.bjdm,a.xsxm , a.km,a.cj , b.km,b.cj , c.km,c.cj from sc a , sc b , sc c
where a.bjdm=b.bjdm and a.bnxh= b.bnxh and b.bjdm=c.bjdm and b.bnxh= c.bnxh
and a.km=』語文』 and b.km=』數學』 and c.km=』外語』
方法三:利用存儲過程(略)
4 輸出一張表格:由成績低於60分的每位學生對應一條記錄,包括欄位:班級代碼、姓名、最低成績。
Select bjdm,xsxm ,min(CJ) from sc where grade<60 group by bjdm,xsxm
5輸出一張表格:由成績低於60分的每位學生對應一條記錄,包括欄位:班級代碼、姓名、最高成績、平均成績。
得到平均成績:create view V1 (bjdm,bnxh ,avg_cj) AS
select bjdm,bnxh ,avg(cj) from sc where bjdm , bnxh
select sc.bjdm,sc.xsxm ,max(cj) , avg_cj from sc , V1
where sc.bjdm=v1.bjdm and sc.bnxh=V1.bnxh and cj<60
group by sc.bjdm,sc.xsxm
6輸出一張表格:所有成績不低於60分的每位學生對應一條記錄,包括欄位:班級代碼、姓名、平均成績。
select bjdm, xsxm , avg(cj) from sc
where sno not in ( select sno from sc where grade<60)
group by bjdm, xsxm
7輸出一張表格:每一位學生對應一條記錄,包括欄位:班級代碼、姓名、去掉一個最低分後的平均成績。
方法一:
得到每個學生的最低分:
create view V1 (bjdm,bnxh ,min_cj) as
select bjdm,bnxh,min(cj) from sc group by bjdm,bnxh
select sc.bjdm,sc.xsxm , avg(cj) from sc , v1
where sc.bjdm=v1.bjdm and sc.bnxh=v1.bnxh and sc.cj <> v1.min_cj
group by bjdm,bnxh
方法二:
select sc.bjdm,sc.xsxm , ( sum(cj) – min(cj) ) / count(*) from sc
group by bjdm , bnxh
8輸出一張表格:每門科目對應一條記錄,包括欄位:科目、去掉一個最低分後的平均成績。
方法一:
得到每門課的最低分:
create view V1 ( km, min_cj) as
select km,min(cj) from sc group by km
select sc.km , avg(cj) from sc , v1
where sc.km=v1.km and sc.cj <> v1.min_cj
group by sc.km
方法二:
select km , (sum( cj) – min(cj) )/count(*) from sc
group by km
補充9:輸出表格:每門科目對應一條記錄,包括欄位:科目、去掉一個最低分和最高分後的平均成績。
select km , (sum( cj) – min(cj) – max(cj) )/count(*) from sc
group by km
五 資料庫存放著某高校1990年以來英語四、六級的考試情況,且規定:
1 英語四、六級考試每年分別在6月和12月舉行二次;
2 四級沒有通過的學生不能報考六級;
3 某一級的考試只要沒有通過可以反復參加考試;
4 某一級的考試一旦通過就不能再報考同級的考試;
5 允許報了名但不參加考試。
該資料庫中有二張表,相應的關系模式如下:
學生表:S(Sno, Sname, Ssex, Sage, Sdept),其中Sno為主碼。
考試表:E(Sno, Year, Month, Level, Grade),學號、年、月、級別、成績。
其中(Sno, Year, Month)為主碼。
1. 找出各次四級和六級考試的參考人數和平均成績(報了名但沒有參加考試的不作統計)
select year , month,level ,count(*) , avg(grade)
group by year,month , level
2. 找出各次四級考試中平均分最高的系科(報了名但沒有參加考試的不作統計)。
A: Select sdept from s , e where s.sno=e.sno
Where level=4
Group by sdept
Having avg(grade)>=ALL(
Select avg(grade) from s , e where s.sno=e.sno where level=4 Group by sdept )
B: Select top 1 sdept from s , e where s.sno=e.sno
Where level=4
Group by sdept
Order by (avg(grade) desc
3. 找出已經通過英語六級考試的學生的學號、姓名和性別(用連接方法做)
select s.sno,sname,ssex from s,e
where s.sno=e.sno and level=6 and grade>=60
4. 找出在同一年中四、六級考試都參加了的學生的學號
1) select sno from E
where (level=4 and grade>=60) or level=6
group by year having count(*)>=2
2) select sno from E X where level=4 and grade>=60 and exists (
select * from E Y where Y.sno=X.sno and year=X.year and level=6 )
5. 找出只參加一次考試就通過了英語六級考試的學生的學號
select sno from E
where level=6
group by sno
having count(*)=1 錯,想想為何?
1) select sno from E
where level=6
group by sno
having count(*)=1 and max(grade)>=60
2) select sno from E where level=6 and grade>=60 and sno in (
select sno from E where level=6 group by sno having count(*)=1)
6. 找出至今沒有通過英語四級考試的學生的學號(應包括至今還沒有參加過考試或者是參加了但還沒有通過兩種)
select sno from E where level=4
group by sno
having max(grade)<60
Union
Select sno from s where sno not in( select sno from E)
7. 找出英語六級考試中合格人數最少的考試年份和月份(有並列的都要列出,用一句SQL語句)。
Select year , month From E
Where level = 6 and grade>=60
Group by year , month
Having count(*) <=all
(Select count(*) from E where level=6 and grade>=60 group by year , month )

我是從「上海全鼎軟體學院」畢業的————————

⑶ 急求SQL資料庫練習題

樓上的--理論很多不太使用:ㄨinsert -增加語句用法 insert into(Name,Sec)values("張三","李四") --這個語句1.習題:插入學員信息 Name,Sex,Age,Address (地址可為null) 要有自動標識列。 2.實現一次插入多行。3.把原有表中的某個欄位 移到新表中 提示:select <欄位> into newtable from <原表> ㄨdelecte --刪除語句delecte from <表> [where<條件>]例題:上表中 --刪除 年齡是66和地址為null 信息 (年齡與地址自己添加) ㄨupdate--更新語句update set <條件> where[限制條件]例題:把年齡大於50歲的 更新為49歲 其餘條件自己加 ㄨselect --查詢語句select <欄位1>,<欄位2> from <表> where [條件]例題:從表1、表2中查找相同欄位並且 把相同欄位存放到新的表中這里子查詢就不多說了。這些題很基礎你試一試。

⑷ sql 資料庫習題謝謝

1. select '類別為:'+tushuleibie 圖書分類 from T_Book
2. select shuming 書名,zuozhe 作者,jiage*0.7 價格 from T_Book where chubanshe ='機械工業出版社'
3. select shuming 書名,zuozhe 作者,jiage 價格, chubanshe 出版社 from T_Book where jiage between 30 and 60
4. select top 3 shuming 書名,zuozhe 作者,chubanshe 出版社, jiage 價格 from T_Book order by jiage desc

6.select chubanshe 出版社,AVG(jaige) 平均價,MAX(jiage) 最高價 ,MIN(jaige) 最低價 from T_Book group by chubanshe order by SUM(jiage) desc

10.select top 1 chubanshe 出版社, count(*) 出版圖書個數 from T_Book group by chubanshe order by COUNT(*) desc
book表的做好了 reader的自己做吧 欄位名我用拼音做代替的 你自己替換成你表中的欄位

⑸ SQL的練習,求答案!!!

有一些類似的題看看吧 一定有幫助
實驗一
練習1、請查詢表DEPT中所有部門的情況。
select * from dept;

練習2、查詢表DEPT中的部門號、部門名稱兩個欄位的所有信息。
select deptno,dname from dept;

練習3、請從表EMP中查詢10號部門工作的雇員姓名和工資。
select ename,sal from emp where deptno=10;

練習4、請從表EMP中查找工種是職員CLERK或經理MANAGER的雇員姓名、工資。
select ename,sal from emp where job='CLERK' or job='MANAGER';

練習5、請在EMP表中查找部門號在10-30之間的雇員的姓名、部門號、工資、工作。
select ename,deptno,sal,job from emp where deptno between 10 and 30;

練習6、請從表EMP中查找姓名以J開頭所有雇員的姓名、工資、職位。
select ename,sal,job from emp where ename like 'J%';

練習7、請從表EMP中查找工資低於2000的雇員的姓名、工作、工資,並按工資降序排列。
select ename,job,sal from emp where sal<=2000 order by sal desc;

練習8、請從表中查詢工作是CLERK的所有人的姓名、工資、部門號、部門名稱以及部門地址的信息。
select ename,sal,emp.deptno,dname,loc from emp,dept where emp.deptno=dept.deptno and job=』CLERK』;

練習9、查詢表EMP中所有的工資大於等於2000的雇員姓名和他的經理的名字。
select a.ename,b.ename from emp a,emp b where a.mgr=b.empno(+) and a.sal>=2000;

練習10、在表EMP中查詢所有工資高於JONES的所有雇員姓名、工作和工資。
select ename,job,sal from emp where sal>(select sal from emp where ename=』JONES』);

練習11、列出沒有對應部門表信息的所有雇員的姓名、工作以及部門號。
select ename,job,deptno from emp where deptno not in (select deptno from dept);

練習12、查找工資在1000~3000之間的雇員所在部門的所有人員信息
select * from emp where deptno in (select distinct deptno from emp where sal between 1000 and 3000);

練習13、雇員中誰的工資最高。
select ename from emp where sal=(select max(sal) from emp);
select ename from (select * from emp order by sal desc) where rownum<=1;

*練習14、雇員中誰的工資第二高(考慮並列第一的情況,如何處理)。
select ename,sal from (select ename ,sal from emp where sal<(select max(sal) from emp) order by sal desc) where rownum<=1;
實驗二
1. 查詢所有雇員的姓名、SAL與COMM之和。
select ename,sal+nvl(comm,0) 「sal-and-comm」 from emp;

2. 查詢所有81年7月1日以前來的員工姓名、工資、所屬部門的名字
select ename,sal,dname from emp,dept where emp.deptno=dept.deptno and hiredate<=to_date(『1981-07-01』,』yyyy-mm-dd』);

3. 查詢各部門中81年1月1日以後來的員工數
select deptno,count(*) from emp where hiredate>=to_date(『1981-01-01』,』yyyy-mm-dd』) group by deptno;

4. 查詢所有在CHICAGO工作的經理MANAGER和銷售員SALESMAN的姓名、工資
select ename,sal from emp where (job=』MANAGER』 or job=』SALES』) and deptno in (select deptno from dept where loc=』CHICAGO』);

5. 查詢列出來公司就職時間超過24年的員工名單
select ename from emp where hiredate<=add_months(sysdate,-288);

6. 查詢於81年來公司所有員工的總收入(SAL和COMM)
select sum(sal+nvl(comm,0)) from emp where to_char(hiredate,』yyyy』)=』1981』;

7. 查詢顯示每個雇員加入公司的准確時間,按××××年××月××日 時分秒顯示。
select ename,to_char(hiredate,'yyyy-mm-dd hh24:mi:ss') from emp;

8. 查詢公司中按年份月份統計各地的錄用職工數量
select to_char(hiredate,'yyyy-mm'),loc,count(*) from emp,dept
where emp.deptno=dept.deptno group by to_char(hiredate,'yyyy-mm'),loc;

9. 查詢列出各部門的部門名和部門經理名字
select dname,ename from emp,dept where emp.deptno=dept.deptno and job=』MANAGER』;

10. 查詢部門平均工資最高的部門名稱和最低的部門名稱
select dname from dept where deptno=(select deptno from (select deptno from emp group by deptno order by avg(sal) ) where rownum<=1)
union all select dname from dept where deptno=(select deptno from (select deptno from emp group by deptno order by avg(sal) desc ) where rownum<=1);

11. *查詢與雇員號為7521員工的最接近的在其後進入公司的員工姓名
select ename from (select ename from
(select ename from emp where hiredate>(select hiredate from emp where empno=7521) order by hiredate ) where rownum<=1)

⑹ SQL簡單習題兩道

1:select "書名","定價" from "圖書館"
where "定價" = (select top 1 "定價" from "圖書館" desc as '定價')
//就是要查出定價最高的價格,可以對定價排序,取到,這個你可以自己實踐下,我忘了命令了,本機上沒裝SQL
2:就是要你查出 藏有 發行「書名=『資料庫系統基礎』」的圖書館館名
3:select CNAME,TEACHER from c where C# =
(select C# from SC where S# = '10001')

⑺ 資料庫SQL語言習題求助!

1.select
s.sno,s.sname
from
s,c,sc
where
s.sno=sc.sno
and
c.cno=sc.cno
and
c.cname="MS"
2.select
s.sno
from
s,sc
where
s.sno=sc.sno
and
sc.cno="c1"
or
s.sno=sc.sno
and
sc.cno="c2"
group
by
s.sno
3.select
s.sno,sc.grade
from
s,c,sc
where
s.sno=sc.sno
and
c.cno=sc.cno
and
c.cname="操作系統"
or
c.cname="資料庫課程"
4.select
s.sno,s.sname,s.age
from
s
where
s.sex="女"
and
s.age
>=18
and
s.age
<=
20
5.select
s.sno,s.sname,sc.grade
from
s,c,sc
where
s.sno=sc.sno
and
c.cno=sc.cno
and
c.teacher="劉平"
6.select
s.sname,s.age,s.SD
from
s
where
s.sname="李%"
7.select
s.sname,s.age,s.SD,count()
as
統計
from
s,sc
where
s.sno=sc.sno
and
統計
>3
group
by
s.sno
打那麼多不容易再追加點分吧

⑻ SQL Server 練習題 在線 急!!!

c,d,d,d,a
c,d,,c,b
d,d,d,a,d
a,d,c,b,?
最後一題不確定,4個答案個人感覺都不準確,數據冗餘是指數據出現重復的情況,並非不太重要的數據,由基本數據導出也不準確

⑼ SQL資料庫練習題~

1.select name from 學生
where 系別='數學系' and 性別='女';

2.select a.name from 學生 a inner join 選課 b
on a.學號=b.學號
where 成績<60;

3.select a.name from 學生 a inner join 選課 b
on a.學號=b.學號
where avg(成績)>=95;

4.select a.name from 學生 a inner join 選課 b
on a.學號=b.學號
where (select b.成績 from 課程 c inner join 選課 b
on c.課程號=b.課程號 where 課程名='SQL Server 2000')<60

5.update 學生
set 年齡=年齡+1;

熱點內容
怎麼看清被塗鴉的內容安卓手機 發布:2024-05-07 05:16:52 瀏覽:702
配置業務分類時主要考慮哪些原因 發布:2024-05-07 05:12:40 瀏覽:184
外網如何訪問內網ip 發布:2024-05-07 05:12:30 瀏覽:814
網易版有拔刀劍的伺服器叫什麼 發布:2024-05-07 04:56:35 瀏覽:751
中國好源碼 發布:2024-05-07 04:56:29 瀏覽:872
小兵的伺服器是什麼 發布:2024-05-07 04:55:46 瀏覽:186
網易我的世界四月伺服器維護 發布:2024-05-07 04:24:58 瀏覽:214
gdb調試文件夾 發布:2024-05-07 04:03:25 瀏覽:155
玩5款大型游戲需要什麼配置 發布:2024-05-07 03:59:09 瀏覽:91
什麼安卓游戲畫面炫酷 發布:2024-05-07 03:58:18 瀏覽:74