Friday, 23 January 2015
Friday, 21 November 2014
Wednesday, 27 August 2014
Longest Common Subsequence in Java using Dynamic Programming
In case you find some error feel free to comment.
/*
I haven`t refactored the code for words with spaces
This Program uses Dynamic Programming Approach
*/
class LCS
{
public static void main(String args[])
{
System.out.print("Enter First String : ");
String x=System.console().readLine();
System.out.print("Enter Second String : ");
String y=System.console().readLine();
char[]xArray=x.toCharArray();
char[]yArray=y.toCharArray();
int xSize=xArray.length;
int ySize=yArray.length;
int lcs[][]=new int[xSize+1][ySize+1];
String marks[][]=new String[xSize][ySize];
for(int j=0;j<=ySize;j++)
{
lcs[0][j]=0;
}
for(int i=0;i<=xSize;i++)
{
lcs[i][0]=0;
}
for(int i=1;i<=xSize;i++)
{
for(int j=1;j<=ySize;j++)
{
if(xArray[i-1]==yArray[j-1])
{
lcs[i][j]=lcs[i-1][j-1]+1;
marks[i-1][j-1]="D";
}
else if(lcs[i][j-1] >= lcs[i-1][j])
{
lcs[i][j]=lcs[i][j-1];
marks[i-1][j-1]="L";
}
else
{
lcs[i][j]=lcs[i-1][j];
marks[i-1][j-1]="U";
}
}
}
/*
System.out.println("\nLCS Matrix is : ");
System.out.println();
for(int i=0;i<=xSize;i++)
{
for(int j=0;j<=ySize;j++)
{
System.out.print(lcs[i][j]+"\t");
}
System.out.println();
}
*/
/* System.out.println("Marks Matrix is :");
for(int i=0;i<xSize;i++)
{
for(int j=0;j<ySize;j++)
{
System.out.print(marks[i][j]+" ");
}
System.out.println();
}
*/
int i=xSize-1;
int j=ySize-1;
System.out.println("----------------------------------------------");
int counter=lcs[xSize][ySize];
System.out.println("Longest Common SubSequence Length is :"+counter);
System.out.println("----------------------------------------------");
char[]lcsArray=new char[counter];
int fill=0;
int seen=0;
while(seen != counter)
{
// System.out.print(marks[i][j]+" ");
if(marks[i][j].equals("U"))
{
i=i-1;
}
else if(marks[i][j].equals("L"))
{
j=j-1;
}
else
{
lcsArray[fill]=xArray[i];
fill++;
i=i-1;
j=j-1;
seen++;
}
}
System.out.print("Longest Common SubSequence is : ");
int lcsLength=lcsArray.length;
for(int k=0;k<lcsLength;k++)
{
System.out.print(lcsArray[lcsLength-k-1]);
}
System.out.println();
}
}
/*
I haven`t refactored the code for words with spaces
This Program uses Dynamic Programming Approach
*/
class LCS
{
public static void main(String args[])
{
System.out.print("Enter First String : ");
String x=System.console().readLine();
System.out.print("Enter Second String : ");
String y=System.console().readLine();
char[]xArray=x.toCharArray();
char[]yArray=y.toCharArray();
int xSize=xArray.length;
int ySize=yArray.length;
int lcs[][]=new int[xSize+1][ySize+1];
String marks[][]=new String[xSize][ySize];
for(int j=0;j<=ySize;j++)
{
lcs[0][j]=0;
}
for(int i=0;i<=xSize;i++)
{
lcs[i][0]=0;
}
for(int i=1;i<=xSize;i++)
{
for(int j=1;j<=ySize;j++)
{
if(xArray[i-1]==yArray[j-1])
{
lcs[i][j]=lcs[i-1][j-1]+1;
marks[i-1][j-1]="D";
}
else if(lcs[i][j-1] >= lcs[i-1][j])
{
lcs[i][j]=lcs[i][j-1];
marks[i-1][j-1]="L";
}
else
{
lcs[i][j]=lcs[i-1][j];
marks[i-1][j-1]="U";
}
}
}
/*
System.out.println("\nLCS Matrix is : ");
System.out.println();
for(int i=0;i<=xSize;i++)
{
for(int j=0;j<=ySize;j++)
{
System.out.print(lcs[i][j]+"\t");
}
System.out.println();
}
*/
/* System.out.println("Marks Matrix is :");
for(int i=0;i<xSize;i++)
{
for(int j=0;j<ySize;j++)
{
System.out.print(marks[i][j]+" ");
}
System.out.println();
}
*/
int i=xSize-1;
int j=ySize-1;
System.out.println("----------------------------------------------");
int counter=lcs[xSize][ySize];
System.out.println("Longest Common SubSequence Length is :"+counter);
System.out.println("----------------------------------------------");
char[]lcsArray=new char[counter];
int fill=0;
int seen=0;
while(seen != counter)
{
// System.out.print(marks[i][j]+" ");
if(marks[i][j].equals("U"))
{
i=i-1;
}
else if(marks[i][j].equals("L"))
{
j=j-1;
}
else
{
lcsArray[fill]=xArray[i];
fill++;
i=i-1;
j=j-1;
seen++;
}
}
System.out.print("Longest Common SubSequence is : ");
int lcsLength=lcsArray.length;
for(int k=0;k<lcsLength;k++)
{
System.out.print(lcsArray[lcsLength-k-1]);
}
System.out.println();
}
}
Friday, 22 August 2014
Thursday, 21 August 2014
Passed Oracle Java EE 6 Web Component Developer 1z0-899 Exam
First of all thanks to Almighty Dear Allah .
Today I took the exam and was able to pass it with 87 %. Thanks to Kathy , Bert and Bryan for writing the book for this exam.
I am thankful to Piotr Nowicki for his great questions related to Servlet 3.0 stuff ,Nikko for his hard work in preparing questions for this exam they are really helpful .
Big thanks to ranchers Bibeault , Tim , Ulf and list goes on. Thanks to KT for again helping me out with Credit card.
I want to share some tips for those who are preparing for the exam.
I read the Head First Jsp Servlet book . Did the exams given at the end of every chapter. And I was doing quite good.
As with any other certification exam you need practice .
Lots of material is already available here Scwcd Links , first time I used Enthuware and It is quite good. It will give you confidence to appear for the real test. Initially I find hard to remember all the API , once I made my own notes it was easy to remember them.
My scores in enthuware test studio
Test 1 72%
Test 2 74%
Test 3 82%
Test 4 68%
Test 5 79%
Test 6 84%
Test 7 79%
Test 8 79%
Test 9 81%
Test 10 80%
Don`t be lazy in making your own notes after taking mock tests. And don`t hesitate to try the code yourself and ranchers will be there to answer your silly questions.
Regarding the real exam it gives more preference to JSTL ,Jsp standard actions , MVC , Things you can do with DD . Annotation questions was very easy , it will be easy pick for you once you go through mock exams.
Memorize the API well if you want to score more.
And when you feel ready , go for it .
Knowledge should make us humble not arrogant.
Stay silent and let your success make the noise
You can read it at coderanch also coderanch
Today I took the exam and was able to pass it with 87 %. Thanks to Kathy , Bert and Bryan for writing the book for this exam.
I am thankful to Piotr Nowicki for his great questions related to Servlet 3.0 stuff ,Nikko for his hard work in preparing questions for this exam they are really helpful .
Big thanks to ranchers Bibeault , Tim , Ulf and list goes on. Thanks to KT for again helping me out with Credit card.
I want to share some tips for those who are preparing for the exam.
I read the Head First Jsp Servlet book . Did the exams given at the end of every chapter. And I was doing quite good.
As with any other certification exam you need practice .
Lots of material is already available here Scwcd Links , first time I used Enthuware and It is quite good. It will give you confidence to appear for the real test. Initially I find hard to remember all the API , once I made my own notes it was easy to remember them.
My scores in enthuware test studio
Test 1 72%
Test 2 74%
Test 3 82%
Test 4 68%
Test 5 79%
Test 6 84%
Test 7 79%
Test 8 79%
Test 9 81%
Test 10 80%
Don`t be lazy in making your own notes after taking mock tests. And don`t hesitate to try the code yourself and ranchers will be there to answer your silly questions.
Regarding the real exam it gives more preference to JSTL ,Jsp standard actions , MVC , Things you can do with DD . Annotation questions was very easy , it will be easy pick for you once you go through mock exams.
Memorize the API well if you want to score more.
And when you feel ready , go for it .
Knowledge should make us humble not arrogant.
Stay silent and let your success make the noise
You can read it at coderanch also coderanch
Friday, 18 April 2014
How to pass 1z0-047 sql expert exam
Bismillahir Rahmanir Raheem.
First of all, this exam title attracts many guys looking for some certificate.
Because the title specifies you as SQL expert guy.
To pass this exam you have to score atleast 66% out of 70 questions which means about 47 correct answers.
After doing my Java Programmer certificate I wanted to have this title.
So I purchased Steve O' Hearn book , which is very good one.
I already had some knowledge of SQL , but not much only about create,insert,update,delete and that also , not in depth knowledge.
It was not only the certificate , I also wanted to study sql in detail.
What I used as Study Material
I prepared from Steve O'Hearn SQL expert exam book , this book does not contain everything.
Some of the topic is not there in this book but this is not much of issue , you can read it somewhere else
Its really not an issue.
I also used Jason Price 11g Database administrator book
and Roopesh Ramklass ,John Watson 11g SQL Fundamentals book
And I spent time searching on internet also , when I was not sure about some topics.
What is not there in Steve`s Book
View with grant option , read only option is not in this book
Alternative quote operator is not in this book
Single and Double Ampersand Substitution operator are not there in this book.
Although Updateable view is there in the book but you might not understand it .
And It might be possible that you don`t understand what you read from the book
topics like hierarchical retrieval It took me quite a time to understand how it works and prior keyword
Which topics took long time for me to understand
Various Timezone functions
Hierarchical retrieval , Reporting Aggregated data using Group Functions
Subqueries
Flashback Feature
Yes ! You can pass this exam
Yes you can pass this exam. Yes exam is tough ,but you can still pass this exam.
All you have to do is to study hard , practice .
---------------Please Don`t Take this exam Lightly------------
If you take it , there is good amount of probability that you will fail this exam
How much Time it will take
I started one year before ,but not able to study regularly.
In between studied the book and left studied and left studied and left.
But then eventually I made My mind no matter what I am going to take this test in first week of April
I read some other guys stories , they only prepared for 1 months or even a weak or two.
And wrote the paper and passed it.
Don`t Hurry
Yes Don`t hurry.
It might happen that you get frustrated by keep studying books .
And not able to feel that confidance to pass the exam.
Don`t hurry by taking chance , hoping that you will pass this exam.
If you have good knowledge and lacking in some topics then you can still hope , but by not preparing for it and then hoping that you will pass this exam .I don`t think so thats going to happen
Take your time
About Real Exam
Two hours will be enough to answer every question
I was able to answer all question and review some questions .
But some questions were very ambiguous and that took some of my exam time.
In some questions not a single options seems to be correct but then question asks you to select two correct answer , it was very scary.
So except ambiguous questions on exam and aim for scoring 100%.
When I left the exam center I felt and thought I failed this exam.
As I was not sure of many of my answers.
But with the grace of Allah I was able to pass.
I nearly passed this exam with 69% and I was aiming for 100%.
Good luck for your preparations , Believe in yourself and Go for it.
Sunday, 6 April 2014
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
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
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;
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;
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';
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.
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);
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;
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
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.
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.
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.
Subscribe to:
Posts (Atom)






