Monday, June 23, 2025
HomeJavaMethods to discover second highest or most wage of Worker in SQL

Methods to discover second highest or most wage of Worker in SQL


Methods to discover the second highest or second most wage of an Worker is among the most incessantly requested SQL interview questions much like discovering duplicate information in desk and when to make use of truncate vs delete. Tlisted here are some ways to seek out second highest wage based mostly upon which database you might be utilizing as totally different database gives totally different function which can be utilized to seek out the second most or Nth most wage of worker. Effectively this query may also be generalized with different state of affairs like discovering second most age and many others. On this SQL tutorial we are going to see totally different instance of SELECT SQL question to discover second highest wage impartial of databases or it’s possible you’ll name in ANSI SQL and different SQL queries which makes use of database particular function to seek out second most wage.

SQL question to seek out second most wage of Worker

On this part, we are going to write SQL queries to get the second highest wage of Workers. Earlier than writing question its good to be accustomed to the schema in addition to knowledge in desk. Right here is the Worker desk we might be utilizing this SQL instance:

mysql> SELECT * FROM Worker;
+——–+———-+———+——–+
| emp_id | emp_name | dept_id | wage |
+——–+———-+———+——–+
| 1      | James    | 10      |   2000 |
| 2      | Jack     | 10      |   4000 |
| 3      | Henry    | 11      |   6000 |
| 4      | Tom      | 11      |   8000 |
+——–+———-+———+——–+
4 rows IN SET (0.00 sec)

If you happen to have a look at knowledge, you can see that the second most wage, on this case, is 6000, and the worker title is Henry. 

Now let’s see some SQL examples to seek out out this second most wage.

Second most wage utilizing subquery and IN clause

mysql> SELECT max(wage) FROM Worker WHERE wage NOT IN (SELECT max(wage) FROM Worker);
+————-+
| max(wage) |
+————-+
|        6000 |
+————-+
1 row IN SET (0.00 sec)

Right here is one other SQL question to seek out second highest wage utilizing subquery and < operator as an alternative of IN clause:

mysql> SELECT max(wage) FROM Worker WHERE wage < (SELECT max(wage) FROM Worker);
+————-+
| max(wage) |
+————-+
|        6000 |
+————-+
1 row IN SET (0.00 sec)

Each of above SQL instance will work on all database together with Oracle, MySQL, Sybase and SQL Server as they’re written utilizing commonplace SQL key phrases. However someday you can even use database particular options like TOP key phrase of SQL Server or Sybase database to seek out out second highest wage of Worker.

how to find second highest salary in SQL

Second highest wage utilizing the TOP key phrase of Sybase or SQL Server database

TOP key phrase of Sybase and SQL Server database is used to pick out high document or row of any outcome set, by rigorously utilizing TOP key phrase you’ll find out second most or Nth most wage as proven beneath.

SELECT TOP 1 wage FROM ( SELECT TOP 2 wage FROM staff ORDER BY wage DESC) AS emp ORDER BY wage ASC

Here’s what this SQL question is doing : First discover out high 2 wage from Worker  desk and checklist them in descending order, Now second highest wage of worker is at high so simply take that worth. 

Although you want to remember of utilizing distinct key phrase if there are a couple of worker with high wage, as a result of in that case identical wage might be repeated and TOP 2 might checklist identical wage twice.

Second highest salary using the TOP keyword of Sybase or SQL Server database

Second most wage utilizing LIMIT key phrase of MYSQL database

SQL Query to find second highest or maximum salary of EmployeeLIMIT key phrase of MySQL database is little bit related with TOP key phrase of SQL Server database and permits to take solely sure rows from outcome set. If you happen to have a look at beneath SQL instance, its very a lot much like SQL Server TOP key phrase instance.

mysql> SELECT wage  FROM (SELECT wage FROM Worker ORDER BY wage DESC LIMIT 2) AS emp ORDER BY wage LIMIT 1;
+——–+
| wage |
+——–+
|   6000 |
+——–+
1 row IN SET (0.00 sec)

That’s on Methods to discover the second highest wage of Worker utilizing SQL question. That is good query which actually check your SQL information, its not robust however positively tough for newcomers. As observe up query you possibly can ask him to seek out third most wage or Nth most wage as nicely.

Different SQL Interview Query solutions it’s possible you’ll like



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments