SQL面试题 五(重复数据、统计)

题一:表stu

+----+------+-----+-------+
| id | sno  | cno | score |
+----+------+-----+-------+
|  1 | 1001 |   1 |    89 |
|  3 | 1001 |   1 |    89 |
|  2 | 1002 |   1 |    87 |
|  4 | 1002 |   1 |    90 |
|  5 | 1003 |   1 |    86 |
+----+------+-----+-------+


1、查询出sno,cno重复的数据:

> select * from rs where (sno,cno) in (select sno,cno from rs group by sno,cno having count(*)>1);

2、查询sno,cno,score都重复的数据,且除id最小的一条数据外:

> select * from rs where (sno,cno,score) in(select sno,cno,score from rs group by sno,cno,score having count(*)>1)and id not in (select min(id) from rs group by sno,cno,score having count(*)>1);

3、删除sno,cno,score都重复的数据,保留id最小的一条数据:

>(SQL Server未验证)delete  from rs where (sno,cno,score) in(select sno,cno,score from rs group by sno,cno,score having count(*)>1)and id not in (select min(id) from rs group by sno,cno,score having count(*)>1);

>(MySQL)

create table temp as select * from rs where (sno,cno,score) in(select sno,cno,score from rs group by sno,cno,score having count(*)>1)and id not in (select min(id) from rs group by sno,cno,score having count(*)>1);

delete  from rs where id in(select  id  from temp);

drop table temp;



题二:有以下三张表

商品表:tp 
+-------+-------+
| pcode | pname |
+-------+-------+
|     1 | cpu   |
|     2 | 内存  |
|     3 | 硬盘  |

城市表:tc 
+-----+-------+
| cno | cname |
+-----+-------+
| 101 | 广州  |
| 102 | 深圳  |
| 103 | 上海  |
+-----+-------+


销售表:ts 
+-------+-----+-------+
| pcode | cno | count |
+-------+-----+-------+
|     1 | 101 | 10000 |
|     1 | 102 |   500 |
|     1 | 103 | 20000 |
|     2 | 101 | 40000 |
|     2 | 103 | 30000 |
|     3 | 102 | 90000 |
+-------+-----+-------+


问题:需要得到以下结构的统计结果,写出SQL语句:

            广州      深圳       上海        总计 

CPU  10000   5000      20000    35000 
内存   40000   0             30000    70000 
硬盘   0            90000    0             90000 
总计   50000   95000    50000   195000 

>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值