SQL 基礎練習
阿新 • • 發佈:2018-03-22
自動編號 面試題 min var IT hide tex pre having
name | kecheng | fenshu |
張三 | 語文 | 81 |
張三 | 數學 | 75 |
李四 | 語文 | 76 |
李四 | 數學 | 90 |
王五 | 語文 | 81 |
王五 | 數學 | 100 |
王五 | 英語 | 90 |
1、用一條SQL語句查詢出每門課都大於80分的學生姓名
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(‘張三‘,‘語文View Code‘,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關鍵字無法與合計函數一起使用-- 12select 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、刪除除了自動編號不同, 其他都相同的學生冗余信息
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、面試題:怎麽把這樣一個表兒
查成這樣一個結果
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)
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答案
SQL 基礎練習