. SQL(Structured Query Language)can be sperated as such categories:
1. DDL: CREATE, ALTER,DROP
2. DCL: GRANT REVOKE
3. DML: SELECT INSERT DELETE UPDATE
. Common used system functions:
1. Charactor
length(字符长度),lenthb(字节长度),trim(ltrim & rtrim, pay attention to no-varible length column) ,substr(source_str, startindex[1,2,3,...],length),
2. Date
sysdate, current_date( ALTER SESSION SET NLS_DATE_FORMAT-'dd-mon-yyy hh:ni:ss'), next_day(sysdate,'星期一')(从目前开始记,下一个星期一的时间).
3. Convert
to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),to_date,to_number
4. Aggregative function
sum,avg,max,min,count(count(*)&count(column1) may return differnt result)
(Aggretive function can not accompany by where, select pub, sum(price*quantity) from book where sum(price*quantity)>500 Group by pub is not correct. select pub, sum(price*quantity) from book group by pub having sum(price*quantity)>500 is well. select al,count(al) from aa group by a1 having count(a1) >1)
5. Other function
select user form dual. (current login users).
select sum(decode(sex,'男',1,0)) 男人数,sum(decode(sex,'女',1,0)) 女人数 from Table1
select al,nvl(a2,"未输入") a2 from Table2 (空值处理)
select count(*) from Table3 where a2 is(in not) null
. Group columns after select must appears in group by (select pub, book_name, sum(price*quantity) from books group by pub is not correct!)
. Fuzzy Query. _ single character, __ two charactors, % any characters.
. Table Join.
Inner Join: select e.id, e.sex,d.dep_name from e join d on d.dep_id=e.dep_id is equal to select e.id ,e.sex, d.dep_name from e, d where e.dep_id=d.dep_id.
(All the tables must total -match!!!)
Outer Join: select e.id ,e.sex,d.dep_name from e, d where e.dep_id=d.dep_id(+) (left join )
select e.id ,e.sex,d.dep_name from e, d where e.dep_id(+)=d.dep_id (right join )
(Tables are part-match!!!)
. Sub Query.
无关(独立)子查询: select * from a where id in (select id from d) select * from a where exists (select * from d )
相关子查询:select * from a where id in(select id from d where id =a.id) select * from a where not exists (select * from d where d.id=a.id )
Please pay attention to follow:
1. sub query can not contain * in select column when where condition is in.
2. join and sub query may perform the same task. first use join (higher performance).
. Other tips.
Union (operation on rows ): select * from a union select * from b. (number and type of columns must be same)
Intersect: select id from e intersect select id from d; (only for oracle)
Batch Insert: Insert into e (eid,name) select id,name from d (number and type of columns must be same)
Create Table from other table: create table ttt as (select * from e) (include data, only for oracle)
Oracle -SQL
![](https://img2024.cnblogs.com/blog/35695/202408/35695-20240826070647288-243310440.jpg)