sql maximum 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: 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