SQL-最最最最最²基础操作介绍(附面试题)
查找:select* from table1 where 范围
like查找:select* from table1 where field1 like '%value%'
select* from table2 where field2 like '1234%'
插入:insert into table1 (field1,field2)value(value1,value2)
删除:delete from table1 where 范围
更新:update table1 set field1 = value1 where 范围
排序:select * from table1 order by field1,field2 [desc]
总数:select count as totalcount from table1
求和:select sum(field1)as sumvalue from table1
平均:select avg(field1)as avgvalue from table1
最大:select max(field1)as maxvalue from table1
最小:select min(field1)as minvalue from table1
举例:
1.user表中,表字段为:name、email、password、loginTime;
写SQL:查询user表中用户名含有“测试”,并返回最近登录的前10条记录。
select* from user where name like '%测试%' order by loginTime desc limit 10;
2.找到pay库card表中的银行卡号bin_code开头为622848的卡;
select * from pay.'cardbin'a where a.bincode like '622848%';
无脑面试题:
- 用一条SQL语句 查询出每门课都大于80分的学生姓名
name kecheng fenshu 张三 语文81 张三数学75 李四语文76 李四数学90 王五语文81 王五数学100 王五英语90
答案:
select distinct name from table where name not in (select distinct name from table where fenshu<=80)
select name from table group by name having min(fenshu)>80
- 学生表 如下,删除除了自动编号不同,其他都相同的学生冗余信息
自动编号学号姓名 课程编号 课程名称 分数 12005001 张三 0001数学 69 22005002 李四 0001数学 89 32005001 张三 0001数学 69
答案:
delete tablename where自动编号not in(select min(自动编号) from tablename group by学号,姓名,课程编号,课程名称,分数)
- 多表联查
这个就不举例了。基本要求SQL能力的面试,都会问会不会多表联查