*nth heighest salary*/
select distinct(empsalary) from employee order by empsalary desc limit N-1,1;
/*Find Duplicates in a table*/
select id
from practice
having count(*)>1;
SELECT t1.column_name
FROM table_name t1
JOIN table_name t2 ON t1.column_name = t2.column_name
WHERE t1.primary_key <> t2.primary_key;
SELECT column_name FROM table_name WHERE column_name IN (
SELECT column_name
FROM table_name
GROUP BY column_name
HAVING COUNT(*) > 1
);
/*and delete duplicate value*/
delete p1 from practice p1 inner join practice p2 where p1.id<p2.id and
p1.marks=p2.marks;
/*The statement uses a self-join to compare each row in the table with every
other row in the table*/
DELETE t1 FROM my_table t1, my_table t2 WHERE t1.id < t2.id AND t1.col =
t2.col;
delete t1 from employee t1 inner join employee t2 on t1.empname=t2.empname
where t1.empid>t2.empid;
/*Find avg salary in each department*/
select avg(marks),subject from practice
group by subject;
/*Write SQL Query to find the ids of all the students are enrolled in the same
subjects.*/
SELECT id
FROM practice
WHERE subject IN (SELECT id FROM practice WHERE id = practice.id)
GROUP BY id
HAVING COUNT(DISTINCT subject) > 1;
SELECT e1.student_id
FROM enrollment e1
JOIN enrollment e2 ON e1.subject_id = e2.subject_id
WHERE e1.student_id <> e2.student_id
GROUP BY e1.student_id
HAVING COUNT(DISTINCT e2.student_id) > 1;
/*SQL Query Used To Fetch Alternate Records From A Table?*/
SELECT * FROM (SELECT *,
Row_Number() OVER()
AS RowNumber FROM Employees) e
WHERE e.RowNumber % 2 = 0;
/**SQL Query to fetch names that ends with vowel**/
select empname from employees where left(empname,1 ) in ('a','e','i','o','u');
/*avg salary of employee in different departments in ascending order*/
select avg(empsalary),deptid from employee group by deptid order by deptid
asc;
/*You have given employee and dept table and you have to find dept which have
max avg salary ?**/
select d.deptid,d.deptname, avg(empsalary) as avgsalary from department d
join employee e on e.deptid=d.deptid
group by e.deptid
order by avgsalary desc
limit 1;