MySQL面试题讲解

一、面试题

1havingwhere的区别;

①、having是在分组后对数据进行过滤; where是在分组前对数据进行过滤

②、having后面可以使用分组函数(统计函数); where后面不可以使用分组函数。

③、WHERE是对分组前记录的条件,如果某行记录没有满足WHERE子句的条件,那么这行记录不会参加分组;而HAVING是对分组后数据的约束

2、请写出删除和插入的MySQL语句

①、创建表

create table xxx (

name varchar ( 11 ),

age int

);

②、插入

A:按字段插入 insert into info(username)  values(lxd);(info表中按给username字段添加lxd)

B:全部插入 insert into info values(lxd,22);(给info表中所有字段添加信息,中间以逗号隔开)

C:批量插入insert into info values(lisi,18),(zhaosi,48);(给info表中所有字段添加多条信息)

③、删除

A:   delete from xxx;(可用来删除一行数据,也可删除表所有数据)(删除数据较多的表有些耗时)注意:不是删除表,只是删除了其中的数据

B:   truncate table xxx;(简单快捷节省时间)(在数据较多的情况下可使用)

C:   drop table xxx;(删除表)

3、MySQL中有两个表,表A 中有名字,成绩,性别,表B 中有名字,成绩,性别,问女生总数有多少人,平均分是多少?

①、女生总数  

Select

(select count(sex) from A where sex=girl;)

+

 select count(sex) from B where sex=girl;)

②、平均分  同上思路,先查询表A的平均分,在查询B表的平均分,利用select算法相加除以二就可

4、mysql中有两个表,表A中有id,name,company,表B中有id,工资,怎么查询李四的工资

 命令:select B.工资 from A inner join B on A.id=B.id where A.name=李四;

 

5、如何按照满分、优秀、良好、一般、不及格来对表中数据进行筛选?

When:条件

Then:结果

End:结尾

命令: select (

    -> case when score=100 then "满分"

    -> when score>=80 and score<90 then "优秀"

    -> when score>=60 and score<80 then "良好"

    -> when score<60 then "不及格" end)

    -> score,name from student;

 

6、需求:查询出相同姓名大于1的信息

命令:

select * from persion

    -> where name in

    -> (

    -> select name from persion group by name having count(*)>1

-> );

 

 

7、需求:查询出不同年龄性别的总和

命令:

select sex ,sum(age)sum from persion group by sex;

 

8、需求:查询出不同年龄性别的平均年龄

命令:select sex ,avg(age)avg from persion group by sex;

 

 

posted @ 2022-08-29 17:30  陌上归程  阅读(58)  评论(0编辑  收藏  举报