数据查询2之多表查询
一、实验目的
多表查询语句可以分为连接查询、嵌套查询和组合查询。
本次实验了解SQL语言的SELECT语句对数据的查询,学会利用SQL Server 2000的查询分析器对数据库SPJ中的表进行简单查询
二、实验内容
1.熟练运用非等值连接和等值连接,外查询
2. 无关子查询
3. 有关子查询
4. 组合查询
三、实验任务
1、 查询与供应商S1提供相同零件的供应商编号和姓名
select sno,sname from s
where not exists(
select * from spj x
where sno='s1'and not exists(
select * from spj
where sno=s.sno and pno=x.pno))
2、查询金额balance最多的项目名称
select jname from j
where balance >= (select max(balance) from j )
select jname from j
where balance >= all(select balance from j)
3 分别使用等值连接,无关、有关子查询三种方式,查询使用了p3零件的工程名称
select jname from j
where jno in (select jno from spj where pno='p3')
4、使用三种方法求供应零件给工程J1,且零件颜色为红色的供应商名
select sname from s ,p,j,spj
where s.sno=spj.sno and p.pno=spj.pno and j.jno=spj.jno and j.jno='j1'and color='红色'
5、使用三种方法求至少使用了零件编号为p3和p5的工程编号
select distinct jno from j
where jno in ( select jno from spj where pno='p3 'and pno='p5')
6、通过子查寻,查寻不使用编号p3零件的工程编号jno和名称jname
select jno , jname from j
where jno not in ( select jno from spj where pno='p3 ')
7、求使用了全部零件的工程名称
select jname from j
where not exists(
select * from p
where not exists(
select * from spj
where jno=j.jno and pno=p.pno))
8、求至少使用了供应商's1'所供应的全部零件的工程号jno
select jno from j
where jno in (select jno from spj where jno=spj.jno and spj.sno= 's1')