Showing posts with label MySQL. Show all posts
Showing posts with label MySQL. Show all posts

Tuesday, 4 October 2011

Find second max salary in MySQL

To find max second salary without using sub-query
SELECT salary FROM `salary_table` GROUP BY `salary` DESC LIMIT 1 , 1;

By using sub-query, get all second salary records
SELECT max(salary) FROM `salary_table` WHERE salary = (SELECT max(salary)FROM `salary_table`);

Wednesday, 28 September 2011

In operator in mysql

If you want to select id values of 1,2,3 in same query we use IN operator in MySQL


Table Name : user

Id    LastName
1      Value1 
2      Value2
3      Value3 
4      Value4


SELECT * FROM `user` WHERE `Id` =1;

Id    LastName
1       Value1

It select only one value.

By using IN operator

SELECT * FROM `user` WHERE `Id` IN ('1','2','3');

Id    LastName
1      Value1 
2      Value2
3      Value3

SELECT * FROM `user` WHERE `Id` IN ('1','2','10');

In this query Id 10 is not in the table user

So output like this


Id    LastName
1      Value1 
2      Value2