mysql面试题以及答案包括优化
2016-09-20 00:03
537 查看
1、用一条SQL语句 查询出每门课都大于80分的学生姓名
id name subject score
1 张三 语文 81
2 张三 数学 75
3 李四 语文 76
4 李四 数学 90
5 王五 语文 81
6 王五 数学 100
7 王五 英语 90
创建表:
答案:
查询计划:
mysql> explain extended select name from (select name,count(*) as tcount from score where score > 80 group by name) as t where t.tcount in (select count(*) from score group by name);
+----+-------------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 3 | 100 | Using where |
| 3 | SUBQUERY | score | ALL | NULL | NULL | NULL | NULL | 7 | 100 | Using temporary; Using filesort |
| 2 | DERIVED | score | ALL | NULL | NULL | NULL | NULL | 7 | 100 | Using where; Using temporary; Using filesort |
+----+-------------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------+
3 rows in set
mysql> show warnings;
+-------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+-------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Note | 1003 | select `t`.`name` AS `name` from (select `demo`.`score`.`name` AS `name`,count(0) AS `tcount` from `demo`.`score` where (`demo`.`score`.`score` > 80) group by `demo`.`score`.`name`) `t` where <in_optimizer>(`t`.`tcount`,`t`.`tcount` in ( <materialize> (select count(0) AS `count(*)` from `demo`.`score` group by `demo`.`score`.`name` ), <primary_index_lookup>(`t`.`tcount` in <temporary table> on distinct_key where ((`t`.`tcount` = `materialized subselect`.`count(*)`))))) |
+-------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set
mysql>
2、mysql删除重复记录
id name
1 aa
2 aa
3 cc
4 cc
创建表:
答案:
id name subject score
1 张三 语文 81
2 张三 数学 75
3 李四 语文 76
4 李四 数学 90
5 王五 语文 81
6 王五 数学 100
7 王五 英语 90
创建表:
mysql> create table score(id int primary key, name varchar(10), subject varchar(20), score int ) engine=innodb; Query OK, 0 rows affected mysql> insert into score select 1,'张三','语文',81; mysql> insert into score select 2,'张三','数学',75; mysql> insert into score select 3,'李四','语文',76; mysql> insert into score select 4,'李四','数学',90; mysql> insert into score select 5,'王五','语文',81; mysql> insert into score select 6,'王五','数学',100; mysql> insert into score select 7,'王五','英语',90;
答案:
mysql> select name from (select name,count(*) as tcount from score where score > 80 group by name) as t where t.tcount in (select count(*) from score group by name); +------+ | name | +------+ | 王五 | +------+ 1 row in set
查询计划:
mysql> explain extended select name from (select name,count(*) as tcount from score where score > 80 group by name) as t where t.tcount in (select count(*) from score group by name);
+----+-------------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 3 | 100 | Using where |
| 3 | SUBQUERY | score | ALL | NULL | NULL | NULL | NULL | 7 | 100 | Using temporary; Using filesort |
| 2 | DERIVED | score | ALL | NULL | NULL | NULL | NULL | 7 | 100 | Using where; Using temporary; Using filesort |
+----+-------------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------+
3 rows in set
mysql> show warnings;
+-------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message |
+-------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Note | 1003 | select `t`.`name` AS `name` from (select `demo`.`score`.`name` AS `name`,count(0) AS `tcount` from `demo`.`score` where (`demo`.`score`.`score` > 80) group by `demo`.`score`.`name`) `t` where <in_optimizer>(`t`.`tcount`,`t`.`tcount` in ( <materialize> (select count(0) AS `count(*)` from `demo`.`score` group by `demo`.`score`.`name` ), <primary_index_lookup>(`t`.`tcount` in <temporary table> on distinct_key where ((`t`.`tcount` = `materialized subselect`.`count(*)`))))) |
+-------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set
mysql>
2、mysql删除重复记录
id name
1 aa
2 aa
3 cc
4 cc
创建表:
mysql> create table t(id int primary key,name varchar(10))engine=innodb; Query OK, 0 rows affected mysql> insert into t select 1,'aa'; mysql> insert into t select 2,'aa'; mysql> insert into t select 3,'cc'; mysql> insert into t select 4,'cc';
答案:
mysql> delete from t where id in(select * from (select max(id) from t group by name having count(*)>1)temp); Query OK, 2 rows affected
相关文章推荐
- SQL数据库面试题以及答案(50例题优化版-增加图片):你必知必会的SQL语句练习
- mysql优化(SQL语句的优化以及索引的应用范围)
- Google面试题以及答案
- 15个Google面试题以及答案~~~~你会几个?
- mysql优化Analyze Table 以及批量分析表的命令
- SQL数据库面试题以及答案
- C语言方面方面的面试题以及答案
- 15个Google面试题以及答案
- jsp及Servlet经典面试题以及答案
- MySQL5.6 单列、多列索引以及IN语句的优化(翻译)
- 很刺激——15个Google面试题以及答案
- android 常见面试题以及答案
- 15个Google面试题以及答案~~~~你会几个?
- 2011Android技术面试整理附有详细答案(包括百度、新浪、中科软等多家公司笔试面试题)
- 15个Google面试题以及答案
- MySQL在插入90万条数据消耗的时间以及查询90万条数据时的性能优化
- 15个Google面试题以及答案
- SQL数据库面试题以及答案
- 如何处理PHP和MYSQL的并发以及优化
- 很全面的ios面试题以及解答,很多答案不一定全对,网上摘录以及自己的理解