现在很多的IT公司都喜欢考SQL笔试题目,然而SQL语言,并非每个开发人员都可以熟练掌握。下面通过一些基础性的知识与实例来讲解,在笔试SQL题目时,应该如何去处理。
一、SQL语句执行顺序
例:
select num from table t where t.num > 10 group by num order by num
此SQL语句的执行顺序是:
1、先执行from 语句(无论from table的表现形式是:from table ,还是
from table a join table b on a.num = b.num);
2、再执行where语句;
3、接着执行group by语句;
4、然后执行order by语句;
5、最后执行select语句。
二、常用SQL语法
1、distinct
用于select语句里,主要是把重复的记录过滤掉;
select distince * from table;
2、inner join on或 join on
用于from语句里,主要关联两个表的记录,把两个表中都存在关系的记录显
示出来;
select * from table a inner join table b on a.id = b.id;
3、left join on
用于from语句里,主要关联两个表的记录,把左边表的全部记录显示出来,
然后右边表的记录如果与左边表存在关系,就显示出来,否则会以null表示;
select * from table a left join table b on a.id = b.id;
4、right join on
用于from语句里,主要关联两个表的记录,把右边表的全部记录显示出来,
然后左边表的记录如果与右边表存在关系,就显示出来,否则会以null表示;
select * from table a right join table b on a.id = b.id;
5、having
用于group by分组语句中,相当于where的用法。
select num from table a group by a.num having count(*) >2;
表示把大于2的num分组显示出来;
在分组语句里,还有一个地方要注意:就是select语句中,只能包括分组的字段
(num)或者统计的函数,例count,max,min等。
6、in和not in
用于where语句中。in 表示在什么范围里,not in 表示不在什么范围里。
select * from table a where a.num in('1','2');
表示查找num=1或2的记录;
select * from table a where a.num not in('1','2');
表示查找num<>1 and num <>2的记录;
7、 。。。。。。。。。。。。。。。。。。。。。。。
三、常见笔试题目
1、用一条SQL语句,查询出每门课都大于80分的学生姓名
name kecheng fenshu
张三 语文 81
张三 数学 75
李四 语文 77
李四 数学 60
小五 语文 80
小五 英语 70
小五 数学 100
select distinct name from table where name not in (select distinct
name from table where fenshu <= 80);
2、学生表,如下:
自动编号 学号 姓名 课程编号 课程名称 分数
1 01 李四 001 数学 60
2 01 李四 001 数学 60
3 02 小五 001 数学 60
删除除了自动编号不同,其它都相同的学生多余信息;
delete from table where 自动编号 not in (select min(自动编号) from
table group by 学号 姓名 课程编号 课程名称 分数);
3、。。。。。。。。。。。。。。。
由于时间的关系,有一部分的内容未能添加上去。以后有时间再完善此文档。欢迎转载,请注明出处。