29 lines
1.2 KiB
Transact-SQL
29 lines
1.2 KiB
Transact-SQL
-- 查找至少有2名男生的班号
|
|
select class from student where sex=N'男' group by class having count(*) >= 2;
|
|
Go
|
|
-- 查找student表中不姓王的同学
|
|
select * from student where name not like N'王%';
|
|
Go
|
|
-- 查找student表中每个学生的姓名和年龄
|
|
select name, year(getdate())-year(birthday) age from student;
|
|
Go
|
|
-- 查询student表中最大生日和最小生日的学生
|
|
select * from student where birthday = (select min(birthday) from student)
|
|
union
|
|
select * from student where birthday = (select max(birthday) from student);
|
|
Go
|
|
-- 以班号和年龄从大到小的顺序显示学生信息
|
|
select * from student order by class desc, birthday;
|
|
Go
|
|
-- 查找男教师的姓名及其上的课程的名称
|
|
select name, cname from course, teacher where no=tno and sex=N'男';
|
|
Go
|
|
-- 查找最高分同学的no, cno, degree
|
|
select * from score where degree = (select max(degree) from score);
|
|
Go
|
|
-- 查找和李军同学同性别,同班级的所有学生的姓名
|
|
select name from student
|
|
where sex=(select sex from student where name=N'李军')
|
|
and class=(select class from student where name=N'李军');
|
|
except select name from student where name=N'李军';
|
|
Go |