if not exists (select * from table_name where aa=xx)
insert into table_name (a,b,c) values (xx,xx,xx)
else update table_name set a = xx,b = xx where aa = xx
select xx from xx where xx=xx order by id offset (pageNo-1)*pageSize rows fetch next pageSize rows only
select tbl1.id from table1 tbl1 where not exists (select 1 from table2 tbl2 where tbl1.id = tbl2.id);
overPaid如下执行sql后,salary大于4000的status列为overpaid,小于2000的overPaid,在2000到4000之间的为ok
name | salary | status |
---|---|---|
user1 | 800 | undearPaid |
user2 | 2000 | OK |
user3 | 4000 | overPaid |
select name,salary,
case when salary <= 2000 then 'undearPaid'
when salary >= 4000 then 'overPaid'
else 'OK'
end as status
from emp
虽然可以用case when then来实现相同功能,但既然有函数咋不用呢,如下将空值替换为0
select coalesce(commo,0) from emp;
如下所示先对departno进行排序,然后再对每个departno中的salary进行倒排
empno | departno | salary | name | job |
---|---|---|---|---|
1 | 10 | 5000 | king | president |
3 | 10 | 2500 | miller | manager |
2 | 20 | 4000 | James | analyst |
4 | 20 | 2000 | Black | clerk |
select empno,departno,salary,name,job from emp order by deptno,sal desc;