数据库的相关SQL查询语句是软件测试工程师面试的一大重点,也是很多小伙伴面试中觉得比较困难的知识点。下面小编总结出一些SQL语句的常用语法公式和常见的面试题目。帮助大家打开快速掌握软件测试面试中SQL题目的技巧:
1、语法公式:
- 简单查询
select * from 表;
select 列名,…… from 表;
- 比较查询
select 列
from 表
where 列 比较运算符 值;
- 多条件查询
select 列
from 表
where 条件1 逻辑运算符 条件2 逻辑运算符 条件3 ……;
- 模糊查询
select 列
from 表
where 列 like ‘通配符(匹配内容)通配符’;
- 范围查询
select 列
from 表
where 列 between 值1 and 值2;
- 列表查询
select 列
from 表
where 列 in/not in (值1,值2,值3……);
- 排序查询
select 列
from 表
(where 条件)
order by 列 asc/desc
- 分组查询
select 列
from 表
group by 列(本列应该在select中出现);
select 列,聚合函数
from 表
group by 列
having 条件(可以使用聚合函数);
- 连接查询
select 表1.列,表2.列
from 表1,表2
where 表1.列=表2.列 and 其他条件(可有可无);
select 表1.列,表2.列,表3.列……
from 表1,表2,表3……
where 表1.列=表2.列 and 表2.列=表3.列 and 其他条件;
- 嵌套查询(子查询)
select 列
from 表1
where 列 not in/= /in (
select 列
from 表
where 条件(可有可无)
);
二、面试真题分享:
- mysql查询一个表中的所有数据的SQL语句?
- 例如查询学生表(student)中的所有学生
Select * from student;
- mysql进行模糊查询的SQL语句?
- 例如查询所有姓张的学生。
Select * from student where stu_name like "张%";
- mysql进行排序查询的SQL语句?
- 例如查询某个学生的所有成绩,按照成绩倒序排列。
Select score from chengji where stu_no=‘10110’ order by score desc;
- mysql进行聚合函数查询的SQL语句?
- 例如查询某个学生的所有成绩的总分、平均分等。
Select sum(score),avg(score) from chengji where stu_no=‘10110’;
- mysql进行分组查询的SQL语句?
- 例如查询所有学生的各科成绩的总和。
Select cno,sum(score) from chengji group by cno;
- mysql进行连接查询的SQL语句?
- 例如学生信息和学生成绩不在同一个表,查询每一个班级的学生的所有成绩。
Select stu_name,stu_class,score
From student,chengji
Where student.stu_id=chengji.stu_id and stu_class=XX;
- mysql进行子查询的SQL语句?
- 例如,查询学生中性别和张三一样的学生的姓名、性别。
Select stu_name,stu_sex
From student
Where stu_sex in(
Select stu_sex from student where stu_name=‘张三’
);
- mysql进行批量添加数据的SQL语句?
- 例如需要添加100个学生的25个课程的数据用于测试,成绩还是随机数。
解析:首先生成一个学生表,添加100个学生;
再生成一个课程表,添加25个课程;
再利用表连接将两个表的联合数据添加进成绩表
添加修改成绩表中的成绩数据,设置为随机数。
go
declare @i int
set @i = 0
while @i<25001
begin
declare @r int
exec awf_RandInt 0,30,@r output
update student set class = '英语' where id = @r+''
set @i=@i+1
end
- mysql进行修改数据的SQL语句?
- 例如将学生张三的姓名改为张三三。
Update student set stu_name=‘张三三’ where stu_name=‘张三’;
- mysql进行数据删除的SQL语句?
- 例如将学生表中和张三同名的学生都删除,只保留一个。
delete from student
where stu_id not in (select min(stu_id) from stu_id where stu_name=‘张三’);