SQL Query to find second highest salary of Employee
SELECT * , max(salary) from employee where salary not in (select max(salary) from employee)
SELECT * , max(salary) from employee where salary < (select max(salary) from employee)
SELECT * FROM (SELECT * FROM employee ORDER BY salary DESC LIMIT 2) ORDER BY salary LIMIT 1
SQL Query to find third highest salary of Employee select * from ( select * from employee order by salary desc limit 3) order by salary limit 1
Write SQL query to find the nth highest salary from table SELECT Salary FROM Employee ORDER BY Salary DESC LIMIT N-1,1;
SQL Query to find Max Salary from each department. SELECT DeptID, MAX(Salary) FROM Employee GROUP BY DeptID
Question 3: Write SQL Query to display the current date. SELECT GetDate();
 Write an SQL Query to check whether date passed to Query is the 
date of given format or not SELECT ISDATE('1/08/13') AS "MM/DD/YY";
Add new column alter table employee add DOB DATETIME null
To find duplicate records SELECT * FROM dup a WHERE rowid != (SELECT MAX(rowid) FROM dup b WHERE a.id=b.id)
To find Unique records SELECT * FROM dup a WHERE rowid = (SELECT MAX(rowid) FROM dup b WHERE a.id=b.id)
To delete duplicate recods delete from dup a where rowid!= (select max(rowid) from dup b where a.id=b.id)
There is a table which contains two column employee and salary, you
need to find all the employees, whose salary are greater than
average salary i.e. list of above average employees select * from employee where salary > (select avg(salary) from employee)
How do you find all employees which are also manager? select distinct e.empname, m.empname from manager e, manager m where e.managername=m.empname
Between clause
SELECT * FROM employee
WHERE empid IN (SELECT empid FROM employee
WHERE salary BETWEEN 50000 AND 100000);
Write a SQL query to fetch department-wise count of employees
sorted by department's count in descending order select count(*) as cnt, deparment from employee group by deparment order by cnt desc
Write a query to fetch only the first name(string before space) from
the FullName column of EmployeeDetails table SELECT LEFT(FullName, CHARINDEX(' ',FullName) - 1) FROM EmployeeDetails;
Write a SQL query to fetch only odd rows from table.
SELECT E.EmpId, E.Project, E.Salary FROM (
SELECT *, Row_Number() OVER(ORDER BY EmpId) AS RowNumber FROM EmployeeSalary) E WHERE
E.RowNumber % 2 = 1
Write a SQL query to create a new table with data and structure
copied from another table SELECT * INTO newTable FROM EmployeeDetails;
Write a SQL query to create an empty table with same structure as
some other table SELECT * INTO newTable FROM EmployeeDetails WHERE 1 = 0;
query to fetch common records between two tables SELECT * FROM EmployeeSalary INTERSECT SELECT * FROM ManagerSalary
query to fetch records that are present in one table but not in another
table SELECT * FROM EmployeeSalary MINUS SELECT * FROM ManagerSalary

Sql queries

  • 1.
    SQL Query tofind second highest salary of Employee SELECT * , max(salary) from employee where salary not in (select max(salary) from employee) SELECT * , max(salary) from employee where salary < (select max(salary) from employee) SELECT * FROM (SELECT * FROM employee ORDER BY salary DESC LIMIT 2) ORDER BY salary LIMIT 1 SQL Query to find third highest salary of Employee select * from ( select * from employee order by salary desc limit 3) order by salary limit 1 Write SQL query to find the nth highest salary from table SELECT Salary FROM Employee ORDER BY Salary DESC LIMIT N-1,1; SQL Query to find Max Salary from each department. SELECT DeptID, MAX(Salary) FROM Employee GROUP BY DeptID Question 3: Write SQL Query to display the current date. SELECT GetDate();  Write an SQL Query to check whether date passed to Query is the  date of given format or not SELECT ISDATE('1/08/13') AS "MM/DD/YY"; Add new column alter table employee add DOB DATETIME null To find duplicate records SELECT * FROM dup a WHERE rowid != (SELECT MAX(rowid) FROM dup b WHERE a.id=b.id) To find Unique records SELECT * FROM dup a WHERE rowid = (SELECT MAX(rowid) FROM dup b WHERE a.id=b.id) To delete duplicate recods delete from dup a where rowid!= (select max(rowid) from dup b where a.id=b.id) There is a table which contains two column employee and salary, you need to find all the employees, whose salary are greater than average salary i.e. list of above average employees select * from employee where salary > (select avg(salary) from employee) How do you find all employees which are also manager? select distinct e.empname, m.empname from manager e, manager m where e.managername=m.empname Between clause SELECT * FROM employee WHERE empid IN (SELECT empid FROM employee WHERE salary BETWEEN 50000 AND 100000); Write a SQL query to fetch department-wise count of employees sorted by department's count in descending order select count(*) as cnt, deparment from employee group by deparment order by cnt desc Write a query to fetch only the first name(string before space) from the FullName column of EmployeeDetails table SELECT LEFT(FullName, CHARINDEX(' ',FullName) - 1) FROM EmployeeDetails; Write a SQL query to fetch only odd rows from table. SELECT E.EmpId, E.Project, E.Salary FROM ( SELECT *, Row_Number() OVER(ORDER BY EmpId) AS RowNumber FROM EmployeeSalary) E WHERE E.RowNumber % 2 = 1 Write a SQL query to create a new table with data and structure copied from another table SELECT * INTO newTable FROM EmployeeDetails; Write a SQL query to create an empty table with same structure as some other table SELECT * INTO newTable FROM EmployeeDetails WHERE 1 = 0; query to fetch common records between two tables SELECT * FROM EmployeeSalary INTERSECT SELECT * FROM ManagerSalary query to fetch records that are present in one table but not in another table SELECT * FROM EmployeeSalary MINUS SELECT * FROM ManagerSalary