常见的SQL面试题经典50例

北京哪家医院治疗白癜风得好 http://m.39.net/pf/a_4782744.html

作者:sh_c_

blog.csdn.net/u/article/details/

SQL基础知识整理select查询结果,如:[学号,平均成绩:组函数avg(成绩)]from从哪张表中查找数据,如:[涉及到成绩:成绩表score]where查询条件,如:[b.课程号=andb.成绩80]groupby分组,如:[每个学生的平均:按学号分组](oracle,SQLserver中出现在select子句后的非分组函数,必须出现在groupby子句后出现),MySQL中可以不用having对分组结果指定条件,如:[大于60分]orderby对查询结果排序,如:[增序:成绩ASC/降序:成绩DESC];limit使用limt子句返回topN(对应这个问题返回的成绩前两名),如:[limit2==从0索引开始读取2个]limit==从0索引开始[0,N-1]

select*fromtablelimit2,1;--含义是跳过2条取出1条数据,limit后面是从第2条开始读,读取1条信息,即读取第3条数据select*fromtablelimit2offset1;--含义是从第1条(不包括)数据开始取出2条数据,limit后面跟的是2条数据,offset后面是从第1条开始读取,即读取第2,3条

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

多表连接:内连接(省略默认inner)join...on..左连接leftjointableNameasbona.key==b.key右连接rightjoin连接union(无重复(过滤去重))和unionall(有重复[不过滤去重])

union并集unionall(有重复)

oracle(SQLserver)数据库

intersect交集minus(except)相减(差集)oracle一、数据库对象:表(table)视图(view)序列(sequence)索引(index)同义词(synonym)1.视图:存储起来的select语句

createviewemp_vwasselectemployee_id,last_name,salaryfromemployeeswheredepartment_id=90;select*fromemp_vw;

可以对简单视图进行DML操作

updateemp_vwsetlast_name=HelloKittywhereemployee_id=;select*fromemployeeswhereemployee_id=;

1).复杂视图

createviewemp_vw2asselectdepartment_id,avg(salary)avg_salfromemployeesgroupbydepartment_id;select*fromemp_vw2;

复杂视图不能进行DML操作

updateemp_vw2setavg_sal=00wheredepartment_id=;2.序列:用于生成一组有规律的数值。(通常用于为主键设置值)

createsequenceemp_seq1startwith1incrementby1maxvalue00minvalue1cyclenocache;selectemp_seq1.currvalfromdual;selectemp_seq1.nextvalfromdual;

问题:裂缝,原因:

当多个表共用同一个序列时。rollback发生异常

createtableemp1(idnumber(10),namevarchar2(30));insertintoemp1values(emp_seq1.nextval,张三);select*fromemp1;3.索引:提高查询效率

自动创建:Oracle会为具有唯一约束(唯一约束,主键约束)的列,自动创建索引

createtableemp2(idnumber(10)primarykey,namevarchar2(30))

手动创建

createindexemp_idxonemp2(name);createindexemp_idx2onemp2(id,name);4.同义词

createsynonymd1fordepartments;select*fromd1;5.表:

DDL:数据定义语言createtable.../droptable.../rename...to..../truncatetable.../altertable...

DML:数据操纵语言

insertinto...values...update...set...where...deletefrom...where...select...组函数(MIN()/MAX()/SUM()/AVG()/COUNT())from...join...on...左外连接:leftjoin...on...右外连接:rightjoin...on...where...groupby...(oracle,SQLserver中出现在select子句后的非分组函数,必须出现在groupby子句后)having...用于过滤组函数orderby...asc升序,desc降序limit(0,4)限制N条数据如:topN数据union并集unionall(有重复)intersect交集minus相减

DCL:数据控制语言


转载请注明:http://www.92nongye.com/zyjs/zyjs/204625185.html

  • 上一篇文章:
  •   
  • 下一篇文章: 没有了