Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Exercise 2: Selecting Data from more than one Table

The following abstract summary represents the notation used for selecting data from
more than one table:
SELECT table1.column, table2.column, ...
FROM table1, table2, ...
WHERE table1.columnX = table2.columnY

Exercises:
1. Write a query to display the name, department number, and the department name
for all employees.
ANSWER:
SELECT dname,deptno
FROM DEPT;

2. Create a unique listing of all jobs with their respective locations that are
in department30.
ANSWER:
SELECT loc,job FROM emp, dept
WHERE dept.deptno LIKE '30';

3. Write a query to display the employee name, department name, and location of
all employees who earn a commission.
ANSWER:
SELECT ename,dname,loc,comm
FROM emp, dept;

4. Write a query to display the name, job, department number, and department name
for all employees who work in Dallas.
ANSWER:
SELECT ename, job, deptno, depid
FROM emp
WHERE city = 'Dallas';

5. Display the employee name and employee number along with their manager’s name and
manager number. Label the columns Employee, Emp#, Manager, and Mgr#, respectively.
ANSWER:
SELECT e.ename AS Employee, e.empno AS "Emp#", m.ename AS Manager,
m.empno AS "Mgr#"
FROM emp e
LEFT JOIN emp m ON e.mgr = m.empno:
6. Modify your query from 5 to display all employees including also those who do not
have a manager.
ANSWER:

SELECT e.ename AS Employee, e.empno AS "Emp#", m.ename AS Manager, m.empno AS


"Mgr#" job
FROM emp
LEFT JOINT emp m ON e.mgr = m.empno OR (e.mgr IS NULL AND m.empno IS NULL);

7. Create a query that will display the name, job, department name, salary, and grade for
all employees.

ANSWER:
SELECT ename, job, dname, esal,
sgrade
FROM emp, dept, salgrade
WHERE emp.deptno = dept.deptno;

You might also like