Tuesday 26 November 2013

Changing NLS_Language NLS_Territory NLS_Calendar

Bismillahir Rahmanir Raheem

When I installed my Oracle database I probably selected US or I think default installation is US.
But you can change it as you like later
We will  see how to change it

You can see all your NLS_parameters from a dynamic view called NLS_Parameters

select *
from NLS_Parameters;

You will see in the results parameters like
NLS_Language    AMERICAN
NLS_Calendar    GREGORIAN
NLS_Territory     AMERICA

and others

so lets change the default values

alter session set NLS_Language='Arabic';

alter session set NLS_Territory='United Arab Emirates';

alter session set NLS_Calendar='Arabic Hijrah';


 Similarly you can change any NLS_parameters as you like

You might  want to know what are the values that you can assign to these NLS parameters

here is link to oracle documentation to help you

Oracle Database Globalization Support

Working with oracle timezone format TZR TZH TZM

Bismillahir Rahmanir Raheem

Today we will look to some format that are used to convert or outputting more precise information.

TZH--
This gives the hour from the timezone and is only valid with timestamp and interval formats

TZM----
This gives the minute from the timezone and is only valid with timestamp and interval formats

TZR---
This gives the time zone region information and is only valid with timestamp and interval formats

alter session set time_zone='asia/calcutta';

select to_char(systimestamp,'TZR-TZH-TZM')
from dual;

this will give us

Asia/calcutta-+05-30

NOte-- remember all these formats are only valid with timestamp and interval types not with date type

Monday 25 November 2013

INSERTING ROWs INTO TABLE FROM SELECTING ROWS FROM ANOTHER TABLE

Bismillahir Rahmanir Raheem

We will insert rows into a table from retreiving rows from another table

Create table table1(id number,name varchar2(20));

insert into table1 values(111,'Mahtab Alam');
insert into table1 values(222,'Aftab Alam');
insert  into table1 values(333,'Sdre Alam');

Now we have three rows in table1 .
Lets add these rows into table2

create table table2(RollNo number,Student_name varchar2(20));

insert into table2
select id,name
from table1;

or

create table table3(pid number);

insert into table3(pid)
select id
from table1;

Oracle To_date() function for converting into date datatype

Bismillahir Rahmanir Raheem

Suppose I have table Entry as

Create table Entry(Entered number);

insert into Entry values(12122012);
insert into Entry values(10102013);
insert into Entry values(11112013);

select * from Entry;





Now lets change it to some meaningful  way , lets change it to date which it actually represents

SELECT  ENTERED,TO_DATE(ENTERED,'DDMMYYYY')
FROM     ENTRY;

HOW TO FIND OUT THE AVAILABLE TIMEZONES IN ORACLE SQL

BISMILLAHIR RAHMANIR RAHEEM

You can query the dynamic view named  as   v$timezone_names to get the list  of
available timezones .

select *
from v$timezone_names;

This will give you a huge list  of timezone names .
Now you can use these names to change your timezone

You can change your timezone either by giving timezone offset or timezone regional name.

Its better to use timezone regional names

Here is the query

alter session set time_zone='ASIA/CALCUTTA';


Sunday 24 November 2013

Using where clause with group by

Bismillahir Rahmanir Raheem

Consider the table student where  I have three columns
student
dept
gender



select dept,count(student)
from   student
where gender='M'
group by dept
having count(student) >= 2;




In the above query I have used where before group by what it does is ,it ignores those rows from the
consideration of group by clause which does not satisfy the where condition.

So group by only makes group that satisfy the condition , this gives you a way to filter your group before they are made and by using having clause with group by we can filter goups after they have been made.

Saturday 23 November 2013

Correlated SubQuery -The best way irrespective of database you are working with

Bismillahir Rahmanir Raheem

Now I am going to talk about the best way to deal with Queries Like finding Second max or min or any other Salary position and the best thing about this approach is that it is irrespective of database.
So it will work with MySQL,Oracle,MS SQL and any other that you probably know.

this is our table account

Name                           Salary
A                                  1000
B                                   500
C                                   800
D                                  1500
E                                  2000
F                                  3000

Before this we were using independent subqueries but in this approach we will use Correlated SubQuery.

So what is the difference between independent subquery and correlated subquery.
Independent subquery can execute even if the outer query is not present
But Correlated subquery will only execute when there is an outer query present .
So Correlated Subquery is only meaningful when there is an outer query.


So How can we find Maximum salary using Correlated subquery

How many salary is higher than Maximum salary ?
Answer : 0

Here is the solution query

Select salary
from account x
where 0=( select count(*)
                 from account y
                 where y.salary > x.salary);

So what about finding Minimum salary
How many salary is higher than Minimum Salary ?
Answer : count(*)-1

So first find number of rows inn the database using

Select count(*) from account;

So lets calculate second Highest salary
How many salary is higher than Second Highest Salary ?
Answer : 1

Select salary
from account x
where 1=( select count(*)
                 from account y
                 where y.salary > x.salary);

Using rank() and dense_rank() to find any salary rank in SQL

Bismillahir Rahmanir Raheem

Table sal
name                                salary
A 1000
B               500
C 600
D 200
E 700
F                                       500

Difference Between -rank and dense_rank

select name,salary,rank() over(order by salary desc)Rank
from sal;

name salary Rank
A 1000 1
E 700 2
C 600 3
B 500 4
F 500 4
D 200 6


select name,salary,dense_rank() over(order by salary desc)Rank
from sal;

name salary Rank
A 1000 1
E 700 2
C 600 3
B 500 4
F 500 4
D 200 5


Now you can find out any rank as your requirement by using a where clause and specifying the rank you desire.

say third most highest

select name,salary
from
(
  select name,salary,rank() over(order by salary desc)Rank
  from sal
)
where Rank=3;

Finding Second Maximum or Second Minimum in SQL

Bismillahir Rahmanir Raheem

Ok we will be finding the second Maximum and second Minimum salary in this post.

So any Idea how we will go about this problem ?

Think about the problem we have to find the second maximum ,If we can remove the first Maximum and then find Maximum
Then We will get the second Maximum Similarly the case for Second Minimum.

Ok this is the table account

name            salary
A                10000
B                2000
C                500
D                1500
E                1000
F                800


That is how we can solve this question

Select Max(salary) as Second_Maximum
from account
where salary != ( Select Max(salary)
                  from account
                 );

Output will be

Second_Maximum
--------------
2000


Now using same logic we can get Second Minimum

Select Min(salary) as Second_Minimum
from account
where salary != ( Select Min(salary)
                  from account
                   );

Output will  be

Second_Minimum
---------------  
800

Retrieving Minimum Salary from the database without using Min function

Bismillahir Rahmanir Raheem

How to retrieve Minimum salary without using Min function

As we have retrieved the maximum salary the concept is same Use self join.

name                                    salary
A                                         4000              
B                                         3000    
C                                         5000
D                                         2500                                        
E                                         1000
F                                         1500

Select a.salary
from account a,account b
where a.salary > b.salary;

Now the  minimum salary is the only one which is not greater than any other salary
All other salary is greater to at least  minimum salary.

This will give result containing all salaries except minimum salary

Now we will write an outer query to select salary that is not inn salaries returned by inner query

Select salary as Minimum_Salary
from account
where salary not in (Select a.salary
                              from account a,account b
                              where a.salary > b.salary);

This will give you the required result

Minimum_Salary
-----------------
1000

Remember one thing Corelated queries is different than independent qurey.



Retrieving Maximum salary without using Max function in SQL

Bismillahir Rahmanir Raheem

Today We are going to find the maximum salary from the database
without using Max function.

So how to do that , we will use Self Join to do that.

Say this is our table Account

name                        salary
A                             4000
B                             3000
C                             5000
D                             2500
E                             1000
F                             1500

Note one thing that maximum salary is the only one that is not less than any other salary in the database
All other salaries are less than maximum salary and other.
Like 3000 is less than 4000 and 5000
        1000 is less than 1500,2500,3000,4000,5000
        Similarly for other salaries But 5000 is not less than any other salarie

Select a.salary
from account a,account b
where a.salary < b.salary;

This will give list of salary having all salary values except the Maximum Salary which is 5000 in this case.

Now we are just one step behind our answer , All we have to do is to nest this query in an outer query
The Outer query will say give me salary that is not in the result of the inner query

select salary as Maximum_Salary
from account
where salary not in ( Select a.salary
                               from account a,account b
                               where a.salary < b.salary);

Maximum_Salary
----------------
5000

This is your result.