《数据库语句练习及答案》由会员分享,可在线阅读,更多相关《数据库语句练习及答案(16页珍藏版)》请在金锄头文库上搜索。
1、实验二-、年龄小于20的为豆蔻,20-30之间为弱冠,30-40之间为儿立,40-50之间为不惑,50-60知命, 60-70,为花甲,其他为古稀 select *,case when age20 and age30 and age40 and age50 and age60 and age70 then 花甲 else 古稀end class_agefrom student-查找以_开头的学生的姓名 select *from student where sname like _%-以%开头的学生的姓名, select *from student where sname like %-查找第一
2、个字母是m或者n第二个字母为ykmb的学生的姓名, select *from student where sname like mnykmb%-查找不是以m或者n开头且第二个字母为a到z之间任何一个字母的学生的姓名及具体息, select *from student where sname like mma-z%-查找不是以a到f字母为首字母的学生的信息。 select *from student where sname like a-f%-3、练习各种连接的操作,诸如 join 、left jion、right join、full join以及crossjoin的应用对A、B两个表进行连接。
3、select *from A join B on A.Field_k=B.Field_kselect *from A left join B on A.Field_k=B.Field_kselect *from A right join B on A.Field_k=B.Field_kselect *from A full join B on A.Field_k=B.Field_kselect *from A cross join B-讲所有计算机学院的男同学信息显示出来并单独生成一个独立的表,表名字为jsjman。select *into jsjmanfrom studentwhere se
4、x=男 and dept=计算机学院实验二6-【例4.1】 查询员工表中所有员工的姓名和联系电话,可以写为:select 姓名,电话 from Employees-【例4.2】 查询员工表中的所有记录,程序为:select *from Employees-【例4.3】 查询进货表中的所有的生产厂商,去掉重复值,程序为:select distinct 生产厂商 from Goods-【例4.4】 查询进货表中商品名称、单价和数量的前4条记录,程序为:select top 4 商品名称,零售价,数量 from Goods-【例4.5】 使用列的别名,查询员工表中所有记录的员工编号(别名为numbe
5、r),姓名(别名为name)和电话(别名为telephone),select 编号 number,姓名 name, 电话 telephone from Employees-【例4.6】 查询各件商品的进货总金额,可以写为:select 商品名称,进货价*数量 from Goods-【例4.7】 在Employees表中查询姓名为王峰的员工的联系电话,程序为:select 姓名,电话 from Employees where 姓名=王峰-【例4.8】 查询笔记本电脑的进货信息,程序为:select *from Goods where 商品名称=笔记本电脑-【例4.9】 查询在2005年1月1日以
6、前销售的商品信息,可以写为:select 商品编号,数量,售出时间 from Sell where 售出时间2005-1-1-【例4.10】 查询进货总金额小于10000元的商品名称,可以写为:select 商品名称 from Goods where 进货价*数量10000-【例4.11】 查询2005年1月1日以前进货且进货价大于1000元的商品,可以写为:select 商品名称 from Goods where 进货时间1000-【例4.12】 查询“李”姓员工的基本信息,可以写为:select *from Employees where 姓名 like 李%-【例4.13】 查询零售价格
7、在2000到3000元之间的所有商品,可以写为:select *from Goods where 零售价=2000 and 零售价90select *from student,scores where student.sn=scores.sn and sname like刘_ and cn=001 and grade90-练习,交叉并笛卡尔成绩sql语句使用方法,通过 a,b实现。select *from A cross join Bselect * from a intersect select * from b-四、查找以“生”开头,且最后一个为“学”的课程的详细情况select *fro
8、m student,scores,coursewhere student.sn=scores.sn and = and cname like 生%学-五、查找选号课程的平均成绩、最大成绩、最小成绩,选课的人数select avg(grade)平均成绩 from scores where cn=001select max(grade)最大成绩 from scores where cn=001select min(grade)最小成绩 from scores where cn=001select count(cn)选课的人数 from scores where cn=001-六、分别查找选择了某一门课程的学生的人数、分别统计各个系的总人数,分别显示各系男女生总人数,男生人数大于3个的系select cname,count(sn) from course,scores where = group by cnameselect dept,count(sn)人数 from student group by deptselect dept,sex,count(sn) from student group by dept,sex order by deptselect dept,