SQL的高級查詢
製作人:丁琪 QQ:854804038
一、判斷jwgl數據庫是否存在,如果存在則刪除,然後創建jwgl數據庫。
源代碼:if exists(select * from sys.sysdatabases where name='jwgl') drop database jwgl
create database jwgl
二、判斷jwgl數據庫中student表是否存在,如果存在則刪除,創建student表。
源代碼:if exists (select * from sys.sysobjects where name='student' and xtype='U') drop table student
else
create table student
(
student_id varchar(7) primary key not null,
studnet_name varchar(10) not null,
)
select * from student
三、刪除已存在的jwgl數據庫,導入jwgl數據庫,要求對jwgl數據庫做以下查詢
1、學生表(student)和班級信息表(class_info)表交叉連接
源代碼:use jwgl
go
select * from student,class_info
2、查詢學生編號、學生姓名、課程名稱及成績,沒有成績的學員姓名也要顯示。
源代碼:select student.student_id,student_name,coursename,grade from student
left outer join student_course on student.student_id=student_course.student_id
left outer join course on course.courseid=student_course.course_id
3、查詢學生編號、學生姓名、班級名稱、課程名稱及成績。
源代碼:select student.student_id,student_name,class_name,coursename from
student full outer join student_course on student.student_id=student_course.student_id
full outer join class_info on student.class_id=class_info.class_id
full outer join course on student_course.course_id=course.courseid
4、查詢年齡大於等於平均年齡的學生信息。
源代碼:select * from student where age>=(select avg(age) from student)
5、由學生表生成學生表1,並在學生表1中插入數據,查詢時合併學生表和學生表1。
源代碼:select top 0 * into dingqi from student
insert into dingqi (student_id,student_name,sex,age,birth)
select student_id,student_name,sex,age,birth from student
select * from dingqi
unin
select * from student
6、查詢大於20歲的女同學信息,並把查詢結果生成新表。
源代碼:use jwgl
go
select * into class1 from student where age>20 and sex=0
select * from class1