You are on page 1of 3

Lab – 07

Sql Joins

Lab Tasks:

1. Write a query to combine all rows of table EMP and table DEPT. Sort the columns
in ascending order by employee names.
SELECT *
FROM EMP
JOIN DEPT ON EMP.ID = DEPT.ID
ORDER BY EMP.NAME ASC;

2. Write a query to display the name, department number, and department name for all
employees.
SELECT Employee.LastName, Employee.DeptID,
Department.DeptName
FROM Employee, Department
WHERE Employee.DeptID = Department .DeptID;

3. Write a query for table EMP and DEPT to retrieve enames , sal, job, deptno, loc
where length of job is greater than length of loc.
SELECT EMP.ename, EMP.sal, EMP.job, DEPT.deptno,
DEPT.loc
FROM EMP
JOIN DEPT ON EMP.deptno = DEPT.deptno
WHERE LENGTH(EMP.job) > LENGTH(DEPT.loc);

4. Write a query to retrieve ename, length of enames, loc , deptno and sal by join table
EMP with table DEPT where deptno is 20 and sal is greater than 10000. Sort the sal
in descending order
SELECT EMP.ename, LENGTH(EMP.ename), DEPT.loc,
DEPT.deptno, EMP.sal
FROM EMP
JOIN DEPT
ON EMP.deptno = DEPT.deptno
WHERE DEPT.deptno = 20 AND EMP.sal > 10000
ORDER BY EMP.sal DESC;

5. Write a query to join table EMP with itself where mgr exists for any employee.
SELECT *
FROM EMP AS e1
JOIN EMP AS e2 ON e1.mgr = e2.empno
WHERE e1.mgr IS NOT NULL;

6. Create a query that displays ename, deptno of those employees who work in the
same department as a given employee. Give each column an appropriate Label.
SELECT Employee.Name AS Employee_Name, Employee.DeptID
AS Department_ID
FROM Employee JOIN Department
WHERE Employee.DeptID = Department .DeptID;

7. Write a query to display the last name, department number, and department name
for all employees.
SELECT Employee.LastName, Employee.DeptID,
Department.DeptName
FROM Employee, Department
WHERE Employee.EmpID = Department.DeptID

8. Write a query to display the employee last name, department name, location ID, and
city of all employees who earn a commission.
SELECT Employee.LastName, Department.DeptName,
Department.DeptID, Location.locCity
FROM Employee, Department, Location
WHERE
Employee.DeptID = Department.DeptID
AND
Department.locID = Location.locID
AND
Employee.commission IS NOT NULL;

9. Display the employee last name and department name for all employees who have
an a (lowercase) in their last names.
SELECT LastName, DeptName
FROM Employee, Department
WHERE Employee.DeptID = Departments.DeptID
AND
LastName LIKE '%a%';

10. Write a query to display the last name, job, department number, and department
name for all employees who work in Toronto.
SELECT Employee.LastName, Employee.JobID,
Employee.DeptID, Department.DeptName
FROM Emploee JOIN Department
ON
(Employee.DeptID = Department.DeptID) JOIN Location
ON
(Department.DeptID = Location.locID)
WHERE Location.locCity = 'Toronto';

You might also like