Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Some useful SQL queries for Oracle database
-- SQL Query to find employees with top three salaries
select * from (
select * from employees order by salary desc)
wHERE rownum <= 3
--SQL query for top three distinct salaries
select * from (
select distinct(salary) from employees order by salary desc)
wHERE rownum <= 3
-- SQL query to find the employee with max salary
SELECT e.SALARY, E.FIRST_NAME
FROM employees e
where e.salary = (SELECT MAX(Salary) FROM employees)
-- Alternative solution to find the employee with max salary
select *
from(select * from employees order by salary desc)
where rownum <2
-- Second highest salary
SELECT MAX(Salary) FROM employees
WHERE Salary NOT IN (SELECT MAX(Salary) FROM employees )
-- SQL query - Select Nth row
select *
from
(
select
salary
,dense_rank() over (order by salary desc) ranking
from employees
)
where ranking = 4 -- Replace 4 with any value of N
--Displays odd only employees
select * from
(select first_name, last_name, salary, rownum rn
from employees
order by employee_id)
where mod (rn, 2) <> 0;
--Displays even employees
select * from
(select first_name, last_name, salary, rownum rn
from employees
order by employee_id)
where mod (rn, 2) = 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment