You are on page 1of 8

1.

sum of price by name

select name,sum(price) as price from test group by name

2. sum of price by name. we need second highest

select tes123.name,tes123.price from (select name,sum(price) as price from test group by name )

tes123

where 1= (select count(distinct(t2.price)) from (select name,sum(price) as price from test group by
name) t2 where tes123.price>t2.price);

3. delete duplicate values

select * from

select name, price,dense_rank() over (partition by name order by price) rnk from test)

4. Deleting duplicate records

delete from test where rowid not in (select max(rowid) from test group by name)

5. Updating records using group by function

update test set price=1000 where name in (select name from test group by name having count(name) >
1)

6. Last 5 records from table

select * from (select * from test order by name desc) where rownum<=5

7. drop table EMP_SRC

the EMP_SRC table will be dropped from the db and It will be available in recycle bin

to get the dropped table use the below query

flashback table emp_src to before drop rename to emp_src_1

Error:
Error starting at line : 1 in command -

CREATE TABLE emp

empno    NUMBER(4),

 ename    VARCHAR2(10),

 job      VARCHAR2(9),

 mgr      NUMBER(4),

  hiredate DATE,

  sal      NUMBER(4),

  comm     NUMBER(4),

  deptno   NUMBER(4)

Error at Command Line : 3 Column : 10

Error report -

SQL Error: ORA-00911: invalid character

00911. 00000 - "invalid character"

*Cause: identifiers may not start with any ASCII character other than

letters and numbers. $#_ are also allowed after the first

character. Identifiers enclosed by doublequotes may contain

any character other than a doublequote. Alternative quotes

(q'#...#') cannot use spaces, tabs, or carriage returns as

delimiters. For all other contexts, consult the SQL Language

Reference Manual.

*Action:
Regular Expression
REGEXP_LIKE
REGEXP_REPLACE
REGEXP_SUBSTR
REGEXP_INSTR

1.REGEXP_LIKE
REGEXP_LIKE condition allows you to perform regular expression matching in the
WHERE clause of a SELECT, INSERT, UPDATE, or DELETE statement.
Syntax

REGEXP_LIKE ( expression, pattern [, match_parameter ] )

SELECT last_name
FROM contacts

WHERE REGEXP_LIKE (last_name, 'Anders(o|e|a)n');


example will return all contacts whose last_name is either Anderson, Andersen, or
Andersan. The | pattern tells us to look for the letter "o", "e", or "a".

SELECT last_name
FROM contacts

WHERE REGEXP_LIKE (last_name, '^A(*)');


Will return all names which starts with A

SELECT last_name
FROM contacts

WHERE REGEXP_LIKE (last_name, '(*)n$');

Will return all names ends with n

REGEXP_REPLACE
REGEXP_SUBSTR
REGEXP_INSTR

To get employe id =2 Manager name from the table

select name from EMP_MNG where emp_id in( select mng_id from EMP_MNG where
emp_id=2)

To get only Manger names from the table

select name,mng_id from emp_mng e1 where emp_id in ( select mng_id from emp_mng
e2 where e2.mng_id=e1.emp_id)

To get distinct records based on kms

select distinct greatest(source,dest), least(source,dest),kms from AMAZON_TEST


To get current dep_id and prev_id from emp_dept table

select enumber, dept_id as current_department_number,


lag(dept_id) over (partition by enumber order by star_date) as
previous_department_number
from emp_dept;

To get department wise 2nd highest sal

select * from
(select empno,ename,sal,deptno,dense_rank() over(partition by deptno order by sal
desc) rnk from EMP_12282017) where rnk=2

Manager with how many emp are working under him

select e1.ename,count(e1.ename) from EMP_12282017 e2,EMP_12282017 e1 where


e1.empno=e2.mgr
group by e1.ename having count(e1.ename)<2

To get count of null values

select count(nvl(id,0)) from naveentest1 where id is null

Swaping 2 columns data

Update table_name set column1=coulmn2,coulmn2=column1


To get 2nd occurrence of .(dot) from email column

select substr(email,instr(email,'.',1,2),length(email)) from SUBSTRINSTR where


instr(email,'.',1,2)<>0

To get only domain name from email column

select substr(email,(instr(email,'@',1))+1,length(email)) from SUBSTRINSTR

While joining it will consider duplicate records as different records

Names with comma separated with department number

SELECT deptno, LISTAGG(ename, ',') WITHIN GROUP (ORDER BY ename)from emp


GROUP BY deptno;

Last 2 records from table

select * from (select * from emp order by rownum desc) where rownum<=2

Even records from table

select * from (select empno,ename,rownum rn from emp order by rk desc) where


mod(rk,2)=0
Odd records from table

select * from (select empno,ename,rownum rn from emp order by rk desc) where


mod(rk,2)=1

To get 2nd highest salary with location from 3 table(EMP1,DEPT1,Location)

select * from
(select emp_id,dept_id,dense_rank() over(partition by dept_id order by sal desc) rk from
emp1) a
join LOCATION on a.dept_id=LOCATION.dept_id where a.rk=2

2nd occurrence of .(dot) after @ in mail column

select
substr(substr(eaddr,instr(eaddr,'@',1)+1),instr(substr(eaddr,instr(eaddr,'@',1)+1),'.',1,2)+1) from
table1
where instr(substr(eaddr,instr(eaddr,'@',1)+1),'.',1,2) > 0

Merge

when you want to do an "upsert" i.e. update existing rows in a table or insert new rows
depending on a match condition. This is typically the case when you have to synchronize a table
periodically with data from another source (table/view/query). In place of 3 separate unwieldy INSERT,
UPDATE and DELETE statements with conditional sub-queries, the all-in-one MERGE does the job in one
shot

Syntax
MERGE into <target table>
USING
<souce table/view/result of subquery>
ON
<match condition>
WHEN MATCHED THEN
<update clause>
<delete clause>
WHEN NOT MATCHED THEN
<insert clause>

Example
SQL> merge into student a
2 using
3 (select id, name, score
4 from student_n) b
5 on (a.id = b.id)
6 when matched then
7 update set a.name = b.name
8 , a.score = b.score
9 when not matched then
10 insert (a.id, a.name, a.score)
11 values (b.id, b.name, b.score);

To get record count of all tables in schema

select table_name,num_rows from all_tables where owner='TESTUAT'

To get random values from table(It should print different result whenever
we run select query)

select * from table order by dbms_random.value

You might also like