1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
|
select * from course where Cname like 'DB\_%s__' escape '\'' ;
--2. 查询名字中第2个字为‘阳’的学生姓名和学号及选修的课程号、课程名; select Sname 学生姓名,sc.Sno 学号,sc.Cno 课程号,Cname 课程名 from student S,sc,course C where S.Sno=sc.Sno and sc.Cno=C.Cno and Sname like '_阳%'; 或 select student.Sno,Sname,sc.Cno,Cname from student,sc,course where student.Sno=sc.Sno and sc.Cno=course.Cno and substring(student.Sname,2,2)='阳'; --3. 列出选修了‘数学’或者‘大学英语’的学生学号、姓名、所在院系、选修课程号及成绩; select S.Sno,Sname,Sdept,Cno,Grade from student S,sc where S.Sno=sc.Sno and Cno in( select Cno from course where Cname='数学' or Cname='大学英语'); 或 select S.Sno,Sname,Sdept,sc.Cno,Grade from student S,sc,course C where S.Sno=sc.Sno and C.cno=sc.cno and (Cname='数学' or Cname='大学英语')/*此处括号不可少*/ --4. 查询缺少成绩的所有学生的详细情况; select * from student where Sno in( select distinct Sno from sc where Grade is null ); 或 select student.sno,sname,ssex,sage,sdept,cno,grade from student,sc where student.sno=sc.sno and Grade is null --5. 查询与‘张毅’(假设姓名唯一)年龄不同的所有学生的信息; select * from student where Sage not in( select Sage from student where Sname='张毅' );
--6. 查询所选课程的平均成绩大于张毅的平均成绩的学生学号、姓名及平均成绩; select student.Sno,Sname,avg(Grade) 平均成绩 from student ,sc where student.sno=sc.sno group by student.Sno,Sname having avg(Grade)>( select avg(Grade) from sc inner join student on sc.Sno=student.Sno where Sname='张毅' );
--7. 按照“学号,姓名,所在院系,已修学分”的顺序列出学生学分的获得情况。其中已修学分为考试已经及格的课程学分之和; select student.Sno,Sname,Sdept,sum(credit) 已修学分 from student inner join sc on student.sno=sc.sno inner join course on sc.cno=course.cno where grade>=60 group by student.sno,sname,sdept; 或 select student.sno,sname,sdept,sum(credit)已修学分 from student,sc,course where student.sno=sc.sno and sc.cno=course.cno and sc.grade>60 group by student.Sno,Sname,Sdept; --8. 列出只选修一门课程的学生的学号、姓名、院系及成绩; select student.Sno,Sname,Sdept,Grade from student inner join sc on student.Sno=sc.Sno where exists( select * from sc x where x.Sno=sc.Sno group by Sno having count(*)=1); 或 select student.Sno,Sname,Sdept,Grade from student,sc where student.Sno=sc.Sno and student.Sno in(select Sno from sc group by Sno having count(Cno)=1) --9. 查找选修了至少一门和张毅选修课程一样的学生的学号、姓名及课程号; select SC.Sno,Sname,Cno from student as S inner join sc as SC on S.Sno=SC.Sno where Cno in ( select Cno from sc where Sno=( select Sno from student where sname='张毅' ) ); 或 select student.Sno,student.Sname,c1.Cno from student,sc c1,sc c2 where student.Sno=c1.Sno and c1.Sno=c2.Sno and c2.Cno in ( select sc.Cno from sc,student where sc.Sno=student.Sno and student.Sname='张毅') and student.Sname != '张毅' --10. 查询至少选修“数据库”和“数据结构”课程的学生的基本信息; select * from student where not exists (select * from course where cname in ('数据库','数据结构') and not exists(select * from sc where student.sno = sc.sno and sc.cno =course.cno))
--11. 查询没有选修张毅所选修的全部课程的学生的姓名; --(就是查找一个学生,存在张毅选修的一个课程,他没有选修) select Sname from student S1 where exists( select * from sc SC1 where Sno=( select Sno from student where Sname='张毅' ) and not exists ( select * from sc SC2 where S1.Sno=SC2.Sno and SC1.Cno=SC2.Cno ) );
--12. 查询每个专业年龄超过该专业平均年龄的学生的姓名和专业; select Sname,Sdept from student S1 where Sage>( select avg(Sage) from student S2 where S1.Sdept=S2.Sdept );
--13. 查询选修了张毅同学所选修的全部课程的学生的姓名; --(就是查找一个学生,不存在张毅选修的一个课程,他没有选修) select Sname from student as S where not exists( select * from sc as SC1 where Sno=( select Sno from student where Sname='张毅' ) and not exists( select * from sc as SC2 where SC2.Cno=SC1.Cno and SC2.Sno=S.Sno ) );
--14. 检索选修了全部课程的学生姓名; --(就是查找一个学生,不存在一个课程,他没有选修) select Sname from student S where not exists( select * from course C where not exists( select * from sc SC where SC.Sno=S.Sno and SC.Cno=C.Cno ) );
--15. 列出同时选修“1”号课程和“2”号课程的所有学生的姓名;(使用三种方法实现) --(1)exists select Sname from student S where exists( select * from sc where S.Sno=sc.Sno and Cno='1' ) and exists( select * from sc where S.Sno=sc.Sno and Cno='2' ); --(2)求交集 select Sname from student where Sno in ((select Sno from sc where Cno='1') intersect (select Sno from sc where Cno='2')); --(3) select Sname from student where Sno in (select Sno from sc where Cno='1'and sno in(select Sno from sc where Cno='2')) --16. 使用嵌套查询列出选修了“数据结构”课程的学生学号和姓名; select Sno,Sname from student where Sno in( select Sno from sc where Cno in( select Cno from course where Cname='数据结构' ) );
--17. 使用嵌套查询查询其它系中年龄小于CS系的某个学生的学生姓名、年龄和院系; select Sname,Sage,Sdept from student where Sdept<>'CS' and Sage<any( select Sage from student where Sdept='CS' );
--18. 查询选课人数最多的课程号和课程名(包含并列); --方法1: select Cno,Cname from course where Cno in( select top 1 with ties Cno from sc group by Cno order by count(Sno) desc ); --方法2: select sc.Cno,course.Cname from sc,course where sc.Cno=course.Cno group by sc.Cno,course.Cname having count(sc.sno)>=all(select count(c1.sno) from sc c1 group by c1.sno) --方法3: select cno,cname from course where cno in (select cno from sc group by cno having count(sno)>=all(select count(c1.sno) from sc c1 group by c1.sno) ) --19. 使用集合查询列出CS系的学生以及性别为女的学生名单; (select Sname from student where Sdept='CS') union (select Sname from student where Ssex='女');
--20. 使用集合查询列出CS系的学生与年龄不大于19岁的学生的交集、差集; --交集 (select * from student where Sdept='CS') intersect (select * from student where Sage<=19); --差集 (select * from student where Sdept='CS') except (select * from student where Sage<=19);
--(二)以数据库原理实验1中SPJ数据库为基础,请使用T-SQL 语句实现教材P130第5题(1)—(7) --(1)找出所有供应商的姓名和所在城市 select Sname,City from S;
--(2)找出所有零件的名称,颜色,重量 select Pname,Color,weights from P;
--(3)找出使用供应商S1所供应零件的工程号码 select Jno from SPJ where Sno='s1';
--(4)找出工程项目J2使用的各种零件的名称以及数量 select pname,qty from p,spj where p.pno=spj.pno and jno='j2'
--(5)找出上海厂商供应的所有零件号码 select distinct Pno from SPJ where Sno in( select Sno from S where City='上海' );
--(6)找出使用上海产的零件的工程名称 --方法1: select distinct jname from s,j,spj where s.sno=spj.sno and j.jno=spj.jno and s.City='上海' --方法2 select jname from j where jno in(select jno from spj where sno in (select sno from s where city='上海')) --(7)找出没有使用天津产的零件的工程号码 --方法1 select jno from j where not exists (select * from spj where spj.jno=j.jno and sno in (select sno from s where city='天津') ) --方法2: select jno from j where not exists (select * from spj,s where spj.sno=s.sno and spj.jno=j.jno and s.city='天津') --本题不可以这样写: select distinct Jno from SPJ where Pno not in( select Pno from SPJ where Sno in( select Sno from S where City='天津' ) ); --(三)对罗斯文(Northwind)数据库完成一下查询 --1.查询每个订单购买产品的数量和总金额,显示订单号,数量,总金额 select OrderID 订单号,sum(Quantity) 数量,sum(Quantity*UnitPrice*(1-Discount)) 总金额 from [Order Details] group by OrderID --2. 查询每个员工在7月份处理订单的数量 --方法1 select EmployeeID,count(*) 订单数 from Orders where datepart(month,OrderDate)=7 group by EmployeeID; --方法2(若员工没有处理订单亦可显示出来) select Employees.EmployeeID,count(Orders.OrderID) 订单数量 from Orders right join Employees on Employees.EmployeeID=Orders.EmployeeID where MONTH(Orders.OrderDate)='07' group by Employees.EmployeeID --3. 查询每个顾客的订单总数,显示顾客ID,订单总数 --方法1 select CustomerID,count(*) 订单总数 from Orders group by CustomerID; --方法2(顾客没有订单也可以显示出来) select Customers.CustomerID,count(Orders.OrderID) 订单总数 from Orders right join Customers on Orders.CustomerID=Customers.CustomerID group by Customers.CustomerID --4. 查询每个顾客的订单总数和订单总金额 --方法1 select CustomerID,count(distinct OD.OrderID) 订单总数,sum(Quantity*UnitPrice*(1-Discount)) 订单总金额 from Orders as O inner join [Order Details] as OD on O.OrderID=OD.OrderID group by CustomerID; --方法2(顾客没有订单也可以显示出来) select Customers.CustomerID,count(distinct Orders.OrderID) 订单总数,sum([Order Details].Quantity*[Order Details].UnitPrice*(1-[Order Details].Discount)) 订单总金额 from Orders right join Customers on Orders.CustomerID=Customers.CustomerID left join [Order Details] on Orders.OrderID=[Order Details].OrderID group by Customers.CustomerID --5. 查询每种产品的卖出总数和总金额 --方法1 select P.ProductID,sum(Quantity)卖出总数,sum(Quantity*OD.UnitPrice*(1-Discount)) 订单总金额 from Products as P left outer join [Order Details] as OD on P.ProductID=OD.ProductID group by P.ProductID; --方法2 select sum([Order Details].Quantity) 卖出总数,sum([Order Details].Quantity*[Order Details].UnitPrice*(1-[Order Details].Discount)) 总金额 from Products left join [Order Details] on Products.ProductID=[Order Details].ProductID group by Products.ProductID --6. 查询购买过全部商品的顾客的ID和姓名 --(查询一个顾客,不存在某个商品他没有购买过) --方法1 select c1.CustomerID,c1.ContactName from Customers c1 where not exists( select * from Products p1 where not exists ( select * from Orders,[Order Details] where Orders.OrderID=[Order Details].OrderID and Orders.CustomerID=c1.CustomerID and [Order Details].ProductID=p1.ProductID)) --方法2 select c1.CustomerID,c1.ContactName from Customers c1,Orders,[Order Details] where c1.CustomerID=Orders.CustomerID and Orders.OrderID=[Order Details].OrderID group by c1.CustomerID,c1.ContactName having count([Order Details].OrderID)=( select count(Products.ProductID) from Products) --(四) 对books数据库完成以下操作 --1. 查询各种类别的图书的类别和数量(包含目前没有图书的类别) select TypeName,count(BookNo)数量 from BookType as B1 left outer join BookInfo as B2 on B1.TypeID=B2.TypeID group by TypeName;
--2. 查询借阅了‘数据库基础’的读者的卡编号和姓名
select B.CardNo,Reader from BorrowInfo as B inner join CardInfo as C on B.CardNo=C.CardNo where B.BookNo=( select BookNo from BookInfo where BookName='数据库基础' ); --或 select CardNo,Reader from CardInfo where CardNo in ( select CardNo from BorrowInfo,BookInfo where BorrowInfo.BookNo=BookInfo.BookNo and BookName='数据库基础' ) --3. 查询各个出版社的图书价格超过这个出版社图书的平均价格的图书的编号和名称。 select BookNo,BookName from BookInfo as B1 where Price>( select avg(Price) from BookInfo as B2 where B1.Publisher=B2.Publisher );
--4. 查询借阅过了全部图书的读者的编号和姓名 --(就是查找一个读者,不存在一本书,这个读者没有借阅过) select CardNo,Reader from CardInfo where not exists( select * from BookInfo where not exists ( select * from BorrowInfo where BorrowInfo.CardNo=CardInfo.CardNo and BorrowInfo.BookNo=BookInfo.BookNo))
--5. 查询借阅图书包含李明所借的全部图书的读者的编号和姓名 --(就是查找某读者,不存在李明借过的图书,这个读者没有借过) select CardNo,Reader from CardInfo as CI where not exists( select * from BorrowInfo as BI2 where BI2.CardNo=( select CardNo from CardInfo where Reader='李明' ) and not exists( select * from BorrowInfo as BI3 where BI3.BookNo=BI2.BookNo and BI3.CardNo=CI.CardNo ) )and CI.Reader!='李明'; --或 select c1.CardNo,c1.Reader from CardInfo c1 where not exists ( select * from CardInfo c2,BorrowInfo b1 where c2.CardNo=b1.CardNo and c2.Reader='李明' and not exists( select * from BorrowInfo b2 where b2.CardNo=c1.CardNo and b2.BookNo=b1.BookNo ) )and c1.Reader!='李明' --6. 查询借阅次数超过2次的读者的编号和姓名 select BI.CardNo,Reader from BorrowInfo as BI inner join CardInfo as CI on BI.CardNo=CI.CardNo group by BI.CardNo,Reader having count(BorrowDate)>2; 或 select CardInfo.CardNo,CardInfo.Reader from CardInfo,BorrowInfo where CardInfo.CardNo=BorrowInfo.CardNo group by CardInfo.CardNo,CardInfo.Reader having count(*)>2 --7. 查询借阅卡的类型为老师和研究生的读者人数 select count(*) 人数 from CardInfo as CI inner join CardType as CT on CI.CTypeID=CT.CTypeID where TypeName in('教师','研究生');
--8. 查询没有被借过的图书的编号和名称 --(查找一本书,不存在它被借过) select BookNo,BookName from BookInfo as BI where not exists( select * from BorrowInfo as BI2 where BI2.BookNo=BI.BookNo ); --或 select BookInfo.BookNo,BookInfo.BookName from BookInfo where BookInfo.BookNo not in( select BorrowInfo.BookNo from BorrowInfo) --9. 查询没有借阅过英语类型的图书的学生的编号和姓名 --(不存在有一本英语类型的图书,他借过) select CardInfo.CardNo,CardInfo.Reader from CardInfo where not exists (select * from BookType,BorrowInfo,BookInfo where BookType.TypeName='英语' and BookType.TypeID=BookInfo.TypeID and BookInfo.BookNo=BorrowInfo.BookNo and BorrowInfo.CardNo=CardInfo.CardNo) and CTypeID in( select CTypeID from CardType where TypeName in('学生') ); --或 select CI.CardNo,Reader from CardInfo as CI left outer join BorrowInfo as BI on BI.CardNo=CI.CardNo where not exists( select * from BorrowInfo BI2 where BI2.BookNo in( select BookNo from BookInfo as BI3 inner join BookType as BT on BI3.TypeID=BT.TypeID where BT.TypeName='英语' ) and BI2.CardNo=BI.CardNo )and CI.CTypeID in( select CTypeID from CardType where TypeName in('学生') );
--10. 查询借阅了‘计算机应用’类别的‘数据库基础’课程的读者编号以及该读者的借阅卡的类型。
select CardInfo.CardNo, CardType.TypeName from CardInfo,CardType where CardInfo.CTypeID = CardType.CTypeID and CardInfo.CardNo in ( select BorrowInfo.CardNo from BorrowInfo, BookInfo,BookType where BorrowInfo.BookNo = BookInfo.BookNo and BookType.TypeID = BookInfo.TypeID and BookType.TypeName = '计算机应用' and BookName = '数据库基础' )
--(五) 对商场数据库完成以下操作 /* Market (mno, mname, city) Item (ino, iname, type, color) Sales (mno, ino, price) 其中,market表示商场,它的属性依次为商场号、商场名和所在城市; item表示商品,它的属性依次为商品号、商品名、商品类别和颜色; sales表示销售,它的属性依次为商场号、商品号和售价。 用SQL语句实现下面的查询要求: */
--1. 列出北京各个商场都销售,且售价均超过10000 元的商品的商品号和商品名 --(不存在北京的一个商场不销售它) select ino,iname from item where not exists ( select * from sales,market where sales.mno=market.mno and market.city='北京' and not exists (select * from sales s1 where s1.ino=item.ino and s1.mno=sales.mno and s1.price>10000 )) --2. 列出在不同商场中最高售价和最低售价之差超过100 元的商品的商品号、最高售价和最低售价
select sales.ino,max(sales.price),min(sales.price) from sales group by sales.ino having max(sales.price)-min(sales.price)>100
--3. 列出售价超过该商品的平均售价的各个商品的商品号和售价 select s1.ino,s1.price from sales s1 where s1.price> (select avg(s2.price) from sales s2 where s1.ino=s2.ino) --4. 查询销售了全部商品的商场号,商场名和城市 --(不存在一个商品,这个商场没有销售过) select m1.mno,m1.mname,m1.city from market m1 where not exists( select* from item i1 where not exists( select * from sales where sales.mno=m1.mno and sales.ino=i1.ino)) --5. 查询所有商场都销售了的商品的商品号和商品名。(用两种方法实现) --(1)(不存在一个商场没有销售过这个商品) select i1.ino,i1.iname from item i1 where not exists (select * from market m1 where not exists (select * from sales where sales.ino=i1.ino and sales.mno=m1.mno))
--(2)销售它的商场数等于商场总数 select item.ino,item.iname from item,sales where item.ino=sales.ino group by sales.ino,item.ino,item.iname having count(sales.mno)=( select count(market.mno) from market)
--6. 查询每个商场里价格最高的商品的名称(用两种方法做) --(1)等于最高价 select mno,iname from sales as S1 inner join item as I on S1.ino=I.ino where price=( select max(price) from sales as S2 where S2.mno=S1.mno );
--(2)>=all select mno,iname from sales as S inner join item as I on S.ino=I.ino where price>=all( select price from sales as S2 where S2.mno=S.mno );
|