当前位置:文档之家› sql高级查询50题

sql高级查询50题

sql高级查询50题
sql高级查询50题

SQL高级查询——50句查询(含答案)

--一个题目涉及到的50个Sql语句

--(下面表的结构以给出,自己在数据库中建立表.并且添加相应的数据,数据要全面些. 其中Student表中,SId为学生的ID)

------------------------------------表结构--------------------------------------

--学生表tblStudent(编号StuId、姓名StuName、年龄StuAge、性别StuSex)

--课程表tblCourse(课程编号CourseId、课程名称CourseName、教师编号TeaId)

--成绩表tblScore(学生编号StuId、课程编号CourseId、成绩Score)

--教师表tblTeacher(教师编号TeaId、姓名TeaName)

---------------------------------------------------------------------------------

--问题:

--1、查询“001”课程比“002”课程成绩高的所有学生的学号;

Select StuId From tblStudent s1

Where (Select Score From tblScore t1 Where t1.StuId=s1.stuId And t1.CourseId='001')> (Select Score From tblScore t2 Where t2.StuId=s1.stuId And t2.CourseId='002')

--2、查询平均成绩大于60分的同学的学号和平均成绩;

Select StuId,Avg(Score) as AvgScore From tblScore

Group By StuId

Having Avg(Score)>60

--3、查询所有同学的学号、姓名、选课数、总成绩;

Select StuId,StuName,

SelCourses=(Select Count(CourseId) From tblScore t1 Where t1.StuId=s1.StuId),

SumScore=(Select Sum(Score) From tblScore t2 Where t2.StuId=s1.StuId)

From tblStudent s1

--4、查询姓“李”的老师的个数;

Select Count(*) From tblTeacher Where TeaName like '李%'

--5、查询没学过“叶平”老师课的同学的学号、姓名;

Select StuId,StuName From tblStudent

Where StuId Not In

(

Select StuID From tblScore sc

Inner Join tblCourse cu ON sc.CourseId=cu.CourseId

Inner Join tblTeacher tc ON cu.TeaId=tc.TeaId

Where tc.TeaName='叶平'

)

--6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;

Select StuId,StuName From tblStudent st

Where (Select Count(*) From tblScore s1 Where s1.StuId=st.StuId And

s1.CourseId='001')>0

And

(Select Count(*) From tblScore s2 Where s2.StuId=st.StuId And s2.CourseId='002')>0

--7、查询学过“叶平”老师所教的所有课的同学的学号、姓名;

Select StuId,StuName From tblStudent st Where not exists

(

Select CourseID From tblCourse cu Inner Join tblTeacher tc On cu.TeaID=tc.TeaID

Where tc.TeaName='叶平' And CourseID not in

(Select CourseID From tblScore Where StuID=st.StuID)

)

--8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;

Select StuId,StuName From tblStudent s1

Where (Select Score From tblScore t1 Where t1.StuId=s1.stuId And t1.CourseId='001')> (Select Score From tblScore t2 Where t2.StuId=s1.stuId And t2.CourseId='002')

--9、查询所有课程成绩小于60分的同学的学号、姓名;

Select StuId,StuName From tblStudent st

Where StuId Not IN

(Select StuId From tblScore sc Where st.StuId=sc.StuId And Score>60)

--10、查询没有学全所有课的同学的学号、姓名;

Select StuId,StuName From tblStudent st

Where (Select Count(*) From tblScore sc Where st.StuId=sc.StuId)<

(Select Count(*) From tblCourse)

--11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名;

------运用连接查询

Select DistInct st.StuId,StuName From tblStudent st

Inner Join tblScore sc ON st.StuId=sc.StuId

Where sc.CourseId IN (Select CourseId From tblScore Where StuId='1001')

------嵌套子查询

Select StuId,StuName From tblStudent

Where StuId In

(

Select Distinct StuId From tblScore Where CourseId In (Select CourseId From tblScore Where StuId='1001')

)

--12、查询至少学过学号为“1001”同学所有课程的其他同学学号和姓名;

Select StuId,StuName From tblStudent

Where StuId In

(

Select Distinct StuId From tblScore Where CourseId Not In (Select CourseId From tblScore Where StuId='1001')

--13、把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;(从子查询中获取父查询中的表名,这样也行????)

--创建测试表

Select * Into Sc From tblScore

go

Update Sc Set Score=(Select Avg(Score) From tblScore s1 Where

s1.CourseId=sc.CourseId)

Where CourseId IN

(Select CourseId From tblCourse cs INNER JOIN tblTeacher tc ON cs.TeaID=tc.TeaID WHERE TeaName ='叶平')

--14、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名;

Select StuID,StuName From tblStudent st

Where StuId <> '1002'

And

Not Exists(Select * From tblScore sc Where sc.StuId=st.StuId And CourseId Not In (Select CourseId From tblScore Where StuId='1002'))

And

Not Exists(Select * From tblScore Where StuId='1002' And CourseId Not In (Select CourseId From tblScore sc Where sc.StuId=st.StuId))

--15、删除学习“叶平”老师课的SC表记录;

Delete From tblScore Where CourseId IN

(Select CourseId From tblCourse cs INNER JOIN tblTeacher tc ON cs.TeaId=tc.TeaId Where tc.TeaName='叶平')

--16、向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“003”课程的同学学号、'002'号课的平均成绩;

Insert Into tblScore (StuId,CourseId,Score)

Select StuId,'002',(Select Avg(Score) From tblScore Where CourseId='002') From tblScore Where

StuId Not In (Select StuId From tblScore Where CourseId='003')

--17、按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、“英语”三门的课程成绩,按如下形式显示:学生ID,,数据库,企业管理,英语,有效课程数,有效平均分

Select StuId

,数据库=(Select Score From tblScore sc Inner Join tblCourse cs On

sc.CourseId=cs.CourseId Where CourseName='数据库' And sc.StuID=st.StuId)

,企业管理=(Select Score From tblScore sc Inner Join tblCourse cs On

sc.CourseId=cs.CourseId Where CourseName='企业管理' And sc.StuID=st.StuId)

,英语=(Select Score From tblScore sc Inner Join tblCourse cs On

sc.CourseId=cs.CourseId Where CourseName='英语' And sc.StuID=st.StuId)

,有效课程数=(Select Count(Score) From tblScore sc Inner Join tblCourse cs On

sc.CourseId=cs.CourseId Where (CourseName='数据库' or CourseName='企业管理' or CourseName='英语') And sc.StuID=st.StuId)

,有效平均分=(Select Avg(Score) From tblScore sc Inner Join tblCourse cs On

sc.CourseId=cs.CourseId Where (CourseName='数据库' or CourseName='企业管理' or CourseName='英语') And sc.StuID=st.StuId)

From tblStudent st

Order by 有效平均分Desc

--18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分

Select CourseId as 课程ID, 最高分=(Select Max(Score) From tblScore sc Where

sc.CourseId=cs.CourseId ),

最低分=(Select Min(Score) From tblScore sc Where sc.CourseId=cs.CourseId )

From tblCourse cs

--19、按各科平均成绩从低到高和及格率的百分数从高到低顺序(百分数后如何格式化为两位小数??)

Select 课程ID,平均分,及格率From

(Select CourseId as 课程ID, 平均分=(Select Avg(Score) From tblScore sc Where

sc.CourseId=cs.CourseId ),

及格率=Convert(varchar(10),((Select Count(*) From tblScore sc Where

sc.CourseId=cs.CourseId And sc.Score>=60)*10000/(Select Count(*) From tblScore sc Where sc.CourseId=cs.CourseId))/100)+'%'

From tblScore cs) as tmp

Group by 课程ID,平均分,及格率

Order by 平均分, Convert(float,substring(及格率,1,len(及格率)-1)) Desc

--20、查询如下课程平均成绩和及格率的百分数(用"1行"显示): 企业管理(001),马克思(002),OO&UML (003),数据库(004)

Select 课程ID=sc.CourseId,课程名称=cs.CourseName,平均成绩=Avg(Score)

,及格率=Convert(varchar(10),((Select Count(Score) From tblScore Where

CourseId=sc.CourseId And Score>=60)*10000/Count(Score))/100.0)+'%'

From tblScore sc

Inner Join tblCourse cs ON sc.CourseId=cs.CourseId

Where sc.CourseId like '00[1234]'

Group By sc.CourseId,cs.CourseName

--21、查询不同老师所教不同课程平均分从高到低显示

Select 课程ID=CourseId,课程名称=CourseName,授课教师=TeaName,平均成绩=(Select Avg(Score) From tblScore Where CourseId=cs.CourseId)

From tblCourse cs

Inner Join tblTeacher tc ON cs.TeaId=tc.TeaId

Order by 平均成绩Desc

--22、查询如下课程成绩第3 名到第6 名的学生成绩单:企业管理(001),马克思(002),UML (003),数据库(004)格式:[学生ID],[学生姓名],企业管理,马克思,UML,数据库,平均成绩

Select * From

(

Select Top 6 学生ID=StuId,学生姓名=StuName

,企业管理=(Select Score From tblScore sc Inner Join tblCourse cs On

sc.CourseId=cs.CourseId Where CourseName='企业管理' And sc.StuID=st.StuId)

,马克思=(Select Score From tblScore sc Inner Join tblCourse cs On

sc.CourseId=cs.CourseId Where CourseName='马克思' And sc.StuID=st.StuId)

,UML=(Select Score From tblScore sc Inner Join tblCourse cs On

sc.CourseId=cs.CourseId Where CourseName='UML' And sc.StuID=st.StuId)

,数据库=(Select Score From tblScore sc Inner Join tblCourse cs On

sc.CourseId=cs.CourseId Where CourseName='数据库' And sc.StuID=st.StuId)

,平均成绩=(Select Avg(Score) From tblScore sc Inner Join tblCourse cs On

sc.CourseId=cs.CourseId Where (CourseName='数据库' or CourseName='企业管理' or CourseName='UML'or CourseName='马克思') And sc.StuID=st.StuId)

,排名=Row_Number() Over(Order by(Select Avg(Score) From tblScore sc Inner Join tblCourse cs On sc.CourseId=cs.CourseId Where (CourseName='数据库' or CourseName='企业管理' or CourseName='UML'or CourseName='马克思') And

sc.StuID=st.StuId) DESC)

From tblStudent st

Order by 排名

) as tmp

Where 排名between 3 And 6

--23、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60] Select 课程ID=CourseId, 课程名称=CourseName

,[100-85]=(Select Count(*) From tblScore sc Where CourseId=cs.CourseId And Score between 85 And 100)

,[85-70]=(Select Count(*) From tblScore sc Where CourseId=cs.CourseId And Score between 70 And 84)

,[70-60]=(Select Count(*) From tblScore sc Where CourseId=cs.CourseId And Score between 60 And 69)

,[<60]=(Select Count(*) From tblScore sc Where CourseId=cs.CourseId And Score <60) From tblCourse cs

--24、查询学生平均成绩及其名次

Select 学号=st.StuId, 姓名=StuName,平均成绩=sc.AvgScore,名次=(Dense_Rank() Over(Order by sc.AvgScore Desc)) From tblStudent st

Inner Join (Select StuId,Avg(Score) as AvgScore From tblScore Group by StuId) as sc On sc.StuId=st.StuId

Order by 学号

--25、查询各科成绩前三名的记录:(不考虑成绩并列情况)

Select 学号=StuId,课程号=CourseId,分数=Score

From

(Select Row_Number() Over(order by CourseId,Score Desc) as i,* From tblScore) as tmp --得到一个临时的排名表,其中i表示编号

Where i In

(

Select Top 3 i From (Select Row_Number() Over(order by CourseId,Score Desc) as i,* From tblScore) as t1 Where t1.CourseId=tmp.CourseId

)

--26、查询每门课程被选修的学生数

Select 课程ID=CourseId,选修人数=(Select Count(*) From (Select Distinct StuId From tblScore Where CourseId=cs.CourseId) as tmp)

From tblCourse cs

--27、查询出只选修了一门课程的全部学生的学号和姓名

Select 学号=StuId,姓名=StuName

From tblStudent st

Where (Select Count(*) From (Select Distinct CourseId From tblScore Where

StuId=st.StuId) as tmp)=1

--28、查询男生、女生人数

Select 男生人数=(select Count(*) From tblStudent Where StuSex='男'),

女生人数=(select Count(*) From tblStudent Where StuSex='女')

--29、查询姓“张”的学生名单

Select * From tblStudent Where StuName like '张%'

--30、查询同名同性学生名单,并统计同名人数

Select Distinct 学生姓名=StuName,同名人数=(Select Count(*) From tblStudent s2

Where s2.StuName=st.StuName) From tblStudent st

Where (Select Count(*) From tblStudent s2 Where s2.StuName=st.StuName)>=2

--31、1981年出生的学生名单(注:Student表中Sage列的类型是datetime)

Select * From tblStudent Where Year(Sage)=1981

--32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列

Select 课程ID=CourseId,课程名称=CourseName,平均成绩=(Select Avg(Score) From tblScore Where CourseId=cs.CourseId)

From tblCourse cs

Order by 平均成绩,CourseId Desc

--33、查询平均成绩大于85的所有学生的学号、姓名和平均成绩

Select 学号=StuId,姓名=StuName,平均成绩=(Select Avg(Score) From tblScore Where StuId=st.StuId) From tblStudent st

Where (Select Avg(Score) From tblScore Where StuId=st.StuId)>85

--34、查询课程名称为“数据库”,且分数低于60的学生姓名和分数

Select 姓名=StuName,分数=Score From tblScore sc

Inner Join tblStudent st On sc.StuId=st.StuId

Inner Join tblCourse cs On sc.CourseId=cs.CourseId

Where CourseName='数据库' And Score<60

--35、查询所有学生的选课情况;

Select 学号=StuId,选课数=(Select Count(*) From (Select Distinct CourseId From tblScore Where StuId=st.StuId) as tmp)

From tblStudent st

Select distinct 姓名=StuName,选修课程=CourseName From tblScore sc

Inner Join tblStudent st On sc.StuId=st.StuId

Inner Join tblCourse cs On sc.CourseId=cs.CourseId

--36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;

Select 姓名=StuName,课程名称=CourseName,分数=Score From tblScore sc

Inner Join tblStudent st On sc.StuId=st.StuId

Inner Join tblCourse cs On sc.CourseId=cs.CourseId

Where Score>=70

--37、查询不及格的课程,并按课程号从大到小排列

Select * From tblScore Where Score<60 order by CourseId Desc

--38、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;

Select StuId,StuName From tblStudent

Where StuId in

(Select StuId From tblScore Where CourseId='003' And Score>=80)

--39、求选了课程的学生人数

Select 选了课程的学生人数=Count(*) From tblStudent st Where StuId IN (Select StuID From tblScore)

--40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩

Select CourseId,CourseName

,该科最高学生=(Select StuName From tblStudent Where StuId in (Select Top 1 StuID From tblScore Where CourseId=cs.CourseId Order by Score Desc))

,成绩=(Select Top 1 Score From tblScore Where CourseId=cs.CourseId Order by Score Desc)

From tblCourse cs Inner Join tblTeacher tc ON cs.TeaId=tc.TeaId

Where TeaName='叶平'

--41、查询各个课程及相应的选修人数

Select 课程ID=CourseId,选修人数=(Select Count(*) From (Select Distinct StuId From tblScore Where CourseId=cs.CourseId) as tmp)

From tblCourse cs

--42、查询不同课程成绩相同的学生的学号、课程号、学生成绩

Select 学号=StuId, 课程号=CourseId, 成绩=Score From tblScore sc

Where Exists (Select * From tblScore Where Score=sc.Score And StuId=sc.StuId And CourseId <>sc.CourseId)

Order by 学号,成绩

--43、查询每门功成绩最好的前两名

Select 课程号=CourseId,

第1名=(Select Top 1 StuId From tblScore Where CourseId=cs.CourseId Order by Score DESC),

第2名=(Select Top 1 StuID From (Select Top 2 StuId,Score From tblScore Where CourseId=cs.CourseId Order by Score DESC) as tmp Order by Score)

From tblCourse cs

--44、统计每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

Select 课程ID=CourseId,选修人数=(Select Count(*) From (Select Distinct StuId From tblScore Where CourseId=cs.CourseId) as tmp)

From tblCourse cs

Where (Select Count(*) From (Select Distinct StuId From tblScore Where

CourseId=cs.CourseId) as tmp)>=10

Order by 选修人数DESC, 课程ID

--45、检索至少选修两门课程的学生学号

Select StuId from tblScore Group by Stuid having Count(*)>=2 --没有重复课程数据时可用此方法

--有重复课程时用此方法(如补考)

Select StuId from tblStudent st Where

(Select Count(*) From (Select Distinct CourseId From tblScore Where StuId=st.StuId) as tmp)>=2

--46、查询全部学生都选修的课程的课程号和课程名

Select CourseId,CourseName From tblCourse cs

Where Not Exists

(

Select * From tblStudent Where StuId Not In --没学过本课程的学生是否存在

(Select StuId From tblScore Where CourseId=cs.CourseId)

)

--47、查询没学过“叶平”老师讲授的任一门课程的学生姓名

Select StuId,StuName From tblStudent

Where StuId Not In

(

Select StuID From tblScore sc

Inner Join tblCourse cu ON sc.CourseId=cu.CourseId

Inner Join tblTeacher tc ON cu.TeaId=tc.TeaId

Where tc.TeaName='叶平'

)

--48、查询两门以上不及格课程的同学的学号及其平均成绩

Select StuID as 学号,Avg(Score) as 平均成绩From tblScore sc

Where (Select Count(*) From tblScore s1 Where s1.StuId=sc.StuId And Score<60)>=2 Group By StuId

--49、检索“004”课程分数小于60,按分数降序排列的同学学号(ok)

Select StuID,Score From tblScore Where CourseId='004' And Score<60 Order by Score Desc

--50、删除“002”同学的“001”课程的成绩

Delete From SC Where StuId='1002' And CourseId='001'

----------------------SC为删除数据临时表

Select * INTO SC From tblScore

Select * from sc Where stuId='1018'

Insert Sc(Stuid,courseId,Score) Select StuID,'009',74 From tblStudent

/********************************* 建库建表建约束,插入测试数

据******************************************/

Use master

go

if db_id('MySchool') is not null

Drop Database MySchool

Create Database MySchool

go

Use MySchool

go

create table tblStudent

(

StuId varchar(5) primary key,

StuName nvarchar(10) not null,

StuAge int,

StuSex nchar(1) not null

)

create table tblTeacher

(

TeaId varchar(3) primary key,

TeaName varchar(10) not null

)

create table tblCourse

(

CourseId varchar(3) primary key,

CourseName nvarchar(20) not null,

TeaId varchar(3) not null foreign key references tblTeacher(teaId)

)

create table tblScore

(

StuId varchar(5) not null foreign key references tblStudent(stuId),

CourseId varchar(3) not null foreign key references tblCourse(CourseId),

Score float

)

----------------------------------表结构----------------------------------------------------

--学生表tblStudent(编号StuId、姓名Stuname、年龄Stuage、性别Stusex)

--课程表tblCourse(课程编号CourseId、课程名称CourseName、教师编号TeaId)--成绩表tblScore(学生编号StuId、课程编号CourseId、成绩Score)

--教师表tblTeacher(教师编号TeaId、姓名TeaName)

--------------------------------插入数据-------------------------------------------------

insert into tblStudent

select '1000','张无忌',18,'男' union

select '1001','周芷若',19,'女' union

select '1002','杨过',19,'男' union

select '1003','赵敏',18,'女' union

select '1004','小龙女',17,'女' union select '1005','张三丰',18,'男' union select '1006','令狐冲',19,'男' union select '1007','任盈盈',20,'女' union select '1008','岳灵珊',19,'女' union select '1009','韦小宝',18,'男' union select '1010','康敏',17,'女' union select '1011','萧峰',19,'男' union select '1012','黄蓉',18,'女' union select '1013','郭靖',19,'男' union select '1014','周伯通',19,'男' union select '1015','瑛姑',20,'女' union select '1016','李秋水',21,'女' union select '1017','黄药师',18,'男' union select '1018','李莫愁',18,'女' union select '1019','冯默风',17,'男' union select '1020','王重阳',17,'男' union select '1021','郭襄',18,'女'

go

insert into tblTeacher

select '001','姚明' union

select '002','叶平' union

select '003','叶开' union

select '004','孟星魂' union

select '005','独孤求败' union select '006','裘千仞' union

select '007','裘千尺' union

select '008','赵志敬' union

select '009','阿紫' union

select '010','郭芙蓉' union

select '011','佟湘玉' union

select '012','白展堂' union

select '013','吕轻侯' union

select '014','李大嘴' union

select '015','花无缺' union

select '017','乔丹'

go

insert into tblCourse

select '001','企业管理','002' union

select '002','马克思','008' union

select '003','UML','006' union

select '004','数据库','007' union

select '005','逻辑电路','006' union

select '006','英语','003' union

select '007','电子电路','005' union

select '008','毛泽东思想概论','004' union select '009','西方哲学史','012' union select '010','线性代数','017' union

select '011','计算机基础','013' union select '012','AUTO CAD制图','015' union select '013','平面设计','011' union

select '014','Flash动漫','001' union select '015','Java开发','009' union select '016','C#基础','002' union

select '017','Oracl数据库原理','010'

go

insert into tblScore

select '1001','003',90 union

select '1001','002',87 union

select '1001','001',96 union

select '1001','010',85 union

select '1002','003',70 union

select '1002','002',87 union

select '1002','001',42 union

select '1002','010',65 union

select '1003','006',78 union

select '1003','003',70 union

select '1003','005',70 union

select '1003','010',85 union select '1003','011',21 union select '1004','007',90 union select '1004','002',87 union select '1005','001',23 union select '1006','015',85 union select '1006','006',46 union select '1006','003',59 union select '1006','004',70 union select '1006','001',99 union select '1007','011',85 union select '1007','006',84 union select '1007','003',72 union select '1007','002',87 union select '1008','001',94 union select '1008','012',85 union select '1008','006',32 union select '1009','003',90 union select '1009','002',82 union select '1009','001',96 union select '1009','010',82 union select '1009','008',92 union select '1010','003',90 union select '1010','002',87 union select '1010','001',96 union

select '1011','009',24 union select '1011','009',25 union

select '1012','003',30 union select '1013','002',37 union select '1013','001',16 union select '1013','007',55 union select '1013','006',42 union select '1013','012',34 union

select '1002','004',55 union select '1004','004',42 union select '1008','004',34 union select '1013','016',86 union select '1013','016',44 union select '1000','014',75 union select '1002','016',100 union select '1004','001',83 union select '1008','013',97

go

常用SQL语句大全

常用SQL语句大全 一、基础 1、说明:创建数据库 CREATE DATABASE database-name 2、说明:删除数据库 DROP database dbname 3、说明:备份sql server --- 创建备份数据的device USE master EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat' --- 开始备份 BACKUP DATABASE pubs TO testBack 4、说明:创建新表 create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..) 根据已有的表创建新表: A:create table tab_new like tab_old (使用旧表创建新表) B:create table tab_new as select col1,col2…from tab_old definition only 5、说明:删除新表 DROP table tabname 6、说明:增加一个列 Alter table tabname add column col type 注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。 7、说明:添加主键:Alter table tabname add primary key(col) 说明:删除主键:Alter table tabname DROP primary key(col) 8、说明:创建索引:create [unique] index idxname on tabname(col….) 删除索引:DROP index idxname 注:索引是不可更改的,想更改必须删除重新建。 9、说明:创建视图:create view viewname as select statement 删除视图:DROP view viewname 10、说明:几个简单的基本的sql语句 选择:select * from table1 where 范围 插入:insert into table1(field1,field2) values(value1,value2) 删除:delete from table1 where 范围 更新:update table1 set field1=value1 where 范围 查找:select * from table1 where field1 like ’%value1%’---like的语法很精妙,查资料! 排序:select * from table1 order by field1,field2 [desc] 总数:select count as totalcount from table1 求和:select sum(field1) as sumvalue from table1 平均:select avg(field1) as avgvalue from table1 最大:select max(field1) as maxvalue from table1 最小:select min(field1) as minvalue from table1 11、说明:几个高级查询运算词

ORACLE常用SQL语句大全

ORACLE常用SQL语句大全 一、基础 1、说明:创建数据库 CREATE DATABASE database-name 2、说明:删除数据库 drop database dbname 3、说明:备份sql server --- 创建备份数据的 device USE master EXEC sp_addumpdevice 'disk', 'testBack', 'c:/mssql7backup/MyNwind_1.dat' --- 开始备份 BACKUP DATABASE pubs TO testBack 4、说明:创建新表 create table tabname(col1 type1 [not null] [primary key],col2 type2 [not nul l],..) 根据已有的表创建新表: A:select * into table_new from table_old (使用旧表创建新表) B:create table tab_new as select col1,col2… from tab_old definition only<仅适用于Oracle> 5、说明:删除表 drop table tablename

6、说明:增加一个列,删除一个列 A:alter table tabname add column col type B:alter table tabname drop column colname 注:DB2DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。 7、添加主键: Alter table tabname add primary key(col) 删除主键: Alter table tabname drop primary key(col) 8、创建索引:create [unique] index idxname on tabname(col….) 删除索引:drop index idxname 注:索引是不可更改的,想更改必须删除重新建。 9、创建视图:create view viewname as select statement 删除视图:drop view viewname 10、几个简单的基本的sql语句 选择:select * from table1 where 范围 插入:insert into table1(field1,field2) values(value1,value2) 删除:delete from table1 where 范围 更新:update table1 set field1=value1 where 范围 查找:select * from table1 where field1 like ’%value1%’ ---like的语法很精妙,查资料! 排序:select * from table1 order by field1,field2 [desc] 总数:select count as totalcount from table1 求和:select sum(field1) as sumvalue from table1 平均:select avg(field1) as avgvalue from table1 最大:select max(field1) as maxvalue from table1 最小:select min(field1) as minvalue from table1 11、几个高级查询运算词 A:UNION 运算符 UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。 B:EXCEPT 运算符 EXCEPT 运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。当ALL 随 EXCEPT 一起使用时 (EXCEPT ALL),不消除重复行。 C:INTERSECT 运算符 INTERSECT 运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当 ALL 随 INTERSECT 一起使用时 (INTERSECT ALL),不消除重复行。 注:使用运算词的几个查询结果行必须是一致的。 12、使用外连接

SQL常用语句+举例

SQL 常用语句+举例 相关表: 1. distinct: 剔除重复记录 例:select distinct stroe_name from Store_information 结果: 2. And / or: 并且/或 例:在表中选出所有sales 高于$1000或是sales 在$275及$500之间的记录 Select store_name ,sales from Store_information Where sales>1000 Or (sales>275 and sales <500) 3. 例:在表中查找store_name 包含 Los Angeles 或San Diego 的记录 Select * from Store_information where store_name in (‘Los Angeles ’,’San Diego ’) 结果: 4. Between : 可以运用一个范围抓出表中的值

与in 的区别:in 依照一个或数个不连续的值的限制抓出表中的值 例:查找表中介于Jan-06-1999 及Jan-10-1999 中的记录 Select * from Store_information where date between ‘Jan-06-1999’ and ‘Jan-10-1999’ 结果: 5. Like : 让我们依据一个套式来找出我们要的记录 套式通常包含: ’A_Z ’: 所有以A 开头,中间包含一个字符,以Z 结尾的字串 ’ABC%’: 所有以ABC 起头的字串 ’%XYZ ’: 所有以XYZ 结尾的字串 ’%AN%’: 所有包含AN 的字串 例:Select * from Store_information where store_name like ‘%An%’ 结果: 6. Order by: 排序,通常与ASC (从小到大,升序)、DESC (从大到小,降序)结合使用 当排序字段不止一个时,先依据字段1排序,当字段1有几个值相同时,再依据字段2排序 例:表中sales 由大到小列出Store_information 的所有记录 Select Store_name, sales,date from Store_information order by sales desc 结果: 7. 函数:AVG (平均值)、COUNT (计数)、MAX (最大值)、MIN (最小值)、SUM(求和) 语句:select 函数名(字段名) from 表名 例:求出sales 的总和 Select sum(sales) from Store_information 结果 8. COUNT (计数) 例:找出Store_information 表中 有几个store_name 值不是空的记录

SQL Server 2005 数据查询练习题及答案

Sql 语句答案: 1. select姓名,单位 from读者 where姓名like'李%' 2. select书名,出版单位 from图书 3. select出版单位,书名,单价 from图书 where出版单位='高等教育出版社' order by单价desc 4. select书名,出版单位,单价 from图书 where单价between 10.00 and 20.00 order by出版单位,单价asc 5. select书名,作者 from图书 where书名like'计算机%' 6. select借阅.总编号,借书证号 from图书,借阅 where图书.总编号=借阅.总编号and借阅.总编号in('112266','449901') 7.select distinct姓名,单位 from读者inner join借阅 on借阅.借书证号=读者.借书证号 8. select书名,姓名,借书日期 from图书inner join借阅 on图书.总编号=借阅.总编号 join读者 on借阅.借书证号=读者.借书证号 where读者.姓名like'李%' 9. select distinct读者.借书证号,姓名,单位 from借阅inner join读者 on借阅.借书证号=读者.借书证号 where借阅.借书日期>='1997-10-1' 10. select借书证号

from借阅 where总编号in(select总编号 from图书 where书名='FoxPro大全') 11. select姓名,单位,借书日期 from借阅,读者 where借阅.借书证号=读者.借书证号and借书日期=(select借书日期 from借阅,读者 where借阅.借书证号=读者.借书证号and姓名='赵正义') 12. select distinct借书证号,姓名,单位 from读者 where借书证号not in(select借书证号 from借阅 where借书日期>='1997-07-01') 13. select max(单价)最高单价,min(单价)as最低单价,avg(单价)as平均单价from图书 where出版单位='科学出版社' 14. select count(借书证号) from借阅 where借书证号in(select借书证号 from读者 where单位='信息系') 15. select出版单位,max(单价)最高价格,min(单价)as最低价格,count(*)册数from图书 group by出版单位 16. select单位,count(借阅.借书证号) from借阅,读者 where借阅.借书证号in(select借书证号 from读者) group by单位 17. select姓名,单位 from读者 where借书证号in(select借书证号 from借阅 group by借书证号 having count(*)>=2)

数据库基本SQL语句大全

数据库基本SQL语句大全 数据库基本----SQL语句大全 一、基础 1、说明:创建数据库 Create DATABASE database-name 2、说明:删除数据库 drop database dbname 3、说明:备份sql server --- 创建备份数据的device USE master EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat' --- 开始备份 BACKUP DATABASE pubs TO testBack 4、说明:创建新表 create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..) 根据已有的表创建新表: A:create table tab_new like tab_old (使用旧表创建新表) B:create table tab_new as select col1,col2…from tab_old definition only 5、说明:删除新表 drop table tabname 6、说明:增加一个列 Alter table tabname add column col type 注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。 7、说明:添加主键:Alter table tabname add primary key(col) 说明:删除主键:Alter table tabname drop primary key(col) 8、说明:创建索引:create [unique] index idxname on tabname(col….) 删除索引:drop index idxname 注:索引是不可更改的,想更改必须删除重新建。 9、说明:创建视图:create view viewname as select statement 删除视图:drop view viewname 10、说明:几个简单的基本的sql语句 选择:select * from table1 where 范围 插入:insert into table1(field1,field2) values(value1,value2) 删除:delete from table1 where 范围 更新:update table1 set field1=value1 where 范围

50个常用sql语句实例(学生表 课程表 成绩表 教师表)

Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师表 create table Student(S# varchar(20),Sname varchar(10),Sage int,Ssex varchar(2)) 前面加一列序号: if exists(select table_name from information_schema.tables where table_name='Temp_Table') drop table Temp_Table go select 排名=identity(int,1,1),* INTO Temp_Table from Student go select * from Temp_Table go drop database [ ] --删除空的没有名字的数据库 问题: 1、查询“”课程比“”课程成绩高的所有学生的学号; select a.S# from (select s#,score from SC where C#='001') a,(select s#,score from SC where C#='002') b where a.score>b.score and a.s#=b.s#; 2、查询平均成绩大于分的同学的学号和平均成绩; select S#,avg(score) from sc group by S# having avg(score) >60; 3、查询所有同学的学号、姓名、选课数、总成绩; select Student.S#,Student.Sname,count(SC.C#),sum(score) from Student left Outer join SC on Student.S#=SC.S# group by Student.S#,Sname 4、查询姓“李”的老师的个数; select count(distinct(Tname)) from Teacher where Tname like '李%'; 5、查询没学过“叶平”老师课的同学的学号、姓名; select Student.S#,Student.Sname from Student

sql查询练习题含答案

--(1)查询20号部门的所有员工信息。 select * from emp e where e.deptno=20; --(2)查询奖金(COMM)高于工资(SAL)的员工信息。 select * from emp where comm>sal; --(3)查询奖金高于工资的20%的员工信息。 select * from emp where comm>sal*0.2; --(4)查询10号部门中工种为MANAGER和20号部门中工种为CLERK的员工的信息。select * from emp e where (e.deptno=10 and e.job='MANAGER') or (e.deptno=20 and e.job='CLERK') --(5)查询所有工种不是MANAGER和CLERK, --且工资大于或等于2000的员工的详细信息。 select * from emp where job not in('MANAGER','CLERK') and sal>=2000; --(6)查询有奖金的员工的不同工种。 select * from emp where comm is not null; --(7)查询所有员工工资和奖金的和。 select (e.sal+nvl(https://www.doczj.com/doc/ca9097691.html,m,0)) from emp e; --(8)查询没有奖金或奖金低于100的员工信息。 select * from emp where comm is null or comm<100; --(9)查询员工工龄大于或等于10年的员工信息。 select * from emp where (sysdate-hiredate)/365>=10; --(10)查询员工信息,要求以首字母大写的方式显示所有员工的姓名。 select initcap(ename) from emp; select upper(substr(ename,1,1))||lower(substr(ename,2)) from emp; --(11)显示所有员工的姓名、入职的年份和月份,按入职日期所在的月份排序, --若月份相同则按入职的年份排序。 select ename,to_char(hiredate,'yyyy') year,to_char(hiredate,'MM') month from emp order by month,year; --(12)查询在2月份入职的所有员工信息。 select * from emp where to_char(hiredate,'MM')='02' --(13)查询所有员工入职以来的工作期限,用“**年**月**日”的形式表示。 select e.ename,floor((sysdate-e.hiredate)/365)||'年' ||floor(mod((sysdate-e.hiredate),365)/30)||'月' ||floor(mod(mod((sysdate-e.hiredate),365),30))||'日' from emp e; --(14)查询从事同一种工作但不属于同一部门的员工信息。

大数据库基本SQL语句大全

数据库基本_SQL语句大全 学会数据库是很实用D~~记录一些常用的sql语句...有入门有提高有见都没见过的...好全...收藏下... 其实一般用的就是查询,插入,删除等语句而已....但学学存储过程是好事...以后数据方面的东西就不用在程序里搞喽..而且程序与数据库只要一个来回通讯就可以搞定所有数据的操作.... 一、基础 1、说明:创建数据库 Create DATABASE database-name 2、说明:删除数据库 drop database dbname 3、说明:备份sql server --- 创建备份数据的device USE master EXEC sp_addumpdevice ‘disk‘, ‘testBack‘, ‘c:\mssql7backup\MyNwind_1.dat‘ --- 开始备份 BACKUP DATABASE pubs TO testBack 4、说明:创建新表 create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)

根据已有的表创建新表: A:create table tab_new like tab_old (使用旧表创建新表) B:create table tab_new as select col1,col2…from tab_old definition only 5、说明:删除新表 drop table tabname 6、说明:增加一个列 Alter table tabname add column col type 注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。 7、说明:添加主键:Alter table tabname add primary key(col)说明:删除主键:Alter table tabname drop primary key(col) 8、说明:创建索引:create [unique] index idxname on tabname(col….) 删除索引:drop index idxname 注:索引是不可更改的,想更改必须删除重新建。 9、说明:创建视图:create view viewname as select statement 删除视图:drop view viewname 10、说明:几个简单的基本的sql语句 选择:select * from table1 where 范围 插入:insert into table1(field1,field2) values(value1,value2) 删除:delete from table1 where 范围

实验3 SQL的高级查询

实验3 SQL的高级查询 一、实验目的 1.继续掌握基本的SELECT查询及其相关子句的使用 2.掌握复杂的SELECT查询、如多表查询,子查询,连接,分组和嵌套查询 3.掌握SQL中的集合并运算union 4.掌握SQL中元组的插入、修改、删除操作(insert,update,delete)。 二、预备知识: SQL中的连接操作: 假设R与K是基本数据表。基本表的连接操作可以分为五类: ●内连接 R inner join K on <条件> 只返回满足条件的行 ●左外连接 R left join K on<条件>返回满足条件的行及左表R中所有的行。如果左表的某条 记录在右表中没有匹配记录,则在查询结果中右表的所有选择属性列用NULL填充。 ●右外连接 R right join K on <条件>返回满足条件的行及右表K中所有的行 ●完全外连接 R full join K on <条件>返回满足条件的行及左右表R,K所有的行。当某条记录 在另一表中没有匹配记录,则在查询结果中对应的选择属性列用NULL填充。 ●交叉连接R CROSS JOIN K:相当于广义笛卡尔积。不能加筛选条件。 三、实验环境 1.个人计算机或局域网 2.Windows 2000操作系统 3.SQL Server 2000数据库管理系统 四、实验步骤 1.启动查询分析器,选择学生选课数据库 2.在学生选课数据库中分别使用SQL命令完成指定任务。 五、实验内容: 1.查询所有学生的情况以及他们选修的课程和得分 2.查询所有学生的姓名以及他们选修的课程名和得分 3.检索已经选了课程的学生的姓名、选修课程和成绩(提示:使用inner join) 4.检索每个学生的姓名,选修课程和成绩,没有选修的同学也列出。 5.列出所有学生所有可能的选课情况(提示:使用cross join) 6.检索每个学生的姓名、选修课程,没有选修的同学和没有被选修的课程也列出 7.求学生的总人数和平均年龄 8.求选修了各课程的学生的人数 9.对计算机系的学生按课程列出选修了该课程的学生的人数 10.查询每个学生的平均成绩 11.求选修课程超过或等于2门的学生的学号 12.查询学生的学号、姓名、所有学生所有课程的最高成绩 13.查询学生的学号、课程号及对应成绩与所有学生中所有课程的最高成绩的差值 14.查询陈小红同学的学号及所选修的课程号 15.查询没有选修C02课程的学生姓名

50个常用的SQL语句练习

基本信息Student(`S#`,Sname,Sage,Ssex) 学生表 Course(`C#`,Cname,`T#`) 课程表 SC(`S#`,`C#`,score) 成绩表 Teacher(`T#`,Tname) 教师表 问题: 1、查询“001”课程比“002”课程成绩高的所有学生的学号; select a.`S#` from (select `S#`,score from SC where `C#`='001') a,(select `S#`,score from SC where `C#`='002') b where a.score>b.score and a.`S#`=b.`S#`; ↑一张表中存在多对多情况的 2、查询平均成绩大于60分的同学的学号和平均成绩; 答案一:select `S#`,avg(score) from sc group by `S#` having avg(score) >60; ↑一对多,对组进行筛选 答案二:SELECT s ,scr FROM (SELECT sc.`S#` s,AVG(sc.`score`) scr FROM sc GROUP BY sc.`S#`) rs WHERE rs.scr>60 ORDER BY rs.scr DESC ↑嵌套查询可能影响效率 3、查询所有同学的学号、姓名、选课数、总成绩; 答案一:select Student.`S#`,Student.Sname,count(`C#`),sum(score) from Student left Outer join SC on Student.`S#`=SC.`S#` group by Student.`S#`,Sname ↑如果学生没有选课,仍然能查出,显示总分null(边界情况) 答案二:SELECT student.`S#`,student.`Sname`,COUNT(sc.`score`) 选课数,SUM(sc.`score`) 总分FROM Student,sc WHERE student.`S#`=sc.`S#` GROUP BY sc.`S#` ↑如果学生没有选课,sc表中没有他的学号,就查不出该学生,有缺陷! 4、查询姓“李”的老师的个数; select count(distinct(Tname)) from Teacher where Tname like '李%'; 5、查询没学过“叶平”老师课的同学的学号、姓名; select Student.`S#`,Student.Sname from Student where `S#` not in (select distinct(SC.`S#`) from SC,Course,Teacher where SC.`C#`=Course.`C#` and Teacher.`T#`=Course.`T#` and Teacher.Tname='叶平'); ↑反面思考Step1:先找学过叶平老师课的学生学号,三表联合查询 Step2:在用not in 选出没学过的 Step3:distinct以防叶平老师教多节课;否则若某同学的几节课都由叶平教,学号就会出现重复 6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名; select Student.`S#`,Student.Sname from Student,SC where Student.`S#`=SC.`S#` and SC.`C#`='001'and exists( Select * from SC as SC_2 where SC_2.`S#`=SC.`S#` and SC_2.`C#`='002' ); ↑注意目标字段`S#`关联 exists subquery 可以用in subquery代替,如下 select Student.`S#`,Student.Sname from Student,Sc where Student.`S#`=SC.`S#` and SC.`C#`='001'and sc.`s#` in ( select sc_2.`s#` from sc as sc_2 where sc_2.`c#`='002' ); ↑不同之处,in subquery此处就不需要关联了

带答案--实验6:SQL高级查询

实验六高级查询 【实验目的与要求】 1、熟练掌握IN子查询 2、熟练掌握比较子查询(尤其要注意ANY、ALL谓词如何用集函数代替) 3、熟练掌握EXISTS子查询(尤其是如何将全称量词和逻辑蕴含用EXISTS谓词代替) 4、熟练掌握复杂查询的select语句 【实验准备】 1.准备好测试数据 2.熟悉多表查询与嵌套查询的用法。 【实验内容】 5.1.嵌套子查询 以下实验在前面实验中创建的CPXS数据库中完成,请根据前面实验创建的表结构和数据,完成如下嵌套查询:(也可以不按指导书给出的思路写查询语句,只要是正确的即可,有疑问时可以和同学及老师商量你的查询语句是否正确) 查询在2004年3月18日没有销售的产品名称(不允许重复)。 用IN子查询: 写出对应SQL语句并给出查询结果: USE CPXS SELECT产品名称 FROM CP WHERE产品编号not IN (SELECT产品编号FROM CPXSB WHERE销售日期='2004-3-12') select distinct 产品名称 from CP where 产品编号 not in ( select 产品编号 from CPXSB where 销售日期='2004-3-18' );

用EXISTS子查询: 写出对应SQL语句并给出查询结果: select distinct 产品名称 from CP where 产品名称!=all ( select 产品名称 from CP where exists ( select 产品编号 from CPXSB where 销售日期!='2004-03-18' and CP.产品编号=CPXSB.产品编号 ) ) 查询名称为“家电市场”的客户在2004年3月18日购买的产品名称和数量。 用IN子查询: 写出对应SQL语句并给出查询结果: select 产品名称,数量 from CPXSB left join CP on(CPXSB.产品编号=CP.产品编号) where 客户编号 in ( select 客户编号 from XSS

实验5_SQL语言之高级查询[1]1

实验五高级查询 【实验目的与要求】 1、熟练掌握IN子查询 2、熟练掌握比较子查询(尤其要注意ANY、ALL谓词如何用集函数代替) 3、熟练掌握EXISTS子查询(尤其是如何将全称量词和逻辑蕴含用EXISTS谓词代替) 4、熟练掌握复杂查询的select语句 【实验准备】 1.准备好测试数据 2.熟悉多表查询与嵌套查询的用法。 【实验内容】 5.1.准备工作 1.创建测试用数据库XSGL,并在其中创建三个表 本实验需用到student、course和SC表,其结构和约束如下: Student表结构及其约束为: 表5-1 student表结构和约束 列名称类型宽度允许空值缺省值主键说明 Sno char 8 否是学号 Sname varchar 8 否学生姓名 Sex char 2 否男性别 Birth datetime 否出生年月Classno char 3 否班级号Entrance_date datetime 否入学时间Home_addr varchar 40 是家庭地址 Course表结构及其约束为: 表5-2 course表结构和约束 列名称类型宽度允许空值缺省值主键说明 cno Char 3 否是课程号 Cname varchar 20 否课程名称Total_perior int 是总学时credit int 是学分 SC表结构及其约束为: 表5-3 SC表结构和约束 列名称类型宽度允许空值缺省值主键外键说明

sno Char 8 否是学号,参照student表 cno char 3 否是课程号,参照course表 grade int 是否成绩其中成绩为百分制。 2.对表添加、修改、删除数据 向student表中插入如下数据: 表5-4 student表 Sno sname sex birth classno Entrance_date Home_addr sdept postcode 20050001 张虹男1984/09/011 051 2005/09/01 南京CS 200413 20050002 林红女1983/11/12 051 2005/09/01 北京CS 100010 20050003 赵青男1982/05/11 051 2005/09/01 上海MA 200013 向course表中插入数据: 表5-5 course表 cno Cname Total_perior credit 001 高数68 3 002 C语言程序设计75 4 003 JAVA语言程序设计68 3 向SC表中插入数据: 表5-6 SC表 Sno Cno grade 20050001 001 89 20050001 002 78 20050001 003 89 20050002 002 60 20050003 001 80 为达到更好的测试效果,请自行向数据库表中添加其它数据,使表中数据量达10条以 上,并使每个字段值表现出多样性。 5.2.复杂查询 (1)查询比“林红”年纪大的男学生信息。 SQL语句: select * from Student where birth< ( select birth from Student where Sname='林虹'

SQL基本语句诠释

SQL基本语句 来自:SQL编程技巧 掌握SQL四条最基本的数据操作语句:Insert,Select,Update和Delete。 练掌握SQL是数据库用户的宝贵财富。在本文中,我们将引导你掌握四条最基本的数据库操作语句—SQL的核心功能—来依次介绍比较操作符、选择谓项以及三值逻辑。当你完成这些学习后,显然你已经开始算是精通SQL了。 在我们开始之前,先使用CREATE TABLE语句来创建一个表(如图1所示)。DDL语句对数据库对象如表、列和视进行定义。它们并不对表中的行进行处理,这是因为DDL语句并不处理数据库中实际的数据。这些工作由另一类SQL语句—数据操作语言(DML)语句进行处理。 SQL中有四种基本的DML操作:INSERT,SELECT,UPDATE和DELETE。由于这是大多数SQL用户经常用到的,我们有必要在此对它们进行一一说明。在图1中我们给出了一个名为EMPLOYEES的表。其中的每一行对应一个特定的雇员记录。请熟悉这张表,我们在后面的例子中将要用到它。 INSERT语句 用户可以用INSERT语句将一行记录插入到指定的一个表中。例如,要将雇员John Smith的记录插入到本例的表中,可以使用如下语句:INSERT INTO EMPLOYEES VALUES ('Smith','John','1980-06-10', 'Los Angles',16,45000); 通过这样的INSERT语句,系统将试着将这些值填入到相应的列中。这些列按照我们创建表时定义的顺序排列。在本例中,第一个值“Smith”将填到第一个列LAST_NAME中;第二个值“John”将填到第二列FIRST_NAME中……以此类推。 我们说过系统会“试着”将值填入,除了执行规则之外它还要进行类型检查。如果类型不符(如将一个字符串填入到类型为数字的列中),系统将拒绝这一次操作并返回一个错误信息。 如果SQL拒绝了你所填入的一列值,语句中其他各列的值也不会填入。这是因为SQL提供对事务的支持。一次事务将数据库从一种一致性转移到另一种一致性。如果事务的某一部分失败,则整个事务都会失败,系统将会被恢复(或称之为回退)到此事务之前的状态。 回到原来的INSERT的例子,请注意所有的整形十进制数都不需要用单引号引起来,而字符串和日期类型的值都要用单引号来区别。为了增加可读性而在数字间插入逗号将会引起错误。记住,在SQL中逗号是元素的分隔符。 同样要注意输入文字值时要使用单引号。双引号用来封装限界标识符。 对于日期类型,我们必须使用SQL标准日期格式(yyyy-mm-dd),但是在系统中可以进行定义,以接受其他的格式。当然,2000年临近,请你最好还是使用四位来表示年份。 既然你已经理解了INSERT语句是怎样工作的了,让我们转到EMPLOYEES表中的其他部分: INSERT INTO EMPLOYEES VALUES ('Bunyan','Paul','1970-07-04', 'Boston',12,70000); INSERT INTO EMPLOYEES VALUES ('John','Adams','1992-01-21', 'Boston',20,100000); INSERT INTO EMPLOYEES VALUES ('Smith','Pocahontas','1976-04-06', 'Los Angles',12,100000); INSERT INTO EMPLOYEES VALUES ('Smith','Bessie','1940-05-02', 'Boston',5,200000); INSERT INTO EMPLOYEES VALUES ('Jones','Davy','1970-10-10', 'Boston',8,45000); INSERT INTO EMPLOYEES VALUES ('Jones','Indiana','1992-02-01', 'Chicago',NULL,NULL); 在最后一项中,我们不知道Jones先生的工薪级别和年薪,所以我们输入NULL(不要引号)。NULL是SQL中的一种特殊情况,我们以后将进行详细的讨论。现在我们只需认为NULL表示一种未知的值。 有时,像我们刚才所讨论的情况,我们可能希望对某一些而不是全部的列进行赋值。除了对要省略的列输入NULL外,还可以采用另外一种

高级SQL语句查询(含答案和截图)

C顾客cidcnamecity discnt c001 李广天津10.00 c002 王开基北京12.00 c003 安利德北京8.00 c004 曹士雄天津8.00 c006 曹士雄广州0.00 P商品pidpname city quantity price p01 梳子天津111400 0.50 p02 刷子成都203000 0.50 p03 刀片西安150600 1.00 p04 钢笔西安125300 1.00 p05 铅笔天津221400 1.00 p06 文件夹天津123100 2.00 p07 盒子成都100500 1.00 A代理aidanamecity percent a01 史可然北京 6 a02 韩利利上海 6 a03 杜不朗成都7 a04 甘瑞北京 6 a05 敖斯群武汉 5 a06 史可然天津 5 O订单ordnomonthcidaid pidqtydollars 1011 01 c001 a01 p01 1000 450.00 1012 01 c001 a01 p01 1000 450.00 1019 02 c001 a02 p02 400 180.00 1017 02 c001 a06 p03 600 540.00 1018 02 c001 a03 p04 600 540.00 1023 03 c001 a04 p05 500 450.00 1022 03 c001 a05 p06 400 720.00 1025 04 c001 a05 p07 800 720.00

1013 01 c002 a03 p03 1000 880.00 1026 05 c002 a05 p03 800 704.00 1 查询所有定购了至少一个价值为0.50的商品的顾客的名字。 https://www.doczj.com/doc/ca9097691.html,ame from client wherecid in (select cid from order1 where pid in(select pid from product where price='0.5')) 2 找出全部没有在代理商a03处订购商品的顾客cid值。 selectclient.cid from client wherecid not in (select cid from order1 where aid in(select aid from agend where aid='a03')) 3 找出只在代理商a03处订购商品的顾客。 selectclient.cid from client wherecid not in (select cid from order1 where aid in(select aid from agend where aid!='a03') )AND cid in (select cid from order1 where aid in(select aid from agend where aid='a03')) 4 找出没有被任何一个在北京的顾客通过在上海的代理商订购的所有商品。 select *from product wherepid not in (select pid from client,agend,order1 where client.cid=order1.cid and agend.aid = order1.aid and client.city='北京' and agend.city = '上海')

相关主题
文本预览
相关文档 最新文档