name | kecheng | fenshu |
张三 | 语文 | 81 |
张三 | 数学 | 75 |
李四 | 语文 | 76 |
李四 | 数学 | 90 |
王五 | 语文 | 81 |
王五 | 数学 | 100 |
王五 | 英语 | 90 |
1、用一条SQL语句查询出每门课都大于80分的学生姓名
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 create table chengji1( 2 name varchar(50) not null, 3 kecheng varchar(50) not null, 4 fenshu int not null 5 ) 6 insert chengji1(name,kecheng,fenshu)values('张三','语文',81),('张三','数学',75),('李四','语文',76),('李四','数学',90),('王五','语文',81),('王五','数学',100),('王五','英语',90) 7 8 select * from chengji1 9 --distinct 用于返回唯一不同的值-- 10 select distinct name from chengji1 where name not in (select distinct name from chengji1 where fenshu <= 80 ) 11 --增加having字句的原因是,where关键字无法与合计函数一起使用-- 12 select name from chengji1 group by name having min(fenshu)> 80 13 ---- 14 select name from chengji1 group by name having COUNT(kecheng) >= 3 and MIN(fenshu) >= 80
2、删除除了自动编号不同, 其他都相同的学生冗余信息
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 create table stutable1( 2 id int primary key IDENTITY(1,1) not null, 3 stunumber varchar(20) not null, 4 name varchar(20) not null, 5 kcnumber varchar(20) not null, 6 kecheng varchar(20) not null, 7 fenshu int not null 8 ) 9 insert stutable1(stunumber,name,kcnumber,kecheng,fenshu) values('2005001','张三','0001','数学',69),('2005002','李四','0001','数学',89),('2005001','张三','0001','数学',69) 10 select * from stutable1 11 delete stutable1 where id not in (select MIN(id) from stutable1 group by stunumber,name,kcnumber,kecheng,fenshu)
3、面试题:怎么把这样一个表儿
查成这样一个结果
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
create table yma( year int not null, month int not null, amount decimal(18,1) not null ) insert yma values(1991,1,1.1),(1991,2,1.2),(1991,3,1.3),(1991,4,1.4),(1992,1,2.1),(1992,2,2.2),(1992,3,2.3),(1992,4,2.4) select * from yma delete from yma select year, (select amount from yma m where month =1 and m.year= yma.year) as m1, (select amount from yma m where month =2 and m.year= yma.year) as m2, (select amount from yma m where month =3 and m.year= yma.year) as m3, (select amount from yma m where month =4 and m.year= yma.year) as m4 from yma group by year
4、说明:拷贝表( 拷贝数据, 源表名:a 目标表名:b)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
create table yma1( year int not null, month int not null, amount decimal(18,1) not null ) insert into yma1(year,month,amount)select year,month,amount from yma select * from yma1
5、通过 SQL,您如何按字母顺序选取 Persons 表中 LastName 介于 Adams 和 Carter 的所有记录?
SELECT * FROM Persons WHERE LastName BETWEEN 'Adams' AND 'Carter'
6、通过 SQL,您如何从 "Persons" 表中选取 "FirstName" 列的值以 "a" 开头的所有记录?
SELECT * FROM Persons WHERE FirstName LIKE 'a%'