必背的sql语句

1:oracle 分页

select * from (select t.*, rownum rn from (select * from menu order by id desc) t where          rownum < 10) where rn >=5

2: mysql 分页

select * from music where id limit 5,5

3:oracle中如何快速将一张表的数据复制到另外一张表中(另外一张表不存在,另外一张       表存在,但数据为空)

  1. .不存在另一张表时:

create  table  新表  as  select * from 将要复制的表

  1. 存在另一张表时:

insert  into  新表名  select  字段  from  将要复制的表名

4:音乐专辑

查询出special <app:ds:special>表中的id  专辑名 并下面有多少首歌曲

Select  s.id , min(s.sname),count(m.mid)  from  special s  inner

join  ms  m  on  s.id=m.id  group  by  s.id

5:快速删除一张表(不可事物回滚,也就是没有日志记录)

TRUNCATE from 表名

6:inner join

select 查找信息 from 表名 1 inner join 表名2 on 表名1.列名 = 表名2.列名

7:left join

左外连接    select 查找信息 from 表名1 left join 表名2 on 表名1.列名 = 表名2.列名

8:right join

右外连接  select 查找信息 from 表名1 right join 表名2 on 表名1.列名 = 表名2.列名

9:oracle中查询遍历树形结构(start  with)

select * from extmenu

start with pid=1

connect by prior id = pid

快速删除父节点以及父节点下的所有节点:

Delete from extmenu where id in (

elect * from extmenu

start with pid=1

connect by prior id = pid

)

10:查询出来60-70,80-90,95-100学生的信息

select * from stu where chengji between 60 and 70 or between 80 and 90 or between 95 and 100

select * from stu where chengji > 60 and chengji < 70 or chengji > 80 and chengji < 90 or chengji > 95 and chengji < 100

11:用exists替换in------进行联表查询

select * from dept where exists(select * from emp where emp.deptno=dept.deptno);

select  *  from  dept  d  inner  join  emp  e  on  d.deptno = e.deptno(只查询出两表共同拥有的字段数据)

12:删除表中的重复数据:

delete from xin a where a.rowid != (

select max(b.rowid) from xin b

where a.name = b.name

);

13:row_number(),rank() over ,dense_rank() over 按工资排序

select sal,

row_number() over(order by sal desc) rank1,

rank() over(order by sal desc) rank,

dense_rank() over(order by sal desc) drank

from emp

14:select * from (select emp.* from(

dense_rank() over(partition by departNo order by sal desc)

rk from emp )

Where rk=4