1. The document describes SQL queries to create tables, insert data, join tables, and perform various operations on employee data like finding duplicate records, minimum/maximum salaries, and employees by department.
2. Examples are provided to find employees without a valid department, get manager names, find duplicate salaries, and retrieve random records.
3. Various date functions are demonstrated like calculating age from DOB and getting the last day of the previous month.
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
97 views
SQLnotes Interview
1. The document describes SQL queries to create tables, insert data, join tables, and perform various operations on employee data like finding duplicate records, minimum/maximum salaries, and employees by department.
2. Examples are provided to find employees without a valid department, get manager names, find duplicate salaries, and retrieve random records.
3. Various date functions are demonstrated like calculating age from DOB and getting the last day of the previous month.
deptid int, deptname varchar(255) ); 4)insert into department (deptid,deptname) values ( 1 , 'IT'), (2 , 'Admin') ; 5)Can you get employee details whose department id is not valid or department id not present in department table? using left join SELECT E.EMPID,E.EMPNAME, E.DEPTID FROM EMPLOYEE E left outer join DEPARTMENT d on E.DEPTID = D.DEPTID WHERE D.DEPTID IS NULL using not in SELECT E.EMPID,E.EMPNAME, E.DEPTID FROM EMPLOYEE E where e.deptid not in (select deptid from department) using not exits SELECT E.EMPID,E.EMPNAME, E.DEPTID FROM EMPLOYEE E where NOT EXISTS (select deptid from department where e.deptid=department.deptid) 6) Employee and Manager ID are in the same table; can you get manager names for employees? SELECT e1.empid as EmployeeId, e1.empname as EmployeeName, e1.ManagerId , e2.empname AS ManagerName FROM employee e1, employee e2 where e1.ManagerId = e2.empid
left oute rjoin
select E1.empid [Emp_id],E1.empname [Emp_name], E2.managerid [Mgr_id],E2.empname [Mgr_name] from [employee] E1 left join [employee] E2 on E1.managerid=E2.empid 7)Can you get the list of employees with same salary?
select distinct e.empid ,e.empname,e.salary from employee e where salary in
(select salary from employee group by salary having count(*)>1) or Select distinct e.empid,e.empname,e.salary from employee e, employee e1 where e.salary =e1.salary and e.empid != e1.empid 8)How can you find duplicate records in Employee table? SELECT EMPID,EMPNAME, SALARY, COUNT(*) AS CNT FROM EMPLOYEE GROUP BY EMPID,EMPNAME, SALARY HAVING COUNT(*)>1
9)How can you DELETE DUPLICATE RECORDS?
DELETE FROM EMPLOYEE WHERE EMPID IN (
SELECT EMPID FROM EMPLOYEE GROUP BY EMPID,EMPNAME, SALARY HAVING COUNT(*)>1 ) 10) Find the second highest salary. Select max(Salary) from employee where Salary not in (Select max(Salary) from employee)
11)can you find 3rd, 5th or 6th i.e. N'th highest Salary? SELECT * FROM EMPLOYEE E WHERE 2 = (SELECT COUNT(DISTINCT E1.SALARY) FROM EMPLOYEE E1 WHERE E1.SALARY>E.SALARY) 2=3-1 i.e N-1
How to find two minimum salaries from the employees?
SELECT DISTINCT Salary FROM Employee E1 WHERE 2 >= (SELECT COUNT(DISTINCT Salary)FROM Employee E2 WHERE E1.Salary >= E2.Salary) ORDER BY E1.Salary DESC How to find two maximum salaries from the employees? SELECT DISTINCT Salary FROM Employee E1 WHERE 2 >= (SELECT COUNT(DISTINCT Salary)FROM Employee E2 WHERE E1.Salary <= E2.Salary) ORDER BY E1.Salary DESC How to find two maximum salaries from the employees? SELECT DISTINCT Salary FROM Employee E1 WHERE 2 >= (SELECT COUNT(DISTINCT Salary)FROM Employee E2 WHERE E1.Salary <= E2.Salary) ORDER BY E1.Salary DESC How to get minimum and maximum from the employees? SELECT MIN(Salary) FROM Employee UNION SELECT MAX(Salary) FROM Employee 12) Can you write a query to find employees with age greater than 30?
select * from employee
where datediff(year,dob, getdate()) >30
--SELECT DATEDIFF("2017-01-01", "2016-12-24");--
13)Please write a query to get the maximum salary from each department. select DeptId, max(salary) as Salary from employee group by deptid 14)Can you show one row twice in results from a table? select d.deptname from department d,department d1 where d.deptname='IT' also we can use union all 14)Can you write a query to get employee names starting with a vowel? Select empid, empname from employee where empname like '[aeiou]%' 15) Can you write a query to get employee names ending with a vowel? Select empid, empname from employee where empname like '%[aeiou]' 16)Can you write a query to get employee names starting and ending with a vowel? select empid, empname from employee where empname like '[aeiou]%[aeiou]' 17)Write a query to get employees whos ID is even. select * from employee where empid %2 =0 18)odd select * from employee where empid %2 !=0 19)How can you get random employee record from the table? select top 1 * from employee order by newid() 20)Give data in a table is of format 'NN/NN', verify that the first and last two characters are numbers and that the middle character is '/'. SELECT DataCol, 'CHECK' = CASE WHEN datacol like '%[0-9]%[^A-Z]%/%[^A-Z]%[0-9]%' then 'NUMBER' else 'NOT NUM' end from TestOne ------------------- How to find duplicate FName along with their count from Employee? SELECT FName, COUNT(Id) AS CNT FROM Employee GROUP BY FName HAVING COUNT(Id)>1 How to find duplicate Salary along with their count from Employees? SELECT Salary, COUNT(Id) AS CNT FROM Employee GROUP BY Salary HAVING COUNT(Id)>1 How to get common records from two different tables which have not any joining conditions? SELECT FName, LName, Address, DOB FROM Employee1 INTERSECT SELECT FName, LName, Address, DOB FROM Employee2 How to display 4 to 7 records? SELECT * FROM ( SELECT ROW_NUMBER() OVER( ORDER BY Id) AS No, FName, Lname FROM Employee ) as tmp WHERE tmp.No between 4 and 7
Alternative for TOP clause
SET ROWCOUNT 3 SELECT * FROM Employee ORDER BY Id ASC SET ROWCOUNT 0
SELECT * FROM Customers
WHERE ROWNUM <= 3;
SELECT * FROM Customers
LIMIT 3;
How to find employees with an age greater than 40 years?
SELECT * FROM Employee WHERE datediff(year,DOB, getdate()) >40 How to find the last employee record? SELECT * FROM Employee WHERE Id= (SELECT max(Id) FROM Employee) How to get the first and last records from employees? SELECT * FROM Employee WHERE Id = (SELECT MIN(Id) FROM Employee) UNION SELECT * FROM Employee WHERE Id = (SELECT MAX(Id) FROM Employee) How to find the maximum salary from each department? SELECT DeptId, MAX(Salary) as Salary FROM Employee GROUP BY DeptId How to get the total number of employees working in the 'HR' department? SELECT COUNT(*) FROM Employee E INNER JOIN Department D ON D.Id = E.DeptId WHERE D.Name = 'HR' How to get a department-wise count of employees? SELECT D.Name, COUNT(*) AS Employees FROM Employee E INNER JOIN Department D ON D.Id = E.DeptId GROUP BY D.Name How to create a new table which consists of only structure without copy data from the other table? SELECT * INTO Employee1 FROM Employee WHERE 1=2 How to find employees with the same salary? SELECT DISTINCT e.Id,e.FName, e.LName, e.Salary FROM Employee e, Employee e1 WHERE e.salary = e1.salary and e.Id != e1.Id How to get employees working in the same department? SELECT DISTINCT e.Id,e.FName, e.LName, e.DeptId FROM Employee e, Employee e1 WHERE e.DeptId = e1.DeptId and e.Id != e1.Id How to get 50% records from the employee? SELECT * FROM Employee WHERE Id <= (SELECT COUNT(Id)/2 from Employee) How to find employees who hold managerial positions?
SELECT DISTINCT e.Id, e.FName, e.LName
FROM Employee e INNER JOIN Employee m ON m.ManagerId = e.Id
How to get the first four characters of FName from employees?
SELECT SUBSTRING(FName, 1, 4) FROM Employee how to find employees whose name starts with an alphabet ‘A’ and contains six alphabets? SELECT * FROM Employee WHERE FName LIKE 'A_____'
How to find salaries paid for each department?
SELECT DeptId, SUM(Salary) FROM Employee GROUP BY DeptId