0% found this document useful (0 votes)
13 views

SQL_Queries update

SQL queries updated information
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

SQL_Queries update

SQL queries updated information
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

What is difference between Delete, Truncate and Drop


Delete : Delete is a DML command ,We can delete single record or multiple records
or entire table
but we can rollback using Delete command.
Truncate :Truncate is a DDL command,we can delete all records expect the
structure.But we cant rollback the data.
drop :Drop is a DDL command.we can delete all the records and table structure
also.but we cant the rollback the data.

Primary Key:
-----------
Primary Key means It doesn't allows duplicates and null values.

Unique Key:
-----------
It doesn't allows duplicate values but allows null values.

Joins :We have different types of Joins like:


----------------------------------------------

1.Inner Join
2.Left Outer Join
3.Right Outer Join
4.Full Outer Join
5.Cross Join
6.Self Join.

Inner Join :It will return matched records from both the tables.
Left Outer Join : It will return all the records from the left side of the table
and matched records from the right side of the table.
Right Outer Join : It will return all the records from right side of the table
and matched records from the Left side of the table.
Full Outer Join : It will return matched and unmatched records from both the
tables.
Cross Join: It join all reocrds of A tbl joins all reocrds b table.
Ex:
3 records A tbl
4 records in B tbl
3*4 =12 Records
Self Join: A table joinns with B tbl called as Self Join.

Ex : Table1
1
1
0
3
2
null
null

Ex : Table2
1
1
2
2
0
4
null
what is the result of
Inner Join: 9
Left Outer join(LOJ): 10
Right Outer Join(ROJ): 10
Full Outer Join(FOJ): 11
Cross Join: 49
Self Join:

How to display records in Oracle:


----------------------------------

1. Select empno,count(*) from emp group by empno having count(*)>1;

2. delete from emp where rowid not in (select max(rowid) from emp group by empno)

3. with cte as (select empid,row_number() over (partition by id order by date


desc) as rownum1
from emp)delete from cte where rownum1>1

How to retrive the top nth salary(means 2nd highest or 3rd highest salary) in
Oracle:
-----------------------------------------------------------------------------------
-

1. select * from emp e where 2 =(select count(distinct(sal))


from emp e2 where e2.sal >e.sal);

2. select * from (select empno,sal,rank() over(order by sal desc)as rnk


from emp)a where a.rnk=2

3. Select max(Sal) from emp where sal not in(select max(sal) from emp)---to get 2nd
highest salary

How to retrive the top 2nd highest salary based on dept wise in Oracle;
--------------------------------------------------------------------------

1. select * from (select empno,sal,rank() over(partition by deptno order by sal


desc)as rnk
from emp)a where a.rnk=2

2. select * from emp e where 2 =(select count(distinct(sal))


from emp e2 where e2.DEPTNO=e.DEPTNO and e2.sal >e.sal);

Analytical Functions:
--------------------
1.Rank():It gives the ranks, incase of same values are there it will skips the
ranks.
2.Dense_rank() : It gives the ranks ,incase of same values are there it wont skip
the ranks.
3.Row_Number() : It gives the row numbers.
4.Lag() : it gives the previous values
5.Lead() : It gives the next value.

Rank()---It skip the sequence


Dense_rank-- It doesn't skip the sequence.
Ex:
Id salary rank Dense_rank
1 15000 1 1
2 15000 1 1
3 10000 3 2

LAG FUNCTION --It Will Returns Previous Value

SYNTAX:

LAG(colname,INT,DEFAULT VALUE) OVER(colname)


If you want to compare previous emp sal and current emp sal we can use lag function
if you want to compare previous q1 profits compare with current quater profits
using lag function
SELECT empid,SAL, LAG(SAL,1,0)OVER(ORDER BY SAL DESC) AS PREV_SAL FROM EMP
select year,sales_amount,lag(sales_aoumnt,1,null) over (order by year) as
prev_sal_amt from tblname

SELECT empid,SAL, LAG(SAL,1,null)OVER(ORDER BY SAL ) AS PREV_SAL FROM EMP


Q1 current quater prev_qauter
1 2000 0
2 3000 2000
3 4000 3000
4 5000 4000

LEAD FUNCTION --IT WILL RETURN NEXT VALUE


SYNTAX:
LEAD(EXPR,INT,DEFAULT VALUE) OVER(EXPR)
If you want to compare CURRENT emp sal and NEXT emp sal we can use Lead function
SELECT Efull_name,SAL, LEAD(SAL,1,0)OVER(ORDER BY SAL DESC) AS PREV_SAL FROM EMP

Select id,sal, lead(sal,1,0)over(order by sal desc) as Next_sal from emp


Q1 current quater prev_qauter
1 2000 3000
2 3000 4000
3 4000 5000
4 5000 Null

Set Operators :

Union : It will return only unique records from both the tables.
Unoon all: It will return the unique and non unique records both the records from
both the tables
Intersect : It will returns common records from both the tables
Minus:it will return only unmatched records from first table.

what are the values we will get if we do Union and Union all ??

Table -A table-B
---------------------------
1 1
1 1
2 2
3 3
4 3
5 4
5 Null
Null
Null

Union:1,2,3,4,5,Null
Union all: 1,1,2,3,4,5,5,Null,Null,1,1,2,3,3,4,Null
Intersect : 1,2,3,4,Null
Minus :5

what is difference Sub Query and Co-Related sub queries:


----------------------------------------------------------

Sub-Query:First it will execute the inner query based on inner query , outer
query will execute and then it will return valaues.

Co-Related Sub query :Each record of outer query, entire inner query will be
execute.
If we have 20 records are there in outer query 20 times it
will execute inner query.

what are difference between View and materlise view:


-----------------------------------------------------

View : View is nothing but a virtual table and if base table changes automatically
it will reflect on the view
View cant change.
Materlised View :materlised view stored the query result physically.If base table
changes it wont reflect on materlised view.
We should refresh explictly.Performce wise materlised view is
good.
exec dbms_mview.refresh('mvname')

How to update wherever 0 is there need to update 1 and where ever 1 need to update
1 using sql query.:
-----------------------------------------------------------------------------------
-----------------

Update tablename set id =case when id =1 then 0 when id =0 then 1 else id end;

Who ever joined last 10 years back using sql Query:


----------------------------------------------------

SELECT *
FROM emp
WHERE hiredate >= sysdate - interval '10' year

Substring & Instring:


---------------------
Substring Syntax

select substring(value,start value, length)


Instring Syntax:

select instr('value1','value2')

How we will get before @ username using sql Query:


---------------------------------------------------

select substr('[email protected]',1,instr('[email protected]','@')-1)
from dual;

output : User
User

How we will get After @ username using sql Query:


-----------------------------------------------

select substr('[email protected]',instr('[email protected]','@')+1)
from dual

Output: ankitha

How to concat the names who ever having same id ??


----------------------------------------------------

I/P
id, name
1 raja
1 rani
2 india
2 usa
2 pakisthan

o/p:
id,name
1 raja,rani
2 india,pakisthan,usa

Query:
SELECT id,
listagg(name, ',') within group(order by name)
as concatname
FROM list1
group by id
order by id;

Question :I wanted to know how many "I" charter are repeated in "INDIA":
--------------------------------------------------------------------------

Query:
select length('INDIA')-length(replace('INDIA','I','')) from dual.
I want to do cummlitave sal using sql Query:
--------------------------------------------

SELECT
SAL,
SUM(SAL) OVER (ORDER BY SAL) CUMTOT
FROM EMP

Sal Cumm_sal
800 800
950 1750
1100 2850
1250 5350
1250 5350
1300 6650

How to cancat the two or more columns:


----------------------------------------

To concat the firstname,middlename and last name in emp table.

select (Firstname|| ' ' || Middlename || ' ' || Lastname) as Fullname from
tablename.

NVL(),NVL2(),Coalese()

Nvl() : It will return the second if first value is null.


Syntax :nvl(exp1,exp2)

nvl2() : if exp1 is null it will return exp3 values


if exp1 is not null it will return the exp2 values.
Coalesce() : It will return the first not null values.

You might also like