SQL_Queries update
SQL_Queries update
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.
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:
2. delete from emp where rowid not in (select max(rowid) from emp group by empno)
How to retrive the top nth salary(means 2nd highest or 3rd highest salary) in
Oracle:
-----------------------------------------------------------------------------------
-
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;
--------------------------------------------------------------------------
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.
SYNTAX:
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
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.
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;
SELECT *
FROM emp
WHERE hiredate >= sysdate - interval '10' year
select instr('value1','value2')
select substr('[email protected]',1,instr('[email protected]','@')-1)
from dual;
output : User
User
select substr('[email protected]',instr('[email protected]','@')+1)
from dual
Output: ankitha
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
select (Firstname|| ' ' || Middlename || ' ' || Lastname) as Fullname from
tablename.
NVL(),NVL2(),Coalese()