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

SQL exercise

The document contains a series of SQL queries related to employee and department data, including selection, filtering, and joining operations. It highlights various SQL functions and conditions, such as filtering by salary, job title, and department location. Additionally, it addresses potential syntax issues and improvements in the queries, such as the use of double quotes and the upper() function for case-insensitivity.

Uploaded by

chaotic8023
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)
4 views

SQL exercise

The document contains a series of SQL queries related to employee and department data, including selection, filtering, and joining operations. It highlights various SQL functions and conditions, such as filtering by salary, job title, and department location. Additionally, it addresses potential syntax issues and improvements in the queries, such as the use of double quotes and the upper() function for case-insensitivity.

Uploaded by

chaotic8023
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/ 3

-- Part A

1.
select *
from employee
where empmsal < 1000;

2. ???
select deptno
from department
where empno is not null;

---
select distinct deptno
from employee
order by deptno;

3.
select *
from employee
where upper(empjob) = 'TRAINER' and empmsal < 2500 and deptno = 20;

4. ??? (Double quotes needed? --- yes)


select empname as "Name", empjob as "Job", empmsal as "Monthly Salary", empcomm as
"Commission"
from employee
where empmsal > empcomm;

5. ??? (upper() needed?)


select empname, empjob
from employee
where upper(empjob) like '%R';

6.
select empname, empjob
from employee
where upper(empname) like 'J%'
or upper(empname) like 'K%'
or upper(empname) like 'M%';

7.
select empname, empjob, empbdate, empmsal
from employee
where empbdate < TO_DATE('01-JAN-1990','dd-MON-YYYY') and empmsal > 1500;

8.
select empname, empjob, empbdate, empmsal
from employee
where empcomm is null;

9.
select empname, empjob, deptname, deptlocation, empmsal
from employee e join department d on e.deptno = d.deptno
where upper(deptlocation) = 'NEW YORK';

10.
select empname, empjob
from employee e join department d on e.deptno = d.deptno
where upper(deptlocation) != 'NEW YORK' and deptlocation != 'CHICAGO';
---
or we can use: NOT IN ('NEW YORK', 'CHICAGO')

11.
select empname, empjob, empbdate, empmsal
from employee
where empbdate between to_date('1970', 'yyyy') and to_date('1975', 'yyyy')
order by empbdate;

---
date should be exactly!!!
empbdate BETWEEN TO_DATE('01-JAN-1970','DD-MON-YYYY') AND TO_DATE('31-DEC-
1974','DD-MON-YYYY')

12.
select empname, empjob, empmsal
from employee
where empmsal < 1500 or empmsal > 3000;

---
or we can use: empmsal NOT BETWEEN 1500 and 3000

13.
select empname, empjob, mgrno
from employee
where mgrno is not null;

14. ???
select empname, empjob, deptname, deptlocation, empmsal
from employee e join department d on e.deptno = d.deptno
where (upper(deptlocation) = 'DALLAS' or
upper(empjob) = 'MANAGER'
) and empmsal > 2500;

15. ??? (salary grade?)


select empname, empjob, empmsal
from
order by

---
SELECT
e.empname,
e.empjob,
e.empmsal,
s.salgrade
FROM
payroll.employee e,
payroll.salgrade s
WHERE
e.empmsal BETWEEN s.sallower AND s.salupper
ORDER BY
s.salgrade,
e.empmsal;

------ OR

SELECT
e.empname,
e.empjob,
e.empmsal,
s.salgrade
FROM
payroll.employee e
JOIN payroll.salgrade s ON (
e.empmsal BETWEEN s.sallower AND s.salupper
)
ORDER BY
s.salgrade,
e.empmsal;

16. --- if all, then use left/right outer join!!!


select deptname, empname
from department d right outer join employee e on d.depno = e.deptno
orderby deptname, empname;

17. ???
select e1.empname, e1.empjob, e2.empname
from employee e1 left outer join employee e2 on e1.mgrno = e2.empno
order by e2.empname, e1.empname;

18.
select empname, deptname, to_char(histbegindate, 'dd-Mon-yyyy'),
to_char(histenddate, 'dd-Mon-yyyy'), empmsal
from employee e join department d on e.deptno = d.deptno join history h on e.empno
= h.empno
order by empname, histbegindate DESC;

19. ???
selct empname, empjob, empmsal, 12*empmsal
from employee;

20.
selct empname, empjob, empmsal, empcomm, 12 * (empmsal + empcomm)
from employee;

---
SELECT
empname,
empjob,
empmsal,
empcomm,
12 * (empmsal + nvl(empcomm,0)) AS "Annual Income"
FROM
payroll.employee
ORDER BY empname;

You might also like