作者 胖熊酱
理论类
1.什么是数据库?
数据库(Database)是按照数据结构来组织、存储和管理数据的建立在计算机存储设备上的仓库。简单来说是本身可视为电子化的文件柜——存储电子文件的处所,用户可以对文件中的数据进行新增、截取、更新、删除等操作。而SQL(Structured Query Language结构化查询语言)是我们与数据库交流的一种语言。如果想获取数据库中的数据,就需要掌握这种语言的语法结构。
2.什么是SQL?
3.什么是表(Table)?
4.什么是字段(Column)?
基础语法类
下面的问题基于这张表,如果你可以快速的回答上面的问题,请简单描述下,下面这张表,然后根据问题,写出SQL 。

5.查询表t_student中的所有数据
考察:select
6.将第5题中查询出的数据按照s_id降序排列
考察:order by
7.只查询第6题中前3条记录
考察:limit
8.查询出所有的男生信息
考察:where
9.查询一班所有的男生信息和2班的女生信息
考察:and ,or
10.查询爱好中包含吃肉的学生信息
考察:like
常用函数
1.求 -10 和 9 的绝对值
考察:abs()
2.将 -1.4 、2.6的四舍五入值保留整数
考察:round(x)
3.将 3.474、-1.875 四舍五入保留2位小数
考察:round(x,d)
4.求 ‘abc’、’love中国’ 的字节长度和字符长度
考察:length()、char_length()
5.将 ‘abd’和’love中国’拼接到一起
考察:concat()
6.查看系统当前的日期
考察:CURRENT_DATE,CURRENT_DATE()
7.求 日期’2018-03-07’往前28天是几号
考察:str_to_date() 和 interval 和 date_add()
1. 请查询出t_employees(员工表),男生和女生各有多少(gender字段:F-女生,M-男生)?
考察:聚合函数group by的使用、count()函数使用
答案输入(语句):
select gender,count(*) from t_employees group by gender
2. 请查询出t_employees(员工表),每名员工入职时的大概年龄?
考察:year()函数使用
答案输入(语句):
select emp_no, year(hire_date)-year(birth_date) as 年龄 from t_employees
3. 请查询出员工编号为10001,10010,10026的员工信息
考察:in的使用
答案输入(语句):
select * from t_employees where emp_no in (10001,10010,10026)
4. 查询员工表中,同一天过生日的员工信息?
考察:in的使用和子查询
答案输入(语句):
select * from t_employees where birth_date in
(select birth_date from t_employees
group by birth_date
having count(birth_date)>1)
5. 查询员工表中,同一天入职并且至少有一个是男生,一个是女生的所有员工信息?
考察:in的使用和子查询和having
答案输入(语句):
select *from t_employees where hire_date in (
select hire_date from t_employees
group by hire_date
having count(distinct gender)>1)
6. 查询员工表中,工号10137的员工入职后,10天内有多少人入职?
考察:date_add函数使用
答案输入(语句):
select *from t_employees a
join (select hire_date,date_add(hire_date,interval 10 day)
as to_hire_date from t_employees
where emp_no=10137
)b on a.hire_date>=b.hire_date
and a.hire_date<=b.to_hire_date
7. 查询所有员工对应的部门编号(不管员工是否有部门编号)?
考察:left join的使用
答案输入(语句):
select *from t_employees a left join t_dept_emp b on a.emp_no=b.emp_no
8. 查询有所有员工的个人信息和对应的部门名称(只查询有部门编号的员工)?
考察:join的使用
答案输入(语句):
select a.*,c.dept_name from t_employees a
join t_dept_emp b on a.emp_no=b.emp_no t_dept_emp
join t_departments c on c.dept_no=b.dept_no
9. 查询部门编号为空的员工信息(没有部门编号)?
考察:left join的使用和where条件
答案输入(语句):
select *from t_employees a
left join t_dept_emp b on a.emp_no=b.emp_no
where b.dept_no is null;
10.查询每个部门的员工人数(部门编号即可)?
考察:join的使用&count函数
答案输入(语句):
Select b.dept_no, count(1) nm from t_employees a join t_dept_emp b
On a.emp_no=b.emp_no group by b.dept_no
11.查询部门人数>20的部门编号及该部门的人数?
考察:join的使用&count函数&having函数使用
答案输入(语句):
Select b.dept_no, count(1) nm from t_employees a
join t_dept_emp b on a.emp_no=b.emp_no
group by b.dept_no having nm>20 ;
12.统计每一天的入职人数,并按照入职人数降序排列?
考察:count的使用和order by
答案输入(语句):
select hire_date,count(1) nm from t_employees a
group by hire_date order by nm desc ;
13.统计每个月(例:2018-01)的员工入职人数,根据员工人数降序排列,并取前5条记录?
考察:group by的使用&count函数&DATE_FORMAT函数&limit
答案输入(语句):
select DATE_FORMAT(hire_date,'%Y-%m') month_id,
count(1) as num from t_employees
group by DATE_FORMAT(hire_date,'%Y-%m')
order by num desc limit 5;
14.统计每个部门的男员工和女员工各有多少人,显示部门名称和性别、人数?
考察:join的使用&count函数
答案输入(语句):
Select c.dept_name,a.gender,count(1) as nm
from t_employees a join t_dept_emp b on b.emp_no=a.emp_no
join t_departments c on c.dept_no=b.dept_no
group by c.dept_name,a.gender;
15查询部门女员工最多的部门中的所有男员工信息(略复杂,可选做)?
考察:count的使用和order by&join&limit
答案输入(语句):
select * from t_employees a join t_dept_emp b
on b.emp_no=a.emp_no join (select b.dept_no,a.gender,count(1)
as nm from t_employees a join t_dept_emp b on b.emp_no=a.emp_no where a.gender='F'
group by b.dept_no,a.gender order by nm desc limit 1
) x on x.dept_no=b.dept_no where a.gender='M';
本文来自知乎,观点不代表一起大数据-技术文章心得立场,如若转载,请注明出处:https://zhuanlan.zhihu.com/p/67642798
注意:本文归作者所有,未经作者允许,不得转载