1.一个表中的Id有多个记录,把所有这个id的记录查出来,并显示共有多少条记录数。
select ID,count(*) from Student
group by ID having count(*)>1
select * from(select ID,count(ID) as count
from Student group by ID)T
2.有一张表,里面有3个字段:语文,数学,英语。其中有3条记录分别表示语文70分,数学80分,英语58分,请用一条sql语句查询出这三条记录并按以下条件显示出来(并写出您的思路):
大于或等于80表示优秀,大于或等于60表示及格,小于60分表示不及格。
if object_id('tb') is not null
drop table tb
go
create table tb(yw int,sx int,yy int)
insert into
tb
select 70,80,58
select
(case when yw <60 then '不及格'
when yw >= 80 then
'优秀'
else'及格' end) as '语文',
(case when sx <60 then
'不及格'
when sx >= 80 then '优秀'
else'及格' end) as
'数学',
(case when yy <60 then '不及格'
when yy >= 80 then
'优秀'
else'及格' end) as '英语'
from tb
drop table tb
3.请教一个面试中遇到的SQL语句的查询问题
表中有A B
C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列。
select (case when a>b
then a else b end),
(case when b>c then b else c end)
from tb
4.为管理岗位业务培训信息,建立3个表:
S (S#,SN,SD,SA) S#,SN,SD,SA
分别代表学号、学员姓名、所属单位、学员年龄
C (C#,CN ) C#,CN 分别代表课程编号、课程名称
SC ( S#,C#,G ) S#,C#,G 分别代表学号、所选修的课程编号、学习成绩
A. 使用标准SQL嵌套语句查询选修课程名称为’税收基础’的学员学号和姓名
B.
使用标准SQL嵌套语句查询选修课程编号为’C2’的学员姓名和所属单位
C.
使用标准SQL嵌套语句查询不选修课程编号为’C5’的学员姓名和所属单位
D. 使用标准SQL嵌套语句查询选修全部课程的学员姓名和所属单位
E.
查询选修了课程的学员人数
F. 查询选修课程超过5门的学员学号和所属单位
A. select S#,SN from S where S# in(select S# from SC,C where SC.C#=C.C# and
CN='税收基础')
B. select S.SN,S.SD from S,SC where S.S#=SC.S# and C#=C2
C. SELECT SN,SD FROM S where [S#] NOT IN(SELECT [S#] FROM SC WHERE
[C#]='C5')
或者select S.SN,S.SD from S,SC where S.S#=SC.S# and C#!=C5
D. select SN,SD, from S where (select count(S#) from SC group by S#)=(select
count(*) from C group by C#)
E. select count(distinct S#) from SC
F. select S#,SD from S where S# in (SELECT [S#] FROM SC GROUP BY [S#] HAVING
COUNT(DISTINCT [C#])>5)
5.问题描述:
S (SNO,SNAME) 学生关系。SNO 为学号,SNAME 为姓名
C (CNO,CNAME,CTEACHER) 课程关系。CNO 为课程号,CNAME 为课程名,CTEACHER 为任课教师
SC(SNO,CNO,SCGRADE) 选课关系。SCGRADE 为成绩
A. 找出没有选修过“李明”老师讲授课程的所有学生姓名
Select SNAME FROM S Where NOT EXISTS (
Select * FROM SC,C Where SC.CNO=C.CNO AND CNAME='李明' AND SC.SNO=S.SNO)
B. 列出有二门以上(含两门)不及格课程的学生姓名及其平均成绩
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
C. 列出既学过“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
D.
列出“1”号课成绩比“2”号同学该门课成绩高的所有学生的学号
Select S.SNO,S.SNAME FROM S,
(Select
SC1.SNOFROM SC SC1,C C1,SC SC2,C C2
Where SC1.CNO=C1.CNO AND
C1.NAME='1'
AND SC2.CNO=C2.CNO AND C2.NAME='2'
AND
SC1.SCGRADE>SC2.SCGRADE ) SC
Where S.SNO=SC.SNO
E. 列出“1”号课成绩比“2”号课成绩高的所有学生的学号及其“1”号课和“2”号课的成绩
Select
S.SNO,S.SNAME,SC.[1号课成绩],SC.[2号课成绩] FROM S,
(Select
SC1.SNO,[1号课成绩]=SC1.SCGRADE,[2号课成绩]=SC2.SCGRADE
FROM SC SC1,C C1,SC SC2,C
C2
Where SC1.CNO=C1.CNO AND C1.NAME='1'
AND SC2.CNO=C2.CNO AND
C2.NAME='2'
AND SC1.SCGRADE>SC2.SCGRADE) SC
Where S.SNO=SC.SNO
6.
======
有如下表记录:
ID Name EmailAddress
LastLogon
100 test4 test4@yahoo.cn 2007-11-25
16:31:26
13 test1 test1@yahoo.cn 2007-3-22
16:27:07
19 test1 test1@yahoo.cn 2007-10-25
14:13:46
42 test1 test1@yahoo.cn 2007-11-20
14:20:10
45 test2 test2@yahoo.cn 2007-4-25
14:17:39
49 test2 test2@yahoo.cn 2007-5-25 14:22:36
用一句sql查询出每个用户最近一次登录的记录(每个用户只显示一条最近登录的记录)
方法一:
SELECT a.* from
users a inner join
(SELECT [Name],
LastLogon=MAX(LastLogon) FROM users GROUP BY [Name])
b
on a.[Name]=b.[Name] and
a.[LastLogon]=b.[LastLogon]
方法二:
SELECT a.* from users a inner
join
(SELECT Name,MAX(LogonID) LogonID
FROM users GROUP BY [Name]) b
on
a.LogonID=b.LogonID
--where a.LogonId=b.LogonId
7.现在我们假设只有一个table,名为pages,有四个字段,id,
url,title,body。里面储存了很多网页,网页的url地址,title和网页的内容,然后你用一个sql查询将url匹配的排在最前,
title匹配的其次,body匹配最后,没有任何字段匹配的,不返回。
select a.[id],a.mark from
(
select
[page].[id],100 as mark from [page] where [page].[url] like '%baidu%'
union
select [page].[id],50 as mark from [page] where [page].[title] like
'%baidu%'
union
select [page].[id],10 as mark from [page] where
[page].[body] like '%baidu%'
) as a order by mark desc
8.查询某表中的第30到40行的数据
select top * 10 from (select top * 40 from table order
by id asc) table_a order by id desc
9.对于教学数据库的三个基本表
学生 S(S#,SNAME,AGE,SEX)
学习 SC(S#,C#,GRADE)
课程 C(C#,CNAME,TEACHER)
试用SQL的查询语句表达下列查询:
A.检索LIU老师所授课程的课程号和课程名。
select C#,CNAME from C where TEACHER='LIU'
B.检索年龄大于23岁的男学生的学号和姓名。
select S#,SNAME from S where AGE>23 and
SEX='男'
C.检索至少选修LIU老师所授课程中一门课程的女学生姓名。
select SNAME from S left join SC on
S.S#=SC.S# left join C on SC.C#=C.C# where S.SEX='女' and C.TEACHER='LIU'
D.检索WANG同学不学的课程的课程号。
SELECT C# FROM C WHERE C# NOT IN (SELECT C# FROM SC
WHERE S# IN (SELECT S# FROM S WHERE SNAME='WANG'))
E.检索至少选修两门课程的学生学号。
select distinct SC1.S# from SC as SC1 where
(select count(*) from SC as SC2 where SC1.S#=SC2.S#)>=2
F.检索全部学生都选修的课程的课程号与课程名。
SELECT C.C#,C.CNAME FROM C WHERE NOT EXISTS
(SELECT S.S# FROM S WHERE S.S# NOT IN(SELECT SC.S# FROM SC WHERE SC.C# =
C.C#))
G.检索选修课程包含LIU老师所授课的学生学号。
select SC.S# from SC,C where SC.C#=C.C# and
C.TEACHER='LIU'