实验二 SQL简单查询

目的:

掌握简单数据查询操作。

内容:

使用SQL语句完成各种查询条件的查询操作

步骤:

1、 以实验一中XSGL数据库为基础,对各表中的数据进行不同条件的查询;

包括的运算:投影、选择、比较运算符、逻辑运算符、字符匹配运算符、匹配列表范围、算术运算符、内部函数、排序、分组、分组函数使用

    1. 查询全体学生的学号和姓名

   

       select sno,sname from student;
    1. 查询全体学生的详细记录
           select * from student;
    1. 查询所有选修过课程的学生学号(不重复)

         

  select DISTINCT sno from sc;
    1. 查询考试不及格的学生学号(不重复)
select distinct sno from sc where grade < 60;
    1. 查询sdept不为’CS’的学生性别、年龄、系别
select ssex,sage,sdept from student where sdept != 'CS';
    1. 查询年龄18-20岁的学生学号、姓名、系别、年龄;
select sno,sname,sdept,sage from student where sage in (18,19,20);
    1. 查询姓刘或姓李的学生情况
        select * from student where sname like '刘%' or sname like '李%';
    1. 查询姓刘且名字为两个字的学生情况

    

    select * from student where sname like '刘_';
    1. 查询1983年以后出生的学生姓名。

      select sname from student where (2022-sage)>1983;
    1. 查询全体学生情况,查询结果按所在系升序排列,对同一系中的学  生按年龄降序排列。

         

  select * from student order by sdept,sage desc;
    1. 查询学生总人数。

       select count(*) from student;
    1. 查询选修了课程的学生人数。

   

       select count(distinct sno) from sc;
    1. 查询选修了7号课程的学生总人数和平均成绩

       

 select count(distinct sno),avg(grade) from sc where cno=7;
    1. 查找每门课的选修人数及平均成绩
    select cno,count(cno),avg(grade) from sc group by cno;
    1. 查找没有先修课的课程情况

  

     SELECT  * FROM Course

WHERE course.cno NOT IN(SELECT cno FROM sc);
  1. 以实验一的MySPJ数据库为基础,用SQL语句完成如下查询:

(1)求供应工程J1零件的供应商号码SNO;

    select distinct sno from spj where jno='J1';

(2)求供应工程J1零件P1的供应商号码SNO;

  select distinct sno from spj where jno='J1' and pno='P1';

(3)求供应工程J1零件为红色的供应商号码SNO;

    select DISTINCT sno from spj,p where spj.jno='J1' and spj.pno=p.pno and color='红';

(4)求没有使用天津供应商生产的红色零件的工程号JNO;

    select distinct jno from spj where jno not in (select jno from spj,s,p where s.city='天津' and color = '红' and spj.sno=s.sno and spj.pno=p.pno);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值