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

TSQL

This document contains 20 SQL queries that: 1. Retrieve employee names, hire dates, and department numbers for employees in department 20. 2. Calculate the number of months the president has worked for the company. 3. Select employee names and hire dates for employees hired in December. 4. Find the average annual salary for each job in each department. 5. Count the number of employees in department 30.

Uploaded by

Nidhi Patankar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views

TSQL

This document contains 20 SQL queries that: 1. Retrieve employee names, hire dates, and department numbers for employees in department 20. 2. Calculate the number of months the president has worked for the company. 3. Select employee names and hire dates for employees hired in December. 4. Find the average annual salary for each job in each department. 5. Count the number of employees in department 30.

Uploaded by

Nidhi Patankar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

TSQL

1. List the names and hire dates in format ('20/3/84') in dept 20.

->select ename, date_format(hiredate,"%d %M %Y"),deptno from employee where


deptNo=20;

2. How many months has the president worked for the company

->select ename, datediff(curdate(), hiredate) from employee where


job='President';

3. List the names of all employees where hire date is in month of December

->select ename, hiredate from employee where month(hiredate)=12;

4. Give SQL command to find average annual salary per job in each department

-> select avg(sal), job from employee group by job;

5. Count number of employees in dept 30:

-> select deptNo, count(ename) from employee where deptno=30;

6. Compute average, minimum and maximum salaries of employees for each department:

-> select ename, deptno, avg(sal),min(sal),max(sal) from employee group by


ename,deptno;

7. Display deptno's where more than two clerks are working:

-> select deptNo, count(empno) from employee group by deptNo having


count(empNo)>3 where empNo=(select empNo from employee where job="Clerk");

8. Who was the first employee hired in each department

-> SELECT ename, job, hiredate FROM employee e WHERE hiredate IN(SELECT
min(hiredate) FROM employee WHERE e.deptno =deptno );

9. Show count of employees and their total sum of salary. Display only those jobs
where minimum salary is greater than or equal to 3000

->select count(ename),sum(sal), job from employee group by job having


min(sal)>=3000;

10. Find out difference between highest and lowest salaries from emp table

->select max(sal)-min(sal)Difference as from employee;

11. Find all departments which have more than 3 employees.

->select job, deptno, count(deptno) as "no of employee per dept" from


employee group by job having count(deptno)>=3;

12. Find the employees who earn more than the lowest salary in each department.

->select ename, sal from employee where sal >ANY (select sal from employee e,
department d where e.deptno=d.deptno);
13. Display employee who earn more than the lowest salary in department 30

->select ename, sal from employee where sal >ANY (select sal from employee
where deptno=30);

14. Find employees who earn more than every employee in each department ordering to
the department.

->select ename e, dname d, max(sal) from employee e, department d where


e.deptno=d.deptno;

15. Determine average salary of employees.

->select ename, avg(sal) from employee group by ename;

16. Find the job with highest average salary

->select job, sal from employee where sal > ALL (SELECT avg(sal) from
employee);

17. Display the name of job, hire date for employees whose salary is greater than
the highest salary in any SALES department

->select job, hiredate, ename, sal from employee where sal > ALL (select sal
from employee where deptno=30);
alternate method:

->select job, hiredate, ename, sal from employee where sal > ALL (select sal
from employee where deptno=(select deptno from department where dname="SALES"));

18. Find out all the jobs either in department 20 or where salary is greater than
3000.

-> select job, deptno,sal from employee where deptno=20 OR sal>=3000;

19. Find out the employees who earn the highest salary in each department.

->select ename, max(sal) as 'highest salary', dname from employee e,


department d where e.deptno=d.deptno;

20. Display the employees who are doing the same job as �FORD�.

->select ename, job from employee where job=(select job from employee where
ename="FORD)

You might also like