0% found this document useful (0 votes)
203 views

SQL Queries

This document contains 20 questions and answers related to writing SQL queries. The questions cover a range of SQL topics including selecting records that meet certain criteria, aggregating data, joining tables, sorting data, limiting results, and more. The provided answers demonstrate how to write the corresponding SQL queries to satisfy each question's requirements.

Uploaded by

rupali kokate
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
203 views

SQL Queries

This document contains 20 questions and answers related to writing SQL queries. The questions cover a range of SQL topics including selecting records that meet certain criteria, aggregating data, joining tables, sorting data, limiting results, and more. The provided answers demonstrate how to write the corresponding SQL queries to satisfy each question's requirements.

Uploaded by

rupali kokate
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Ques.1. Write a SQL query to fetch the count of employees working in project 'P1'.

ANS: select count(*) from EmployeeSalary where project='p1'

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)

select d.fullname from employeedetails d left outer join


EmployeeSalary s on d.empid = s.empid where s.salary between 5000 and 10000

Ques.3. Write a SQL query to fetch project-wise count of employees sorted by


project's count in descending order.
ANS: select project,count(project) as count from EmployeeSalary group by project
desc order by count

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.8. Write a SQL query to fetch duplicate records from a table.


ANS: select * from EmployeeSalary group by project,empid,salary having count(*)>1

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.16. Write a SQL query to find current date-time.


ANS: newTable

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.18. Write a SQL query to fetch top n records?


ANS: select * from EmployeeSalary order by salary desc limit n

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

You might also like