数据库实验三

--(一)以数据库系统实验1中student数据库为基础,请使用T-SQL 语句实现进行以下操作:
--1.查询以‘DB_’开头,且倒数第3个字符为‘s’的课程的详细情况;
select *
from course
where cname like 'DB[_]%'and SUBSTRING(cname,len(cname)-2,1)='s'
--2.查询名字中第2个字为‘阳’的学生姓名和学号及选修的课程号、课程名;
select student.sname,student.sno,course.cno,course.cname
from course,sc,student
WHERE SUBSTRING(student.sname,2,1)='阳'and student.sno=sc.sno and sc.cno = course.cno
--3.列出选修了‘数学’或者‘大学英语’的学生学号、姓名、所在院系、选修课程号及成绩;
select student.sname,student.sno,student.sdept,course.cno,sc.grade
from course,sc,student
WHERE course.cno=sc.cno and student.sno=sc.sno and (course.cname='数学' or course.cname='大学英语')
--4.查询缺少成绩的所有学生的详细情况;
select student.sname,student.sno,student.sdept,course.cno,course.cname
from course,sc,student
WHERE course.cno=sc.cno and student.sno=sc.sno and (sc.grade is NULL)
--5.查询与‘张力’(假设姓名唯一)年龄不同的所有学生的信息;
select student.sname,student.sno,student.ssex,sage,student.sdept
from student
WHERE sage<>
(select sage
from student
where student.sname = '张力'
)
--6.查询所选课程的平均成绩大于张力的平均成绩的学生学号、姓名及平均成绩;
select student.sname,student.sno,平均grade=AVG(sc.grade)
from student,sc
where sc.sno=student.sno  
group by student.sno ,student.sname
having AVG(sc.grade)>
(
select AVG(sc.grade)
from student,sc
where student.sname='张力'
)
--7.按照“学号,姓名,所在院系,已修学分”的顺序列出学生学分的获得情况。其中已修学分为考试已经及格的课程学分之和;
select student.sname,student.sno,student.sdept,已修学分=sum(course.credit)
from student,sc,course
where student.sno=sc.sno and sc.cno=course.cno and sc.grade>=60
group by student.sname,student.sno,student.sdept
--8.列出只选修一门课程的学生的学号、姓名、院系及成绩;
select Student.Sno'学号',Student.Sname'姓名',Student.Sdept'所在院系',SC.Grade'成绩'
from sc join student on Student.Sno=SC.Sno
where student.sno in(select sno 
        from sc
        group by sno        
        having count(cno)=1)
--9.查找选修了至少一门和张力选修课程一样的学生的学号、姓名及课程号;
select student.sname,student.sno,sc.cno
from student , sc 
where    student.sno = sc.sno and sc.sno in (
        select sc.sno 
        from sc , student
        where cno in (
            select cno 
            from sc,student
            where sc.sno = student.sno
            and sname = '张力'
            )
    )
--10.查询至少选修“数据库”和“数据结构”课程的学生的基本信息;
select distinct student.* 
from student , sc ,course
where student.sno = sc.sno and cname ='数据库'
        and sc.sno  in (
        select sc.sno
        from course , sc
        where course.cno =sc.cno
        and  cname='数据结构'
        )
--11.查询没有选修张力所选修的全部课程的学生的姓名;

 

select s1.Sname
from student s1
where  not exists(
select *
from sc sc2,student
where student.Sno=sc2.Sno and student.Sname='张力' and exists(
select *
from sc sc3
where sc3.Cno=sc2.Cno and sc3.Sno=s1.Sno
)
)
--12.查询每个专业年龄超过该专业平均年龄的学生的姓名和专业;
select x.sname , x.sdept
from student x
where sage > (
    select avg(sage)
    from student y
    where y.sdept = x.sdept
    )
--13.查询选修了张力同学所选修的全部课程的学生的姓名;
select sname
from student , sc
where student.sno = sc.sno and
cno in(
    select cno
    from student , sc
    where student.sno=sc.sno  and
    sname = '张力'
)
group by sname 
having count(cno) >= (
    select count(*)
    from sc,student
    where sc.sno = student.sno and 
    sname = '张力'
)
--或者
select  distinct sname
from  sc ,student
where  student.sno = sc.sno and not exists 
(
    select * 
    from sc c1 ,student s1
    where  c1.sno = s1.sno and  s1.sname = '张力' and
    not exists
    (
    select *
    from sc c2 ,student s2
    where c2.sno = sc.sno and c2.cno = c1.cno
        )    
)
--14.检索选修了全部课程的学生姓名;
select sname
from student , sc
where student.sno = sc.sno 
group by sname 
having count(cno) = (
    select count(*)
    from course
)

--或者
select sname
from student 
where not exists 
(
    select * 
    from course 
    where not exists
    (
    select *
    from sc
    where course.cno = sc.cno and sc.sno = student.sno
        )
)
--15.列出同时选修“1”号课程和“2”号课程的所有学生的姓名;(使用两种方法实现)
select sname
from sc,student
where sc.sno = student.sno
and cno in(
 select cno 
 from course
 where cno = '1' or cno = '2'
 )
 group by sname 
 having count(sname) = 2

select sname
from sc,student
where sc.sno = student.sno and cno = '1'
intersect 
select sname
from sc,student
where sc.sno = student.sno and cno = '2'
--16.使用嵌套查询列出选修了“数据结构”课程的学生学号和姓名;
select sname,student.sno
from sc,student
where sc.sno = student.sno and 
    cno in (
    select cno 
    from course 
    where cname = '数据结构'
    )
--17.使用嵌套查询查询其它系中年龄小于CS系的某个学生的学生姓名、年龄和院系;
select sname , sage ,sdept
from student
where sage    select sage
   from student
   where sdept = 'CS'
   ) and sdept != 'CS'
--18.查询选课人数最多的课程号和课程名(包含并列)
select distinct cname ,sc.cno
from course  , sc
where sc.cno in (
        select cno 
        from sc
        group by cno
        having count(*)>=all(
        select  count(*)
        from sc 
        group by cno
        )
) and sc.cno = course.cno
--19.使用集合查询列出CS系的学生以及性别为女的学生名单;
select distinct sname
from student
where sdept='CS' or ssex = '女'
--20.查询选修了所有男生所选的全部课程的女生的学号和姓名。
select  distinct sname , student.sno
from  sc ,student
where  student.sno = sc.sno and student.ssex ='女' and not exists 
(
    select c1.cno
    from sc c1 ,student s1
    where  c1.sno = s1.sno and  s1.ssex = '男' and
    not exists
    (
    select c2.cno
    from sc c2 ,student s2
    where c2.sno = sc.sno and c2.cno = c1.cno 
        )
)
--(二)对罗斯文(Northwind)数据库完成一下查询
--1.查询每个订单购买产品的数量和总金额,显示订单号,数量,总金额
select  OrderID , 'sum'=Sum(UnitPrice*(1-Discount)*Quantity)
from [Order Details]
group by OrderID         
--2. 查询每个员工在7月份处理订单的数量
select Orders.EmployeeID ,'订单数'=count(*)
from Orders
where MONTH(OrderDate) = 7
group by Orders.EmployeeID
--3. 查询每个顾客的订单总数,显示顾客ID,订单总数
select Orders.CustomerID ,'订单数'=count(*)
from Orders
group by Orders.CustomerID
--4. 查询每个顾客的订单总数和订单总金额
select Orders.CustomerID ,'订单数'=count(Orders.OrderID),'总金额'=sum(UnitPrice*(1-Discount)*Quantity)
from Orders join [Order Details] on  Orders.OrderID = [Order Details].OrderID 
group by Orders.CustomerID
--5. 查询每种产品的卖出总数和总金额
select ProductID , '总数'=count(*) , '总金额'=sum(UnitPrice*(1-Discount)*Quantity)
from [Order Details]
group by ProductID
--6. 查询购买过全部商品的顾客的ID和姓名
select Customers.CustomerID , Customers.ContactName
from  Customers,Orders
where  not exists
(
    select *
    from Products ,Orders o1
    where not exists 
    (
    select *
    from [Order Details] , Orders o2
    where Products.ProductID=[Order Details].ProductID  and [Order Details].OrderID=o2.OrderID and o2.CustomerID = Customers.CustomerID
    )
)
--(三)    对books数据库完成以下操作
--1.    查询各种类别的图书的类别和数量(包含目前没有图书的类别)
select BookType.TypeName , count(BookInfo.TypeID)as '数目'
from BookType  left join BookInfo on BookType.TypeID = BookInfo.TypeID
group by TypeName 
--2.    查询借阅了‘数据库基础’的读者的卡编号和姓名
select CardInfo.CardNo , CardInfo.Reader
from BorrowInfo ,CardInfo , BookInfo
where BorrowInfo.CardNo=CardInfo.CardNo 
and BorrowInfo.BookNo=BookInfo.BookNo 
and BookInfo.BookName = '数据库基础'
--3.    查询各个出版社的图书价格超过这个出版社图书的平均价格的图书的编号和名称。
select BookInfo.BookName , BookInfo.BookNo
from BookInfo
where BookInfo.Price>(
    select avg(Price)
    from BookInfo b1
    where b1.Publisher = BookInfo.Publisher
)
--4.    查询借阅过了全部图书的读者的编号和姓名
select CardInfo.CardNo ,CardInfo.Reader
from CardInfo
where not exists (
    select BookInfo.BookNo
    from BookInfo
    where not exists(
        select BorrowInfo.BookNo
        from BorrowInfo
        where BorrowInfo.CardNo = CardInfo.CardNo
        and BorrowInfo.BookNo = BookInfo.BookNo
    )
)
--5.    查询借阅图书包含李明所借的全部图书的读者的编号和姓名
select distinct CardInfo.CardNo ,CardInfo.Reader
from BorrowInfo,CardInfo
where  BorrowInfo.CardNo = CardInfo.CardNo and not exists (
    select *
    from BorrowInfo b1 , CardInfo c1
    where c1.CardNo = b1.CardNo and c1.Reader = '李明' 
    and not exists (
    select *
    from BorrowInfo b2,CardInfo c2
    where b2.BookNo = b1.BookNo and b2.CardNo =CardInfo.CardNo 
    )
)
--6.    查询借阅次数超过2次的读者的编号和姓名
select distinct CardInfo.CardNo ,CardInfo.Reader
from BorrowInfo,CardInfo
where BorrowInfo.CardNo=CardInfo.CardNo and
    CardInfo.CardNo in(
    select CardNo
    from BorrowInfo
    group by CardNo
    having count(*)>2
    )
--7.    查询借阅卡的类型为老师和研究生的读者人数

select '教师数'=count(case when TypeName ='教师'THEN 1 END), 
'研究生数'=count(case when TypeName ='研究生'THEN 1 END)
from CardInfo,CardType
where CardInfo.CTypeID=CardType.CTypeID
--8.    查询没有被借过的图书的编号和名称
select BookInfo.BookNo , BookInfo.BookName
from BookInfo
where BookInfo.BookNo not in(
select BookNo
from BorrowInfo
)
--9.    查询没有借阅过英语类型的图书的学生的编号和姓名
select CardNo,Reader
from CardInfo
where CardNo not in(
    select CardNo
    from BorrowInfo
    where BookNo in(
        select BookInfo.BookNo
        from BookInfo , BookType 
        where BookInfo.TypeID=BookType.TypeID and BookType.TypeName='英语'
        )
    )
--10.    查询借阅了‘计算机应用’类别的‘数据库基础’书的读者的编号读者以及该读者的借阅卡的类型。
select CardNo,Reader,CardType.TypeName
from CardInfo , CardType
where  CardInfo.CTypeID=CardType.CTypeID  and CardNo  in(
    select CardNo
    from BorrowInfo
    where BookNo in(
            select BookNo
            from BookInfo ,BookType
            where BookInfo.TypeID=BookType.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 item.ino ,iname
from item
where not exists(
    select *
    from market 
    where city='北京' and not exists (
    select *
    from sales 
    where mno=market.mno and ino=item.ino and price>10000
    )
)
--2.    列出在不同商场中最高售价和最低售价只差超过100 元的商品的商品号、最高售价和最低售价
select ino,max(price),min(price)
from sales
group by ino 
having max(price) - min(price)>100
--3.    列出售价超过该商品的平均售价的各个商品的商品号和售价
select s1.ino,s1.price
from sales s1
where s1.price>(
    select avg(s2.price)
    from sales s2
    where s2.ino = s1.ino)
--4.    查询销售了全部商品的商场号,商场名和城市
select mno,mname,city
from market 
where not exists
( select *
from item
where not exists (
select *
from sales
where sales.ino = item.ino and sales.mno=market.mno)
)
--5.    查询所有商场都销售了的商品的商品号和商品名。(用两种方法实现)
select ino,iname
from item
where not exists(
select *
from market 
where not exists (
select *
from sales 
where sales.ino = item.ino and sales.mno=market.mno))
--或者
select distinct item.ino,iname
from sales ,item
where sales.ino=item.ino 
group by item.ino,iname
having count(mno)=(
    select count(*)
    from    market
)
--6.    查询每个商场里价格最高的商品的名称(用两种方法做)
select mno,ino
from sales as s1
where price>=all(
    select price
    from sales as s2
    where s1.mno=s2.mno
)
order by mno
--或者
select mno,MAX(price) maxp into temp
from sales
group by mno
select sales.mno,sales.ino,sales.price
from sales join temp
on sales.mno=temp.mno and sales.price=temp.maxp
order by mno
drop table temp

你可能感兴趣的:(SQL)