當前位置:首頁 » 編程語言 » sql練習詳解
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql練習詳解

發布時間: 2022-03-13 16:32:31

❶ 誰有sql 語句練習題,越多越好!

額 我只曉得SQLServer的T-SQL
中文版SQL Server 2000開發與管理應用實例
http://www.china-pub.com/26313
都是大量項目中能用到的實例
CSDN SQL大版主的書籍,可說非常不錯了
希望對你有幫助

如果還要再深入理解點原理東西
可以看下 微軟技術叢書sql2005 T-SQL查詢與T-SQL程序設計

❷ sql的學習技巧

SQL2000入門很容易的啊。找幾本基礎的教程看看,先學會如何在企業管理器創建和管理表,查詢,視圖。了解各種數據類型的含義,了結主鍵,外鍵的含義,你基本可以用SQL2000做一點簡單的事情了,然後再逐步的學習p-sql語言,學會創建觸發器,存儲過程,那麼你又更進一步了。關鍵是多練習

❸ sql練習!!!

alter talbe stuscore
add constraint ch_a check(english<100 and english>0)
select * from stuinfo where name like '張%' and sex = '男'
select time from stuinfo where name = '劉k'
……………………
……………………
只能到這里了,僅供參考

❹ sql 語句練習題

你去矽谷教育裡面有

❺ 簡單的SQL語句練習,最好給出解釋

1D 2B 3A 初步答案,你試試

❻ 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的練習,求答案!!!

/*創建Moonfox_db資料庫*/
use master
if exists(select * from sysdatabases where name='Moonfox_db')
drop database Moonfox_db
create database Moonfox_db
on
(
name='Moonfox_db_data',
filename='D:\Visual Studio 2008 & Sql server 2005\Sql server\Moonfox_db.mdf',
size=10,
filegrowth=2MB
)
log on
(
name='Moonfox_db_log',
filename='D:\Visual Studio 2008 & Sql server 2005\Sql server\Moonfox_db.ldf',
size=5,
filegrowth=20%
)/*創建Department表*/
use Moonfox_db
if exists(select * from sysobjects where name='Department')
drop table Department
create table Department
(
DID int identity (1,1)primary key,--部門編號,主鍵
Dname nvarchar(20),--部門名稱
Address nvarchar(50),--部門地址
Photo decimal(12,0),--電話
)/*創建Employee表*/
use Moonfox_db
if exists(select * from sysobjects where name='Employee')
drop table Employee
create table Employee
(
EID int identity (1,1)primary key,--職工編號,主鍵
Ename varchar(10),--職工名
Gender nchar(2) check(Gender='男' or Gender='女'),--性別,添加限制
Position nvarchar(10) check(Position='員工' or Position='組長' or Position='經理'),--職務,添加限制
Address nvarchar(50),--家庭地址
DID int,--部門編號,外鍵
foreign key(DID) references Department(DID)--外鍵約束
)
/*創建Care表*/
use Moonfox_db
if exists(select * from sysobjects where name='Care')
drop table Care
create table Care
(
CID int identity (1,1)primary key,--保健卡編號,主鍵
EID int,--職工號,外鍵
foreign key(EID) references Employee(EID),--外鍵約束
CheckDate datetime,--檢查身體日期
PhysicalCondition nvarchar(4) check(PhysicalCondition='一般' or PhysicalCondition='差' or PhysicalCondition='好'),--健康狀況
)
/*創建Care表約束*/
alter table Care
add
constraint DF_CheckDate default(getdate()) for CheckDate--預設,默認凈時間為當前計算機時間 路徑自己修改,試圖自己做,選擇語句自己寫。我該睡覺了,抱歉,你試著在sql server中運行下,我等著休息,也不知道寫的有沒有錯誤,沒時間幫你寫省下的了。不急著用的話我明天幫你寫吧。

❽ sql語句練習,新手幫幫忙

insert /select/update /delete /left join /right join
inner join
如果你能把這些搞懂了,你應該這些都會做了!
我剛學的時候也是這樣搞的