SQL Queries
SQL Queries
Ques.2. Write a SQL query to fetch employee names having salary greater than or
equal to 5000 and less than or equal 10000.
ANS: select fullname from employeedetails where empId in(select empId from
EmployeeSalary where salary between 5000 and 10000)
Ques.4. Write a query to fetch only the first name(string before space) from the
FullName column of EmployeeDetails table.
ANS: select substring_index(fullname,' ',+1) from employeedetails //before space
select substring_index(fullname,' ',-1) from employeedetails //after space
Ques.5. Write a query to fetch employee names and salary records. Return employee
details even if the salary record is not present for the employee.
ANS: select d.fullname,s.salary from employeedetails d
left outer join EmployeeSalary s on d.empid = s.empid
Ques.6. Write a SQL query to fetch all the Employees who are also managers from
EmployeeDetails table.
ANS: select m.fullname from employeedetails e
inner join employeedetails m on e.managerId = m.empId
Ques.7. Write a SQL query to fetch all employee records from EmployeeDetails table
who have a salary record in EmployeeSalary table.
ANS: select * from employeedetails where empid in(select empid from EmployeeSalary
where salary is not null)
Ques.9. Write a SQL query to remove duplicates from a table without using temporary
table.
ANS: DELETE FROM EmployeeSalary
WHERE EmpId IN (
SELECT EmpId
FROM EmployeeSalary
GROUP BY Project, Salary
HAVING COUNT(*) > 1));
Ques.10. Write a SQL query to fetch only odd rows from table.
ANS:
Ques.11. Write a SQL query to fetch only even rows from table.
ANS:
Ques.12. Write a SQL query to create a new table with data and structure copied
from another table.
ANS: create table newTable like EmployeeSalary // for datastructure
insert into newTable (select * from EmployeeSalary ) // for data
Ques.13. Write a SQL query to create an empty table with same structure as some
other table.
ANS: create table newTable like EmployeeSalary // for datastructure
Ques.14. Write a SQL query to fetch common records between two tables.
ANS:
Ques.15. Write a SQL query to fetch records that are present in one table but not
in another table.
ANS:
Ques.17. Write a SQL query to fetch all the Employees details from EmployeeDetails
table who joined in Year 2016.
ANS: select * from employeedetails where dateofjoining like '2016%'
.
select * from employeedetails where dateofjoining between '2016-01-01' and
'2016-12-31'
Ques.19. Write SQL query to find the nth highest salary from table.
ANS: select * from EmployeeSalary order by salary desc limit n-1,1
Ques.20. Write SQL query to find the 3rd highest salary from table without using
TOP/limit keyword.
ANS: select * from EmployeeSalary order by salary desc limit 2,1