maximum value in sql query code example

Example 1: sql display max value

SELECT MAX(<numeric column>) FROM <table>;
SELECT MAX(<numeric column>) FROM <table> GROUP BY <other column>;

Example 2: sql max value in column

SELECT MAX(salary) FROM employees;
SELECT id, MAX(salary) FROM employees GROUP BY id;

SELECT t1.*
FROM employees t1
INNER JOIN (
    SELECT id, max(salary) AS salary FROM employees GROUP BY id
) t2 ON t1.id = t2.id AND t1.salary = t2.salary;

Example 3: maximum element in sql

Who is the person getting 1st max salary
SELECT first-name
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees);

2nd max salary=
SELECT first-name , salary
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees WHERE salary NOT IN
(SELECT MAX(salary) FROM employees));

3rd max salary=
SELECT * FROM employees
WHERE salary = (SELECT * FROM (SELECT DISTINCT salary FROM employees
ORDER BY salary DESC) WHERE ROW NUM <=3
MINUS
SELECT * FROM (SELECT DISTINCT salary FROM employees
ORDER BY salary DESC) WHERE ROW NUM <=2);

Tags:

Sql Example