Sunday, July 6, 2008

Interesting SQL Query

Write a SQL query to compute the Nth Maximum Salary

Lets get started with an easy one.
Write a SQL query to compute the 2nd Maximum Salary

select Max(Salary)
from Employee
where Salary != (select Max(Salary) from Employee);

To see how it works, you need to understand how a nested query is executed:
First the innermost (nested) query is executed and its result is placed in a temporary table, then the outer query is executed which uses that result.

So if you have to find the 3rd Maximum Salary, you could extend the previous query by increasing one more level of nesting. And so on.

A more interesting and simpler way to find Nth maximum salary would be the following correlated subquery

select * from Employee E1 where
(N-1) = (select Count(Distinct(E2.Salary))
from Employee E2
where E2.Salary > E1.Salary );

Let us now see how this correlated query works:
For each tuple of table Employee, the inner query is executed once and
the result is now stored in some temporary form which is used by the
outer query.

Thus, this correlated query takes O(n*n) time (quadratic time) as compared to linear time {O(n+n) = O(n)} of nested queries but it is still better than nesting the query n number of times as that would result in cumbersome code. However, both would take equal time. :-)

The best way to understand the query is to dry run it on some sample data and some value for N

Is Java "Purely" Object-Oriented ??

There are two schools of thought here......obviously disagreeing on the issue.

One, considers Java to be purely Object-oriented language as
Every single program that you can write in Java has to be encapsulated
in a Class (unlike in languages like C++) i.e you cannot write any java
program without having to write a class and since we know that every

class in java implicitly extends Object class, thereby exhibiting characteristics and features of an Object-Oriented Language.

On the other hand, some people refute the previous claim by saying that
Since there are primitive data types in Java such as int, double, char, boolean etc. and we can write a simple program in Java without having to create any object or without having to use other objects or predefined classes, so it can't be called as a completely object-oriented language, especially when we compare it with languages such as Smalltalk, Python, Lisp etc.

As it goes, I'm also a bit confused on the issue but i would like to believe that Java is a
purely object-oriented language.

Any comments, corrections are welcome and required. :-)

Tuesday, July 1, 2008

My Blog Wins Award

My Blog Entry JavaMail for Starters !! is one of those few, which have won themselves quite a fortune in Sun's Student Review Contest on NetBeans and OpenSolaris. You can see the complete result here.

The blog entry was about how to send Email from a Java Application, making use of JavaMail API. The blog was written for the target audience, primarily unfamiliar with such API's and using them with NB.

Tuesday, June 17, 2008

New Sun Campus Ambassador for Delhi University

I'm feeling extremely happy to announce Aadhar Mittal as the New Sun Campus Ambassador for Department of Computer Science, University of Delhi.
Aadhar Mittal is pursuing Masters of Computer Applications (MCA) from University of Delhi and is immensely talented and hardworking student.
Hope that he carry forward the legacy and do wonders. Wishing him all the best for all his future endeavours.

Also, if you haven't tried Mozilla's FireFox RC 3 till now, go download it here and see why people (read smart, intelligent and trendy people) do not use IE anymore. :-)
I just love the way its Smart Location Bar works. It also helps me manage my bookmarks easily.