![b842dd0f0bc8fe17e6025233a68e6d69.png](https://i-blog.csdnimg.cn/blog_migrate/387b6d1073e8f7792a9c5a0a513d5cee.jpeg)
1:用一条SQL语句查出每门课都大于80分的学生的姓名,数据表结构如下:
![a25dfdd5245b00cbf6d9c675bb24f5c7.png](https://i-blog.csdnimg.cn/blog_migrate/2fd6c0b16a40656bc467a878cb8d8ff8.png)
ts1:SELECT DISTINCT name FROM STRUNDENT2
WHERE name NOT IN
(SELECT DISTINCT name FROM STRUNDENT2 WHERE score <=80);
结果: 只有李立 和 张华
![b8ec1b24d9852a346fa70680fb0f8057.png](https://i-blog.csdnimg.cn/blog_migrate/6179f36345848b76d5ffced99e3e3f56.png)
图上学生表:删除除了自动编号不同,其他都相同的学生冗余信息
ts:delete from STUDENTS
where 自动编号 not in
(select min(自动编号) from STUDENTS group by 学号,姓名,课程编号,课程名称,分数) ;
结果是:第三条冗余数据被删除。
![feceef5781c844d10b1a069d333b98f6.png](https://i-blog.csdnimg.cn/blog_migrate/a7504ac7c1e6d343f6de7e0576d7b459.png)
如上图所示:字段name中有四条记录,分别对应为ABCD四个球队,现在四个球队进行比赛,
用一条sql语句显示所有可能的比赛组合.
ts1: select a.name as 组队1, b.name as 组队2 from team a, team b where a.name < b.name ;
如下图依次对应: AB、AC 、AD 、BC 、BD 、CD ;
![34e0f7c72539c5932ff48302efe808c6.png](https://i-blog.csdnimg.cn/blog_migrate/17f14c1b110471a6ff0df5f01772f141.png)
![ea1647b37214a94b8b0bf01a52e427e4.png](https://i-blog.csdnimg.cn/blog_migrate/968d3bdbaa26c2f9864f5009a9ea3387.png)
如图所示:怎样转变为行转列的行时 输出:
ts1:
SELECT
year,
MAX ( CASE WHEN month = 1 THEN amount end) AS m1,
MAX ( CASE WHEN month = 2 THEN amount end) AS m2,
MAX ( CASE WHEN month = 3 THEN amount end) AS m3,
MAX ( CASE WHEN month = 4 THEN amount end) AS m4
FROM yearm GROUP BY year
ts2:
select year,
(select amount from yearm m where month=1 and m.year=yearm.year) as m1,
(select amount from yearm m where month=2 and m.year=yearm.year) as m2,
(select amount from yearm m where month=3 and m.year=yearm.year) as m3,
(select amount from yearm m where month=4 and m.year=yearm.year) as m4
from yearm group by year
![c17a0fdeb879223c195262bd56ee5fe0.png](https://i-blog.csdnimg.cn/blog_migrate/98b0c99afb54aea0a3d18380f07b2a1d.png)
说明:
1:复制表(只复制结构,源表名:a 新表名:b) select * into b from a where 1<>1
2:拷贝表(拷贝数据,源表名:a 目标表名:b) insert into b(a, b, c) select d,e,f from b;
3:显示文章、提交人和最后回复时间
select a.title,a.username,b.adddate from table a,
(select max(adddate) adddate from table
where table.title=a.title) b ;
4:日程安排提前五分钟提醒 select * from 日程安排 where datediff('minute',f开始时间,getdate())>5
5:有两个表A 和B ,均有key 和value 两个字段,如果B 的key 在A 中也有,就把B 的value 换为A 中对应的value
这道题的SQL 语句怎么写?
update b set b.value=(select a.value from a where a.key=b.key)
where b.id in(select b.id from b,a where b.key=a.key);
6:查询表A中存在ID重复三次以上的记录,完整的查询语句如下:
select * from(select count(ID) as count from table group by ID)T where T.count>3
7:SQL优化:
1:在表中建立索引,优先考虑where.group by使用到的字段
2:查询条件中,一定不要使用select *
3:不要在where条件中使用左右两边都是%的like模糊查询
4:尽量不要使用in 和not in,会造成全表扫描
5:对于连续的数值,能用 between 就不要用 in
6:尽量不要使用or,会造成全表扫描
7:尽量不要在 where 子句中对字段进行表达式操作,造成全表扫描
8:where条件里尽量不要进行null值的判断
9:尽量不要在where条件中等号的左侧进行表达式.函数操作
10:尽量不要使用where 1=1的条件
11:程序要尽量避免大事务操作,提高系统并发能力
12:一个表的索引数最好不要超过6个
8:表中有A B C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列,
当B列大于C列时选择B列否则选择C列。
select (case when a>b then a
when a>c then a
when b>c then b else c
end) result
from NUMBER_SORT;
如下图所示:
![979e5bd17f34e9a75c95dde3fa8a9f6b.png](https://i-blog.csdnimg.cn/blog_migrate/3c2e11ca9875acfacd7a91700e1f210e.png)
![bb96d8fc519a3d15026a8b47bce9c01c.png](https://i-blog.csdnimg.cn/blog_migrate/0a3c89ca1ba710c512e3e53b9082cf4d.png)