Skip to content

Latest commit

 

History

History
73 lines (54 loc) · 1.19 KB

176-second-highest-salary.md

File metadata and controls

73 lines (54 loc) · 1.19 KB

Question Description
Write a SQL query to get the second highest salary from the Employee table.


Question Approach


Question Tips


Question Analyze Procedure
N/A


Question Set Up and Solution Demo
Schema (MySQL v8.0)

CREATE TABLE Employee (
  `Id` INTEGER,
  `Salary` INTEGER
);

INSERT INTO Employee
  (`Id`, `Salary`)
VALUES
  ('1', '100'),
  ('2', '200'),
  ('3', '300');

CREATE TABLE Result (
  `SecondHighestSalary` INTEGER,
  `ignore` VARCHAR(1)
);

INSERT INTO Result
  (`SecondHighestSalary`, `ignore`)
VALUES
  ('200', '_');

Query #1

SELECT
	MAX(Salary) AS SecondHighestSalary
FROM
	Employee
WHERE Salary NOT IN (
  SELECT
  	MAX(Salary)
  FROM Employee
  );
SecondHighestSalary
200

View on DB Fiddle