Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts

Wednesday, July 04, 2012

Frequently used/asked SQL Queries

Finding the nth highest salary of an employee.

It is very easy to find the highest salary as:-
--Highest Salary
select max(Emp_Sal) from Employee_Test

Now, if you are asked to find the 3rd highest salary, then the query is as:-
--3rd Highest Salary
select min(Emp_Sal) from Employee_Test where Emp_Sal in
(select distinct top 3 Emp_Sal from Employee_Test order by Emp_Sal desc)

To find the nth highest salary, replace the top 3 with top n (n being an integer 1,2,3 etc.)
--nth Highest Salary
select min(Emp_Sal) from Employee_Test where Emp_Sal in
(select distinct top n Emp_Sal from Employee_Test order by Emp_Sal desc)

Alternate Way:-
SELECT * FROM MYTABLE T1
WHERE (N =
(SELECT COUNT(DISTINCT (T2.MYCOLUMN))
FROM MYTABLE T2
WHERE T2.MYCOLUMN >= T1.MYCOLUMN))

Finding TOP X records from each group

There are three groups of pgm_main_category_id each with a value of 17 (group 17 has four records),18 (group 18 has three records) and 19 (group 19 has three records).
Now, if you want to select top 2 records from each group, the query is as follows:-
select pgm_main_category_id,pgm_sub_category_id,file_path from
(
select pgm_main_category_id,pgm_sub_category_id,file_path,
rank() over (partition by pgm_main_category_id order by pgm_sub_category_id asc) as rankid
from photo_test
) photo_test
where rankid < 3 -- replace 3 by any number 2,3 etc for top2 or top3.
order by pgm_main_category_id,pgm_sub_category_id
The result is as:-

pgm_main_category_id   pgm_sub_category_id    file_path
17                         15                     photo/bb1.jpg
17                         16                     photo/cricket1.jpg
18                         18                     photo/forest1.jpg
18                         19                     photo/tree1.jpg
19                         21                     photo/laptop1.jpg
19                         22     

Deleting duplicate rows from a table
A table with a primary key doesn’t contain duplicates. But if due to some reason, the keys have to be disabled or when importing data from other sources, duplicates come up in the table data, it is often needed to get rid of such duplicates.
This can be achieved in tow ways :-
(a) Using a temporary table.
(b) Without using a temporary table.
(a) Using a temporary or staging table

Step 1: Create a temporary table from the main table as:-
select top 0* into employee_test1_temp from employee_test1
Step2 : Insert the result of the GROUP BY query into the temporary table as:-
insert into employee_test1_temp
select Emp_ID,Emp_name,Emp_Sal
from employee_test1
group by Emp_ID,Emp_name,Emp_Sal
Step3: Truncate the original table as:-
truncate table employee_test1
Step4: Fill the original table with the rows of the temporary table as:-
insert into employee_test1
select * from employee_test1_temp
Now, the duplicate rows from the main table have been removed.
select * from employee_test1
gives the result as:-
Emp_ID  Emp_name   Emp_Sal
1       Anees      1000
2       Rick       1200
3       John       1100
4       Stephen    1300
5       Maria      1400
6       Tim        1150
(b) Without using a temporary table

;with T as
(
        select * , row_number() over (partition by Emp_ID order by Emp_ID) as rank
        from employee_test1
)

delete
from T
where rank > 1

The result is as:-

Emp_ID  Emp_name   Emp_Sal
1       Anees      1000
2       Rick       1200
3       John       1100
4       Stephen    1300
5       Maria      1400
6       Tim        1150
  
Find DUPLICATE DATA

SELECT OBJECT_NAME NAME,
COUNT(OBJECT_NAME) MYCOUNT
FROM GENERALDB.JAVAOBJECTS
GROUP BY OBJECT_NAME
HAVING COUNT(OBJECT_NAME)>0

Question: Given an Employee table which has 3 fields - Id (Primary key), Salary and Manager Id, where manager id is the id of the employee that manages the current employee, find all employees that make more than their manager in terms of salary. Bonus: Write the table creation script.How do you find all employees that make more than their manager. There are 2 ways to do this - one, use a self join; and two; use a sub-query.

Self-join solution:
select e.*, m.Salary as "Manager Salary"
from Employee e
join Employee m on e.ManagerId = m.Id
where e.Salary > m.Salary


Here you are "joining" Employee table to itself on the FK relation of ManagerId and then querying on salary.

Sub-query solution:
select *
from Employee e
where e.Salary >
    (select m.Salary from Employee m
    where e.ManagerId = m.Id)