有2张表,为父子关系表:
1 Employee 表
id Name Age
1 张三 31
2 李四 28
3 王五 40
…
2 Wages 表
Emp_id | 工资项目 | 工资金额 |
1 | 基本工资 | 1.00 |
1 | 补助 | 2.00 |
1 | 津贴 | 3.00 |
2 | 基本工资 | 4.00 |
2 | 补助 | 5.00 |
2 | 津贴 | 6.00 |
3 | 基本工资 | 7.00 |
3 | 补助 | 8.00 |
3 | 津贴 | 9.00 |
…
其中, Employee 表的 id 对应于 Wages 表的 Emp_id 是1对多的关系。
不用存储过程,能否直接用查询检索出下面的结果?
id Name Age 基本工资 补助 津贴 合计
1 张三 31 1 2 3 6
2 李四 28 4 5 6 15
3 王五 40 7 8 9 24
SELECT ID, NAME, age, t1.基本工资,t2.补助,t3.津贴,t4.合计
FROM employee,
(SELECT emp_id, 工资金额 AS 基本工资
FROM wages
WHERE 工资项目 = ‘基本工资’) t1,
(SELECT emp_id, 工资金额 AS 补助
FROM wages
WHERE 工资项目 = ‘补助’) t2,
(SELECT emp_id, 工资金额 AS 津贴
FROM wages
WHERE 工资项目 = ‘津贴’) t3,
(SELECT emp_id, SUM (工资金额) AS 合计
FROM wages
GROUP BY emp_id) t4
WHERE ID = t1.emp_id AND ID = t2.emp_id AND ID = t3.emp_id AND ID = t4.emp_id
建立一个数据库表student,数据表computer,字段名name,number,sex,SQL2000,flash,net ,其中SQL2000,flash,net设置为浮点型 float.
1、输出所有男生的成绩
use student
select yuyan as SQL数据库 ,flash as 网络动画,net as 计算机网络
from computer
where sex=‘男’
2、输出所有SQL成绩在90以上的女生的成绩
use student
select SQL2000 as SQL数据库 from computer
where sex='女’and SQL2000>=90
3、输出某一科目不合格所有的男生的成绩
use student
select yuyan as SQL数据库 ,flash as 网络动画,net as 计算机网络
from computer
where sex='男’and SQL2000<60 or flash<60 or net<60
4、计算并显示每位同学各科的总分和平均分,并按总分从高到低排序
use student
select SQL2000+flash+net as 总分,(SQL2000+flash+net/3)as 平均分
from computer
order by SQL2000+flash+net desc
5、输出所有计算机网络成绩在70-79之间的同学
use student
select * from computer
where flash between 70 and 79
6、输出所有姓“陈”和姓“李”的男生
use student
select * from computer
where sex='男’and left(name,1) in (‘李’, ‘陈’)
或者 use student
select * from computer
where sex=‘男’ (and name like '李__'or name like ‘陈__’)
7、输出所有学号为偶数的同学成绩
use student
select num as 学号,SQL2000 as SQL数据库 ,flash as 网络动画,net as
计算机网络 from computer
where num%2=0
8、输出Flash成绩最好的5位同学
use student
select top 5 * from computer
order by flash desc
9、更新同学的成绩,把计算机网络成绩在55-59之间的同学该科的成绩调整为60分
use student
update computer
set net=60
where net between 55 and 59
10、删除平均分最低的3位同学
use student
select top 3 *,(SQL2000+flash+net)/3 as 平均分 from computer
order by (SQL2000+flash+net)/3
delete from computer
where number in(033001,033003,033011)
11、统计成绩表中平均分为90以上(含90分)人数
use student
select count(*) from computer
where (SQL2000+flash+net)/3>=90
12、用SQL命令向成绩表添加一个新字段——C语言
use student
alter table computer
add c语言 float
问题描述:
已知关系模式:
s (sno,sname)学生关系。
sno 为学号,
sname 为姓名
c (cno,cname,cteacher) 课程关系。
cno 为课程号,
cname 为课程名,
cteacher 为任课教师
sc(sno,cno,scgrade) 选课关系。
scgrade 为成绩
要求实现如下5个处理:
1.找出没有选修过“李明”老师讲授课程的所有学生姓名
2.列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩
3.列出既学过“1”号课程,又学过“2”号课程的所有学生姓名
4.列出“1”号课成绩比“2”号同学该门课成绩高的所有学生的学号
5.列出“1”号课成绩比“2”号课成绩高的所有学生的学号及其“1”号课和“2”号课的成绩
- 找出没有选修过“李明”老师讲授课程的所有学生姓名
–实现代码:
select sname from s
where not exists(
select * from sc,c
where sc.cno=c.cno
and c.cteacher=‘李明’
and sc.sno=s.sno)
- 列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩
–实现代码:
select s.sno,s.sname,avg_scgrade=avg(sc.scgrade)
from s,sc,(
select sno
from sc
where scgrade<60
group by sno
having count(distinct cno)>=2
)a where s.sno=a.sno and sc.sno=a.sno
group by s.sno,s.sname
- 列出既学过“1”号课程,又学过“2”号课程的所有学生姓名
–实现代码:
select s.sno,s.sname
from s,(
select sc.sno
from sc,c
where sc.cno=c.cno
and c.cname in(‘1’,‘2’)
group by sno
having count(distinct cno)=2
)sc where s.sno=sc.sno
- 列出“1”号课成绩比“2”号同学该门课成绩高的所有学生的学号
–实现代码:
select s.sno,s.sname
from s,sc sc1,sc sc2
where sc1.cno=‘1’
and sc2.sno=‘2’
and sc1.cno=s.cno
and sc1.scgrade>sc2.scgrade
- 列出“1”号课成绩比“2”号课成绩高的所有学生的学号及其“1”号课和“2”号课的成绩
–实现代码:
select sc1.sno,[1号课成绩]=sc1.scgrade,[2号课成绩]=sc2.scgrade
from sc sc1,sc sc2
where sc1.cno=‘1’
and sc2.cno=‘2’
and sc1.sno=sc2.sno
and sc1.scgrade>sc2.scgrade
有四个表(关系):
product(maker,model,type)
maker:产品制造商,model:型号,type:产品分类(pc,printer,laptop))
pc(model,speed,ram,hd,cd,price)
laptop(model,speed,ram,hd,screen,price)
printer(model,color,price)
四个表中主键都是model,
求一SQL命令,能查出所有产品中价格(price)最高的产品。
select model
from product
where model in (select model
from (
select pc.model model,
pc.price price
from produce,
pc
where pc.model=product.model
union
select laptop.model model,
laptop.price price
from produce,
laptop
where laptop.model=product.model
union
select printer.model model,
printer.price price
from produce,
printer
where printer.model=product.model
)
where price in (select max(price)
from (
select pc.model model,
pc.price price
from produce,
pc
where pc.model=product.model
union
select laptop.model model,
laptop.price price
from produce,
laptop
w