0% found this document useful (0 votes)
633 views11 pages

Empm Deptm: Select From Select From

This document contains 59 SQL queries that perform various operations on employee and department tables such as selecting data, filtering on conditions, aggregating results, using functions, joins, and subqueries. The queries retrieve information like employee names and salaries, minimum and maximum salaries by job and department, counts of managers, and more. They demonstrate many fundamental SQL concepts.

Uploaded by

pratyushaPinky
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
633 views11 pages

Empm Deptm: Select From Select From

This document contains 59 SQL queries that perform various operations on employee and department tables such as selecting data, filtering on conditions, aggregating results, using functions, joins, and subqueries. The queries retrieve information like employee names and salaries, minimum and maximum salaries by job and department, counts of managers, and more. They demonstrate many fundamental SQL concepts.

Uploaded by

pratyushaPinky
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 11

--assignment

--1) Select all the Information Regarding the Employee


Table
select * from empm
select * from deptm

--2) List all the employees who have a sal between 1000
and 2000.
select ename from empm
where sal between 1000 and 2000

--3) Display all the different job types.

select distinct job


from empm

--4) List the details of the employees in departments 10


and 20 in alphabetical order.
select *
from empm
where deptno in (10,20)

--5) List names and jobs of all clerks in department 20.


select ename,job
from empm
where job='clerk' and deptno=20

--6) Display all employee names, which have TH and LL in


them.
select ename
from empm
where ename like '%TH%' or ename like '%LL%'

--7) List the following details for all employees who


have a manager.

select *
from empm
where mgr is not null
--8) Display name and total remuneration for all
employees.

select ename,sal+isnull(comm,0) "total salary"


from empm
select ename,hiredate from empm
--9) Display all employees who were hired during 1993.
select ename
from empm
where year(hiredate)='1993'-- between 1980/01/01 and
1980/12/31

--10) Display name, annual sal and commission of all


salespeople whose monthly salary is greater than
-- their commission. The output should be order by
salary,
-- highest first. If two employees have the same
sal sort by employee names, within the highest salary
order.
select e.ename,e.sal*12 "annual sal",e.comm
from empm e
inner join deptm d on e.deptno=d.deptno
where dname='sales' and sal>comm
order by sal desc,ename

--11) List the employees name and salary incremented


by 15% and express as whole number of Rupees.

select ename,round(sal+sal*(15/100),5) "new salary"


from empm

--12) Display each employees name and hiredate from


dept 20. Make sure that you specify
--the alias ‘Date Hired’ after your expression
otherwise the formatted column will wrap;
--it uses a width of 80 characters which is the default
for character columns.

select ename,hiredate "Date Hired"


from empm
where deptno=20

--13) Display each employee name with hiredate, and


salary review data. Assume review date
--is one year after hire date. Order the Output in
Ascending.

select ename,hiredate "date hired",dateadd(yy,


+1,hiredate) "Review Date"
from empm

--14) Print a list of employees displaying ‘just salary


if more than 1500’.
--If exactly 1500 display ‘On Target’, if less than
1500 display
--‘Below Target’

select ename,empno,
case when sal>1500 then 'just salary'
when sal=1500 then 'on target'
when sal<1500 then 'below target'
end
as "position"
from empm

--15)Write a query that will return the Day of the week


(i.e. Monday) for any date entered in the format DD:
MM.YY.

select datename(dw,hiredate)
from empm

--16) Write a query to calculate the length of time


any employee has been with the company.

--17) Find the minimum sal of the Employees.

select min(sal)
from empm

--18) Find the minimum, maximum and avg sal of all


employees.

select min(sal) "minimum sal",max(sal) "max


sal",avg(sal) "avg sal"
from empm

--19) List the minimum and maximum sal for each job
type.

select job, min(sal) "min sal",max(sal) "max sal"


from empm
group by job

--20) Find out how many managers there are without


listing them.

select count(mgr) "total managers"


from empm
--21) Find the avg sal and avg total remuneration for
each job type.

select job,avg(sal) "avg sal",avg(sal+isnull(comm,0))


"avg total remuneration"
from empm
group by job

--22) Find out the diff between highest and lowest


sal.
select max(sal)-min(sal) "difference"
from empm

--23) Find all dept, which have more than 3


employees.

select d.dname
from deptm d
inner join empm e
on d.deptno=e.empno

--24) Check whether all employee numbers are indeed


unique.

select isnumeric(empno)
from empm

--25) List lowest paid employees working for each


manager, exclude any group
--Where the minimum sal is less than 1000. Sort the
output by sal.

select min(sal)
from empm
except select min(sal) from empm
where sal<200
group by mgr

--26) Display all EMP names and their dept names in


dept name order.

select e.ename,d.dname
from empm e,deptm d
order by d.dname

--27) Display all EMP names, dept number and name.

select e.ename,d.dname,d.deptno
from empm e,deptm d

--28) Display the name, location and dept of


employees whose sal is more than 1500 a month.

select e.ename,d.loc,d.deptno
from empm e,deptm d
where e.sal>1500

--29) Produce a list showing employees’ sal grades.


select e.ename,g.grade
from empm e,salgradem g

--30) Show only employees on Grade 3.


select e.ename,e.sal
from empm e,salgradem g
where g.grade=3
select * from salgradem
--31) Show all employees in Dallas.

select e.ename "employees working in dallas"


from empm e,deptm d
where d.loc='dallas'

--32) List the Employee Name, job, sal, grade and


dept name for everyone in the company except clerks.
-- Sort on salary, displaying the highest sal first.

select e.ename,e.job,e.sal,g.grade,d.dname
from empm e,deptm d,salgradem g
--except select e.ename,e.job,e.sal,g.grade,d.dname
-- from empm e,deptm d,salgradem g
-- where e.job='clerk'
where e.job not in ('clerk')
order by e.sal desc

--33) List the following details of EMP whose earn


36000 a year or who are clerks.

select ename,empno
from empm
where sal*12>36000 or job='clerk'

--34) Display the dept that has no employees.

select distinct(d.dname)
from deptm d left outer join empm e
on e.deptno=d.deptno
--35) List all EMP by name and number along with
their manager’s name and number.

select e.ename,e.empno,e.mgr,m.ename "manager name"


from empm e,empm m
where e.mgr=m.empno

--36) Modify solution to above question to display


KING who has no manager.

select e.ename
from emp e
where e.mgr is null

--37) Find the job that was filled in the first half
of 1983, and the
--same job that was filled during the same period in
1984.

--38) Find all employees who joined the company


before their manager.
select e.ename
from empm e,empm m
where year(e.hiredate)>year(m.hiredate)

--39) Find another query method for about question.

--40) Find the Employees who earn the highest sal in


each job type Sort in Desc Sal order.

select ename,job,max(sal) "maximum salary"


from empm
group by ename,job
order by max(sal) desc
--41) Find the Employees who earn the minimum sal for
their job. Display the result in ascending order of
salary.

select ename,job,min(sal) "minimum salary"


from empm
group by ename,job
order by min(sal) desc

--42) Find the most recently hired employees in each


dept. Order by hiredates.

select deptno,min(hiredate)
from empm
group by deptno

--43) Show the following details for any EMP who


earns a sal greater than the avg for their
--dept. sort in deptno order.
select ename,deptno
from empm
where sal in (select avg(sal) from empm group by
deptno)
order by deptno

--44) List all the dept where there are no employees


using subquery only.

--45) Display the following information for the dept


with the HIGHEST annual remuneration bill.

select ename,sal
from empm
where sal in (select max(sal) from empm group by
deptno)

--46) Who are the Top 3 Earners of the Company?


select top 3 sal ,ename from empm

--47) In which year did most People join the Company?


Display the year and Number of employees.

--48) Modify above question to also display the avg


sal figure for the dept.

--49) Write a Query to display an ‘*’ against the row


of the most recently hired employee.
--Display ENAME, HIREDATE and column.
SELECT CONVERT(VARCHAR(10),ENAME)+'*' AS "MODIFIED
NAME" ,HIREDATE FROM EMPm WHERE
DATEDIFF(DD,HIREDATE,GETDATE()) <=(SELECT
MIN(DATEDIFF(DD,HIREDATE,GETDATE()))FROM EMPm )

--50) Replace Salesman with Salesperson in the


Employee Table.
alter empm replace(

--51) Display the Number of Months between getdate( )


and Hiredate of the EMP Table.

select datediff(mm,hiredate,getdate()) "number of


months"
from empm

--52) Add 2 Months for the existing Sysdate and


Display the Result of the Employee Table
--and also display the Next Day of the Hiredate.
select dateadd(mm,+2,getdate()) "result",dateadd(dd,
+1,hiredate) "1st day"
from empm

--53) Display the Last day of the given format dd-mm-


yyyy.
--54) Display all the Employees who earn less than
their managers.
select e.ename
from empm e
where sal>(select sal from empm m where m.empno=e.mgr)

--55) Display the Lowest Sal of Each Department using


subquery.

select deptno,sal
from empm
where sal in (select min(sal) from empm group by
deptno)

--56) Display the Employees who earn more than the


lowest sal in Dept 30

select ename,sal
from empm
where sal>(select min(sal) from empm where deptno=30)

--57) Display the Employees who earn more than every


employee in the Department 30.
select ename,sal
from empm
where sal>(select max(sal) from empm where deptno=30)

--58) Display the name, job and hiredate for


employees whose sal is greater than the
-- highest sal in any sales department.

select ename,job,hiredate
from empm where sal>(select max(e.sal) from empm
e,deptm d where d.dname='sales')

--59) Display all the Employees whose department is


not in Department table.

select e.ename,d.dname
from empm e,deptm d
where d.dname not in (select dname from deptm)

You might also like