sql server 查詢練習


需要建的三個表:

  學生表

  create table Student

  (
  Sno varchar(20) not null primary key,
  Sname varchar(20) not null,
  Ssex varchar(20) not null,
  Sbirthday datetime,
  Class varchar(20)
  )

    課程表

  create table Course
  (
  Cno varchar(20) not null primary key,
  Cname varchar(20) not null,
  Tno varchar(20) not null references Teacher(Tno)
  )

  成績表: 

  create table Score
  (
  Sno varchar(20) not null references Student(Sno),
  Cno varchar(20) not null references Course(Cno),
  Degree Decimal(4,1)
  )

插入數據:

  學生表:  

  insert into student values (108,'曾華','男',1977-09-01,95033);
  insert into student values (105,'匡明','男',1975-10-02,95031);
  insert into student values (107,'王麗','女',1976-01-23,95033);
  insert into student values (101,'李軍','男',1976-01-23,95033);
  insert into student values (109,'王芳','女',1975-02-10,95031);
  insert into student values (103,'陸君','女',1974-036-03,95031);

   課程表: 

  insert into Course values (3-105,'計算機導論',825);
  insert into Course values (3-245,'操作系統',804);
  insert into Course values (6-166,'數據電路',856);
  insert into Course values (9-888,'高等數學',831);

   成績表:

  insert into Score values (103,3-245,86);
  insert into Score values (105,3-245,75);
  insert into Score values (109,3-245,68);
  insert into Score values (103,3-105,92);
  insert into Score values (105,3-105,88);
  insert into Score values (109,3-105,76);
  insert into Score values (101,3-105,64);
  insert into Score values (107,3-105,91);
  insert into Score values (108,3-105,78);
  insert into Score values (101,6-166,85);
  insert into Score values (107,6-166,79);
  insert into Score values (108,6-166,81);

 

查詢題目:  

--1) 查詢java 課程比C#分數高的學生

--2)查詢平均分成績大於 70 分的同學的姓名和平均成績

--3)查詢所有同學的學號、姓名、選課數、總成績

--5)查詢沒有學過 java 課的學生的學號、姓名
  --學過java

  --沒學過java

--6)查詢學過“C#”課程並且也學過“sql”課程的學生的學號、姓名

--7)查詢所有課程的平均分、及格率

---8)查詢所有課程成績小於 60 分的同學的學號、姓名、性別

--9)查詢沒有學全所有課的同學的學號、姓名、性別

--10)查詢至少有一門課與學號為“002”的同學所學相同的同學的學號和姓名

--13)查詢和“002”號的同學學習的課程完全相同的其他同學學號和姓名

--15)按平均成績從高到低顯示所有學生的“sql”、“java”、“c#”三門的課程 成績,按如下形式顯示:學生 ID,sql,java,c#,有效課程數,有效平均分

--16)查詢各科成績最高和最低的分:以如下形式顯示:課程 ID,最高分,最低分

--17)查詢不同班級所教不同課程平均分從高到低顯示

--18)查詢各科成績前三名的記錄:(不考慮成績並列情況)

/*
row_number() over( order by sc.mark desc)
*/

--19)查詢每門課程被選修的學生數

--20)查詢出只選修了一門課程的全部學生的學號和姓名

--21)查詢男生、女生人數

--22)查詢姓“張”的學生名單

--23)查詢同名同性學生名單,並統計同名人數

--24)查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時, 按課程號降序排列

--25)查詢平均成績大於70的所有學生的學號、姓名和平均成績

--26)檢索至少選修兩門課程的學生學號

--27)查詢兩門以上不及格課程的同學的學號及其平均成績

--28)檢索“java”課程分數小於 60,按分數降序排列的同學姓名

 

查詢題目答案:

 1 1select * from Student stu 
 2 left join Score sco on stu.Sno=sco.Sno and sco.Cno=-102
 3 left join Score sco2 on stu.Sno=sco2.Sno and sco2.Cno=-242
 4 where sco.Degree>sco2.Degree;
 5 
 6 2select Sname,AVG(sco.Degree) from Student stu 
 7 left join Score sco on stu.Sno=sco.Sno
 8 group by stu.Sname,sco.Sno  having AVG(sco.Degree)>70;
 9 
10 3select stu.Sno,stu.Sname,count(sco.Sno) as '選課數' ,sum(sco.Degree) '總成績'
11 from Student stu inner join  Score sco on stu.Sno=sco.Sno
12 group by stu.Sname,stu.Sno,sco.Sno;
13 
14 5.1:select * from Student where Sno  in(select Sno from score  where Sno  not in (select sno from Score where Cno=-242));
15 
16 5.2:select * from Student where Sno in (select Sno from score where  Sno in (select sno from Score where Cno=-242));
17 
18 6:select  * from Student stu  
19 left join Score sco on stu.Sno=sco.Sno and sco.Cno=-102
20 left join Score sco2 on stu.Sno=sco.Sno and sco2.Cno=-242
21 where sco.Sno=sco2.Sno;
22 
23 7:select a.Cno,avg(a.Degree) as 'avg', 
24 CONVERT(float,SUM(case when Degree>0  then 1 else 0 end))  as '總人數',
25 cONVERT(float,SUM(case when Degree>60  then 1 else 0 end))  as '每門的及格人數',
26 CONVERT(varchar(20),CONVERT(decimal(18,2),cONVERT(float,SUM(case when Degree>60  then 1.0 else 0.0 end))/
27 SUM(case when Degree>0  then 1 else 0 end))*100 )+'%'
28  as '及格率'
29 from Score a
30 group  by a.Cno 
31 
32 8:select * from Student where Sno in(select Sno from Score where  Degree<60);
33 
34 9:select sco.Sno,stu.Sname,stu.Ssex from Student stu
35 left join Score sco on stu.Sno=sco.Sno
36 group by sco.Sno,stu.Sname,stu.Ssex having COUNT(sco.Sno)!=(select COUNT(*) from Course)
37 
38 10:select * from Student where sno in (select Sno from Score where Cno in (select Cno from Score where Score.Sno=108));
39 
40 
41 
42 13:select sco.Sno from Score  sco where sco.Sno not  in (select Sno from Score where Cno not in (select Cno from Score where Sno=103)) 
43  group by sco.Sno having COUNT(*)=(select COUNT(*) from Score where Sno=103)
44  and sco.Sno<>103 
45 
46 15:select stu.Sno,stu.Sname,
47 sum(case when sco.cno=-102 then sco.Degree else 0 end) '計算機導論',
48 sum(case when sco.cno=-160 then sco.Degree else 0 end) '數據電路',
49 sum(case when sco.cno=-242 then sco.Degree else 0 end) '操作系統',
50 sum(case when sco.cno=-879 then sco.Degree else 0 end) '高等數學',
51 COUNT(*) as '有效課程數' ,AVG(sco.Degree) as '有效平均分'
52 from Student stu 
53 left join Score sco on stu.Sno=sco.Sno
54 group by stu.Sno,stu.Sname order by AVG(sco.Degree)desc;
55 
56 16:select Sno,MAX(Degree) as '最高分' ,MIN(Degree) as '最低分' from Score group by Sno;
57 
58 17:select cou.Cno,stu.Class,avg(sco.Degree)from Score sco
59 left join Course cou
60 on sco.Cno=cou.Cno
61 left join Student stu
62 on stu.Sno=sco.Sno
63 group by cou.Cno,stu.Class 
64 order by AVG(sco.Degree) desc;
65 
66 18:select * from (select *, ROW_NUMBER() over (partition by cno order by Degree desc  ) ev   from Score sco) t
67 where t.ev<4 order by t.Cno,t.Degree desc
68 
69 19:select Sno,COUNT(cno) '選修的課程數' from Score group by Sno;
70 
71 20:select * from Student stu
72 left join  Score sco  on stu.Sno=sco.Sno
73 where(select COUNT(*) from Score sco2 where sco.Sno=sco2.Sno)=3; 
74 
75 21:select SUM(case when ssex='' then 1 else 0 end )as '',
76 SUM(case when ssex='' then 1 else 0 end )as ''
77 from Student;
78 
79 22:select * from Student where sname like '張%'; 
80 
81 23:select sname,COUNT(*) from Student group by Sname having COUNT(*)>1;
82 
83 24:select Cno,AVG(Degree) from Score group by Cno order by AVG(Degree) desc ,Cno  ;
84 
85 25:select Sno,AVG(Degree) as '平均分' from Score group by Sno having AVG(Degree)>70;
86 
87 26:select sno ,count(Cno) as '選修課程數' from  Score group by Sno having COUNT(Cno)>2 or COUNT(Cno)=2 ;
88 
89 27:select Sno,SUM(Case when Degree<60 then 1 else 0 end ) as '不及格人數' , 
90 avg(Degree) from Score group by Sno having SUM(Case when Degree<60 then 1 else 0 end )=1;
91 
92 28:select * from Student stu left join Score sco on stu.Sno=sco.Sno
93 where Degree>60 and Cno=-242 order by Degree desc ;
View Code

 

 

   

 

  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM