Created
April 14, 2014 09:06
-
-
Save sjmach/10630254 to your computer and use it in GitHub Desktop.
Some useful SQL queries for Oracle database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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