Imp SQL Solved

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

Imp Queries

Example 1:

# Find 2nd highest employ salary


select * from
(select empno,ename,sal,
dense_rank() over(order by sal desc) as rnk
from emp
)
where rnk=2;

# Find department wise highest salary


select * from
(select empno,ename,sal,
dense_rank() over(partition by deptno order by sal desc) as rnk
from emp
) as emp_rnk
where rnk=1;
# Find department wise 2nd highest salary with its departments
select * from
(
SELECT e.empno, e.ename, e.sal,d.deptno,d.dname,
DENSE_RANK() OVER (partition by d.deptno order by e.sal DESC) AS rnk
from emp e
inner join dept d
on e.deptno=d.deptno
) as dept_rnk
where rnk=1;

Example-2:

# Find employee name and its manager name


 We can solve such queries using self join

select e.emp_name,e1.emp_name as man_name from emp_mng e


inner join emp_mng e1
on e1.emp_id=e.manager_id;
Example3:

# Find child name and its parent name (self join)


select f.name,f1.name as parent from family f
inner join family f1
on f.parent_id=f1.id;

Example 4:
>>select t.name||' Vs '||t1.name from mock t
inner join mock t1
on t1.id>t.id;

example 5:
# find the total distance travelled by each user (name,total_distance_travelled)

>>
select u.name,r.passenger_id,sum(distance_travlled)as total_distance_travelled from
rides r
inner join users u
on u.id=r.passenger_id
group by u.name, passenger_id
order by total_distance_travelled;
>>
select u.name,sum(distance_travlled)as total_distance_travelled from rides r
inner join users u
on u.id=r.passenger_id
group by u.name
order by total_distance_travelled;
Example 6:
# find even employee id
>> select * from employees where mod(employee_id,2)=0;
#find odd employee id
>> select * from employees where mod(employee_id,2)!=0;

Example:7
Rownum is a pseudo which generate unordered sequence number.
#fetch first 50% records from employ table
Example 8:
# Query to Find unique records
select name,sal,count(*) from emp_dup
group by name,sal
having count(*)=1;

# Query to find duplicate records


Syntax-
Select column1,column2,count(*) from table_name
Group by column1,column2
Having count(*) > 1;

select name,sal,count(*) from emp_dup


group by name,sal
having count(*)>1;
Example 9:
# Query to delete duplicate records
delete from emp_dup
where (name,sal) in
(select name,sal,count(*) from emp_dup
group by name,sal
having count(*)>1 );
--above query will delete all duplicate records but we want to remove only one
duplicate record and keep 1

ROWID:
- it is unique identification given to the each row in the table
- for duplicate also there will be different unique identification
- rowid is useful to delete duplicate records.

# Delete duplicate records using rowid


select max(rowid) from emp_dup
group by name,sal;
--this query gives unique records only

delete from emp_dup


where rowid not in
(select max(rowid) from emp_dup group by name,sal);
Example 10:
Table – orders

# Find the largest order amount for each sales person.


>> select sales_person_id,max(order_amount) as largest_amount from orders
group by sales_person_id;

# find the total order amount of each sales person


>> select sales_person_id,sum(order_amount) as total_amount from orders
group by sales_person_id;
Example 11:
Case statement:
CASE statement in SQL is a conditional expression that allows you to perform
different actions based on specified conditions.
It is same as if ..else statement .
Ex. Grade of student
If marks >= 90 then “A grade”

Syntax:
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
WHEN conditionN THEN resultN
ELSE default_result
END
# Order category
# replace - emp fisrt_name when Jane then Balaji, when bob then Ram and keep
remaining as its

# emp – when sex M then 1 and when sex F then 0

#student grades
select stud_id,name,marks,
case when marks between 85 and 100 then 'O'
when marks between 70 and 80 then 'A+'
when marks between 60 and 70 then 'A'
when marks between 50 and 60 then 'B'
when marks between 40 and 50 then 'C'
else 'FAIL' end as grades from student;
# Find total marks for each gender

>> select gender,sum(marks)as total from gender


group by gender;

# complicated -- Find total marks for each gender

select new_gender,sum(marks) from(


select gender,marks,
case when gender in ('Girl','Female','Women') then 'F'
when gender in ('Male','Boy','Men','Man') then 'M'
end as new_gender
from gender1
)
group by new_gender;
# cricket

>> select overs,sum(run) as run from


(
select ball_no,run,
case when ball_no between 1 and 6 then 1
when ball_no between 7 and 12 then 2
else 3 end as overs
from cricket
)
group by overs;
if the match is 500 balls then we have to write multiple case statements
generalize solution-

select over_number,sum(run) from


(
SELECT
CEIL(ball_no / 6) AS over_number,run
FROM Cricket
)
group by over_number;

(or)
SELECT
CEIL(ball_no / 6) AS over_number,
SUM(run) AS runs_in_over
FROM Cricket
GROUP BY CEIL(ball_no / 6);

# count no of overs
SELECT round(MAX(ball_no) / 6) AS overs_count
FROM Cricket;
# find the total cash amount and online amount
Example 12:
Fetch total revenue of each product.

Example 13:
# find highest salaried employ
# Find highest salary between 3 and 5
select * from
(select employee_id,first_name,last_name,salary,
dense_rank() over(order by salary desc) as rnk
from employees
)
where rnk between 3 and 5;

#Find department wise highest salary


Note –partition by clause is used is used to partition the data (group the data)
select * from
(select department_id,first_name,last_name,salary,
dense_rank() over(partition by department_id order by salary desc) as rnk
from employees
)
where rnk=1;
# find department wise minimum salary
select * from
(select department_id,first_name,last_name,salary,
dense_rank() over(partition by department_id order by salary) as rnk
from employees
)
where rnk=1;

# Find minimum salary along with all employee details (department wise)
select department_id,first_name,last_name,salary,
min(salary) over(partition by department_id) as min_sal_dpt
from employees;

# Find average salary along with employee details (department wise)


select department_id,first_name,last_name,salary,
avg(salary) over(partition by department_id) as avg_sal_dpt
from employees;

# Find maximum salary along with employee’s details (All)


select department_id,first_name,last_name,salary,
max(salary) over() as min_sal
from employees;

# Find total salary along with employee’s details (all)


select department_id,first_name,last_name,salary,
sum(salary) over() as min_sal
from employees;
# Find average salary of all employees
select department_id,first_name,last_name,salary,
avg(salary) over() as avg_sal
from employees;

# Find the employees who get salary more than the average salary of the all
employees.
select * from
(
select department_id,first_name,last_name,salary,
avg(salary) over() as avg_sal
from employees
)
where salary> avg_sal;
(or)
select * from employees
where salary >
(select avg(salary) from employees);

# Find the employees who get salary more than the minimum salary of the all
employees.
select * from
(
select department_id,first_name,last_name,salary,
min(salary) over() as min_sal
from employees
)
where salary> min_sal;
(or)
select * from employees
where salary >
(select min(salary) from employees);

#find the employees who are getting same salary.


select * from employees
where salary in
(select salary from employees
group by salary
having count(*)>1)
order by salary;
(or)
select * from
(select employee_id,first_name,last_name,salary,
count(*) over(partition by salary order by salary) as cnt
from employees )
where cnt>1;

# Find running total salary


>>select employee_id,first_name,last_name,salary,
sum(salary) over(order by salary) as run_sal
from employees;
# Find running count of employees;
>>select employee_id,first_name,last_name,salary,
count(employee_id) over(order by employee_id) as run_sal
from employees;

Examples on Join:
# Fetch employees who get highest salary in each department
select * from
(select d.department_id,d.department_name,e.first_name,e.last_name,e.salary,
dense_rank() over(partition by e.department_id order by e.salary desc) as rnk
from employees e
inner join departments d
on e.department_id=d.department_id)
where rnk=1;

(or) --> with CTE


with temp_tab as
(select department_id,first_name,last_name,salary,
dense_rank() over(partition by department_id order by salary desc) as rnk
from employees)
select d.department_id,d.department_name,t.* from temp_tab t
inner join departments d
on d.department_id=t.department_id
where rnk=1;

You might also like