面试 SQL整理 常见的SQL面试题:经典50题

目录

SQL基础知识整理:

常见的SQL面试题:经典50题

三、50道面试题

2.汇总统计分组分析

3.复杂查询

sql面试题:topN问题

4.多表查询

【面试题类型总结】这类题目属于行列如何互换,解题思路如下:

其他面试题:



preview

SQL基础知识整理:

select 查询结果    如: [学号,平均成绩:组函数avg(成绩)]from 从哪张表中查找数据   如:[涉及到成绩:成绩表score]where 查询条件    如:[b.课程号='0003' and b.成绩>80]group by 分组    如:[每个学生的平均:按学号分组](oracle,SQL server中出现在select 子句后的非分组函数,必须出现                                                                                        在group by子句后出现),MySQL中可以不用having 对分组结果指定条件    如:[大于60分]order by 对查询结果排序    如:[增序: 成绩  ASC / 降序: 成绩 DESC];

limit   使用limt子句返回topN(对应这个问题返回的成绩前两名)如:[ limit  2 ==>从0索引开始读取2个]
limit==>从0索引开始 [0,N-1]

  1. ① select * from table limit 2, 1;                
  2. //含义是跳过2条取出1条数据,limit后面是从第2条开始读,读取1条信息,即读取第3条数据
  3. ② select * from table limit 2 offset 1;     
  4. //含义是从第1条(不包括)数据开始取出2条数据,limit后面跟的是2条数据,offset后面是从第1条开始读取,即读取第2,3条

组函数: 去重 distinct()  统计总数sum()   计算个数count()  平均数avg()  最大值max() 最小数min() 

多表连接: 内连接(省略默认inner) join ...on..左连接left join tableName as b on a.key ==b.key右连接right join  连接union(无重复(过滤去重))和union all(有重复[不过滤去重])

--union 并集
--union all(有重复)
oracle(SQL server)数据库

--intersect 交集 
--minus(except) 相减(差集)

oracle

一、数据库对象: 表(table)  视图(view)  序列(sequence)  索引(index)  同义词(synonym)

1. 视图: 存储起来的 select 语句create view emp_vwas
select employee_id, last_name, salary
from employees
where department_id = 90;

select * from emp_vw;

--可以对简单视图进行 DML 操作
update emp_vw
set last_name = 'HelloKitty'
where employee_id = 100;

select * from employees
where employee_id = 100;

1). 复杂视图
create view emp_vw2
as
select department_id, avg(salary) avg_sal
from employees
group by department_id;

select * from emp_vw2;

--复杂视图不能进行 DML 操作
update emp_vw2
set avg_sal = 10000
where department_id = 100;

2. 序列:用于生成一组有规律的数值。(通常用于为主键设置值)
create sequence emp_seq1
start with 1
increment by 1
maxvalue 10000
minvalue 1
cycle
nocache;

select emp_seq1.currval from dual;

select emp_seq1.nextval from dual;

--问题:裂缝 .  原因:①当多个表共用同一个序列时。  ②rollback  ③发生异常
create table emp1(
       id number(10),
       name varchar2(30)
);

insert into emp1
values(emp_seq1.nextval, '张三');

select * from emp1;

3. 索引:提高查询效率
--自动创建:Oracle 会为具有唯一约束(唯一约束,主键约束)的列,自动创建索引
create table emp2(
       id number(10) primary key,
       name varchar2(30)
)

--手动创建
create index emp_idx
on emp2(name);

create index emp_idx2
on emp2(id, name);

4. 同义词
create synonym d1 for departments;

select * from d1;

5. 表:
DDL :数据定义语言 create table .../ drop table ... / rename ... to..../ truncate table.../alter table ...
DML : 数据操纵语言
insert into ... values ...
update ... set ... where ...
delete from ... where ...

【重要】select ... 组函数(MIN()/MAX()/SUM()/AVG()/COUNT())from ...join ... on ... 左外连接:left join ... on ... 右外连接: right join ... on ...where ... 
group by ... (oracle,SQL server中出现在select 子句后的非分组函数,必须出现在 group by子句后)having ... 用于过滤 组函数order by ... asc 升序, desc 降序

limit (0,4) 限制N条数据 如: topN数据

--union 并集
--union all(有重复)
--intersect 交集 
--minus 相减

DCL : 数据控制语言  commit : 提交 / rollback : 回滚 / 授权grant...to...  /revoke 

 

索引

何时创建索引:

一、
select employee_id, last_name, salary, department_id
from employees
where department_id in (70, 80) --> 70:1  80:34--union 并集
--union all(有重复部分)
--intersect 交集 
--minus 相减

select employee_id, last_name, salary, department_id
from employees
where department_id in (80, 90)  --> 90:4  80:34


--问题:查询工资大于149号员工工资的员工的信息
select * 
from employees
where salary > (
      select salary
      from employees
      where employee_id = 149
)

--问题:查询与141号或174号员工的manager_id和department_id相同的其他员工的
--employee_id, manager_id, department_id  
select employee_id, manager_id, department_id
from employees
where manager_id in (
      select manager_id
      from employees
      where employee_id in(141, 174)
) and department_id in (
      select department_id
      from employees
      where employee_id in(141, 174)
) and employee_id not in (141, 174);

select employee_id, manager_id, department_id
from employees
where (manager_id, department_id) in (
      select manager_id, department_id
      from employees
      where employee_id in (141, 174)
) and employee_id not in(141, 174);

--1. from 子句中使用子查询
select max(avg(salary))
from employees
group by department_id;

select max(avg_sal)
from (
      select avg(salary) avg_sal
      from employees
      group by department_id
) e

--问题:返回比本部门平均工资高的员工的last_name, department_id, salary及平均工资

select last_name, department_id, salary, (select avg(salary) from employees where department_id = e1.department_id)
from employees e1
where salary > (
      select avg(salary)
      from employees e2
      where e1.department_id = e2.department_id
)

select last_name, e1.department_id, salary, avg_sal
from employees e1, (
     select department_id, avg(salary) avg_sal
     from employees
     group by department_id
) e2
where e1.department_id = e2.department_id
and e1.salary > e2.avg_sal;


--case...when ... then... when ... then ... else ... end 
--查询:若部门为10 查看工资的 1.1 倍,部门号为 20 工资的1.2倍,其余 1.3 倍
select employee_id, last_name, salary, case department_id when 10 then salary * 1.1
                                                          when 20 then salary * 1.2
                                                          else salary * 1.3
                                                          end "new_salary"
from employees;

select employee_id, last_name, salary, decode(department_id, 10, salary * 1.1,
                                                             20, salary * 1.2,
                                                             salary * 1.3) "new_salary"
from employees;


--问题:显式员工的employee_id,last_name和location。其中,若员工
--department_id与location_id为1800的department_id相同,则location为’Canada’,其余则为’USA’。
select employee_id, last_name, case department_id when (
                    select department_id
                    from departments
                    where location_id = 1800
) then 'Canada' else 'USA' end "location"
from employees;

--问题:查询员工的employee_id,last_name,要求按照员工的department_name排序
select employee_id, last_name
from employees e1
order by (
      select department_name
      from departments d1
      where e1.department_id = d1.department_id
)

-- SQL 优化:能使用 EXISTS 就不要使用 IN

--问题:查询公司管理者的employee_id,last_name,job_id,department_id信息
select employee_id, last_name, job_id, department_id
from employees
where employee_id in (
      select manager_id
      from employees
)


select employee_id, last_name, job_id, department_id
from employees e1
where exists (
      select 'x'
      from employees e2
      where e1.employee_id = e2.manager_id

-- 问题:查询departments表中,不存在于employees表中的部门的department_id和department_name
select department_id, department_name
from departments d1
where not exists (
      select 'x'
      from employees e1
      where e1.department_id = d1.department_id
)

--55. 更改 108 员工的信息: 使其工资变为所在部门中的最高工资, job 变为公司中平均工资最低的 job
update employees e1
set salary = (
    select max(salary)
    from employees e2
    where e1.department_id = e2.department_id
), job_id = (
   select job_id
   from employees
   group by job_id
   having avg(salary) = (
         select min(avg(salary))
         from employees
         group by job_id
   )
)
where employee_id = 108;
--56. 删除 108 号员工所在部门中工资最低的那个员工.
delete from employees e1
where salary = (
      select min(salary)
      from employees
      where department_id = (
            select department_id
            from employees
            where employee_id = 108
      )
)

select * from employees where employee_id = 108;
select * from employees where department_id = 100
order by salary;

rollback;

常见的SQL面试题:经典50题

========================================================

已知有如下4张表:

学生表:student(学号,学生姓名,出生年月,性别)

成绩表:score(学号,课程号,成绩)

课程表:course(课程号,课程名称,教师号)

教师表:teacher(教师号,教师姓名)

根据以上信息按照下面要求写出对应的SQL语句。

ps:这些题考察SQL的编写能力,对于这类型的题目,需要你先把4张表之间的关联关系搞清楚了,最好的办法是自己在草稿纸上画出关联图,然后再编写对应的SQL语句就比较容易了。下图是我画的这4张表的关系图,可以看出它们之间是通过哪些外键关联起来的:

一、创建数据库和表

为了演示题目的运行过程,我们先按下面语句在客户端navicat中创建数据库和表。

(如何你还不懂什么是数据库,什么是客户端navicat,可以先学习这个:

1.创建表

1)创建学生表(student)

按下图在客户端navicat里创建学生表

学生表的“学号”列设置为主键约束,下图是每一列设置的数据类型和约束

创建完表,点击“保存”

2)创建成绩表(score)

同样的步骤,创建"成绩表“。“课程表的“学号”和“课程号”一起设置为主键约束(联合主键),“成绩”这一列设置为数值类型(float,浮点数值)

3)创建课程表(course)

课程表的“课程号”设置为主键约束

4)教师表(teacher)

教师表的“教师号”列设置为主键约束,

教师姓名这一列设置约束为“null”(红框的地方不勾选),表示这一列允许包含空值(null)

2.向表中添加数据

1)向学生表里添加数据

添加数据的sql

  1. insert into student(学号,姓名,出生日期,性别)
  2. values( '0001' , '猴子' , '1989-01-01' , '男');
  3. insert into student(学号,姓名,出生日期,性别)
  4. values( '0002' , '猴子' , '1990-12-21' , '女');
  5. insert into student(学号,姓名,出生日期,性别)
  6. values( '0003' , '马云' , '1991-12-21' , '男');
  7. insert into student(学号,姓名,出生日期,性别)
  8. values( '0004' , '王思聪' , '1990-05-20' , '男');

在客户端navicat里的操作

  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值