Skip to main content

To Find Second Max Salary Using Mysql query

CREATE TABLE `Emp_Det` (
`Emp_ID` int(11) default NULL,
`Emp_name` varchar(100) default NULL,
`Salary` decimal(18,3) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `Emp_Det` (`Emp_ID`, `Emp_name`, `Salary`) VALUES
(1, 'Prakash', 1000.000),
(2, 'Praful', 1200.000),
(3, 'Manish', 1100.000),
(10, 'Nilesh', 900.000),
(11, 'Chetan', 1200.000);
1) To find Second Max salary 

SELECT
 max( Salary ) 
FROM Emp_Det
WHERE Salary NOT 
IN (


SELECT max( Salary ) 
FROM Emp_Det
)


O/p:

max(Salary)
1100.000


2) To find Second Max salary with Description


SELECT *
FROM `Emp_Det`
WHERE Salary NOT
IN (


SELECT max( Salary )
FROM `Emp_Det`
)
ORDER BY salary DESC
LIMIT 1 ;

OR
SELECT *
FROM Emp_det
WHERE 
Salary = (
SELECT Max( 
Salary )
FROM Emp_det
WHERE 
Salary < (
SELECT Max( 
Salary )
FROM Emp_det ) ) ;



O/p:-

Emp_ID  Emp_nameSalary
3           Manish          1100.000


1) To Find Duplicate Salary


SELECT Salary
FROM Emp_Det
GROUP BY Salary
HAVING count( * ) >1
LIMIT 0 , 30

O/p:

Salary
1200.000



1) How to use case in mysql?
SELECT a.id, a.name, 
CASE b.Salary
WHEN 0 
THEN 'Absent'
ELSE '1000'
END AS Salary
FROM animal a, emp_det b
WHERE a.id = b.Emp_ID
LIMIT 0 , 30


=========================================================

How to write a query to get the second largest value from a given column of a table?
Ans: 

 This is a query to get the second largest value from a given column of a table,

SELECT MAX(COLUMN_NAME) FROM TABLE_NAME
WHERE COLUMN_NAME <
(SELECT MAX(COLUMN_NAME) FROM TABLE_NAME)

For Example:-
To get the second largest marks from “Marks” column of a “Students” table
Select Max(Marks) from Students
Where Marks< (Select Max(Marks) from students)


How to write a query to get 10thhighest salary from an employee table?
Ans:

This is a query to get nth highest salary from an employee table,
SELECT *
FROM Employee Emp1
WHERE ( n ) = (
SELECT COUNT( DISTINCT ( Emp2.Employee_Salary ) )
FROM Employee Emp2
WHERE Emp2.Employee_Salary >= Emp1.Employee_Salary
)

Here, you can replace the n with any number. For example, if you want to get10th highest salary, then replace n with 10.

SELECT *
FROM Employee Emp1
WHERE (10) = (
SELECT COUNT( DISTINCT ( Emp2.Employee_Salary ) )
FROM Employee Emp2
WHERE Emp2.Employee_Salary >= Emp1.Employee_Salary
)
=================================================== 

Comments

Popular posts from this blog

Learn phpfox

PHPFox  is a social network script, it is an internet application and when you install it, it is a website. The  phpfox  script comes in 3 packages, each with a different set of modules. it has two products: 1. Nebula (upto phpfox 3.8) 2. Neutron (Newer) You can check the demo on :  http://www.phpfox.com =================================================== To clear cache in phpfox follow the following steps, admincp >> Tools >> Maintenance >> Cache Manager >> Click on Clear All button =================================================== To work facebook app on local Following settings need to done in facebook app   1) go => setting => Advance 2) see "OAuth Settings" area and set "Valid OAuth redirect URIs" =  http:// projectdomain /index.php?do=/user/login/, http:// projectdomain .com/index.php?do=/user/register/, http:// projectdomain .com, http:// projectdomain .com/index.php 3) en...

How to Make Your Own PHP Captcha Generator

In this article we will create file based simple yet successful captcha generator. 3 Major Anti-spamming techniques used? Mathematical Operation like Random number + Random Number = -> The user must specify the answer Random word -> User must type the word Random question -> Obvious one which the user should answer correctly [ex: Are you human?] How Captcha works? The captcha generator generates an IMAGE with the question and then put up a session variable storing the value. User input though an input box. Using php POST, we compare the session variable data with the user input and tell whether its a bot or human. Its coding time The Code First let's write the php script which generates the captcha image. We use the simple header-content change technique, from which we can easily bring up an image from a given text. captcha.php PHP Code: array("Num" => "Num"), 1 => array("Are y...

Joomla Flash Tutorial

Joomla Flash Tutorial Adding a custom-made Flash animation created with Anim-FX to Frontpage is a snap By following these simple steps, you will be up and running in no time at all and impressing all of your visitors with your talents! First create your Flash animations in Anim-FX, and make sure the *.swf and *.txt files are in the base html directory.  Do not make the mistake of uploading them into the images or images/flash directory.  Also the flash does not work if loaded into the template directory.  Both files must be in the html directory where the index.php file is located. Then you can either copy the HTML generated by Anim-FX into the index.php file to the appropriate location or use a Joomla Flash Module.  Edit the index.php file which drives your Joomla template. Method 1: Editing the index.php for your Joomla template: Copy the follow...