SQL Assignment 5
1. Write a query for the HR department to produce the addresses of all the departments.
Use the LOCATIONS and COUNTRIES tables. Show the location ID, street
address, city, state or province, and country in the output. Use a NATURAL JOIN to
produce the results.
Select e.location_id,e.street_address,e.city,e.state_province “state or
province”,d.country_name “country” from locations e,countries d where
e.country_id=d.country_id;
Select location_id,street_address,city,state_province "state or
province",country_name “country” from locations natural join countries;
2. When Cartesian product is formed in Oracle Join and how to avoid it?
Cartesian product or cross join is formed when join condition is omitted or join
condition is invalid .In order to avoid it, we need to include a valid join condition;
3. When Cartesian products are useful in Oracle?
To test the performance of DBMS, we need to generate a large number of rows to
form big amount of data;
4. The HR department needs a report of all employees. Write a query to display the last
name, department number, and department name for all employees.
select e.last_name,e.department_id “department number”,d.department_name
“department name” from employees e,departments d where
e.department_id=d.department_id;
5. The HR department needs a report of employees in Toronto. Display the last name,
job, department number, and department name for all employees who work in
Toronto.
select e.last_name,e.department_id,d.department_name from employees
e,departments d,locations l where e.department_id = d.department_id and
d.location_id = l.location_id and l.city='TORONTO';
6. Additional restrictions can be implemented by using a WHERE clause on a
NATURAL join (TRUE/FALSE).
TRUE
7. Create a report to display the last name and employee number of employees along
with their manager’s last name and manager number. Label the columns Employee,
Emp#, Manager, and Mgr#, respectively.
select e.last_name "Employee",e.employee_id "Emp#",m.last_name
"Manager",m.employee_id "Mgr#" f rom employees e,employees m where e.
manager_id =m. employee_id;
8. What is the need to qualify the names of the columns with the table name in joining?
We needed to qualify the names of columns with table names in order to avoid
ambiguity.
Ex. without table prefixes locaton_id can be from locations or countries creating
ambiguity.
9. What is non-equi join in Oracle? Explain with appropriate example.
A nonequijoin in Oracle is something that containing operator other than equality
operator (=).
Ex.Salary coloumn in employees table must be between lowest_salary and
highest_salary coloumns in job_grades table,here operator used is BETWEEN.
10.Create a report for the HR department that displays employee last names, department
numbers, and all the employees who work in the same department as a given employee.
Give each column an appropriate label.
SELECT e.department_id department, e.last_name employee, c.last_name colleague
FROM employees e JOIN employees c
ON (e.department_id = c.department_id)
WHERE e.employee_id <> c.employee_id
ORDER BY e.department_id, e.last_name, c.last_name;
11. Differentiate between LEFT OUTER JOIN and RIGHT OUTER JOIN.
LEFT OUTER JOIN returns matched rows from joined tables and unmatched rows
from the left table where as RIGHT OUTER JOIN returns matched rows from joined
tables and unmatched rows from right table;
12. What can be the maximum length of a table alias in table joining?
30 Characters.
13. The NATURAL JOIN and USING clauses are mutually inclusive (TRUE/FALSE).
False.
14. What is FUL OUTER JOIN in Oracle?
FUL OUTER JOIN returns matched rows from both the joined tables and unmatched
rows from either table. It combines the results from both LEFT OUTER JOIN and
RIGHT OUTER JOIN.
15. The HR department needs to find the names and hire dates for all employees who were
hired before their managers, along with their managers’ names and hire dates.
select e.first_name ||' '||e.last_name "name",e.hire_date, m.first_name ||' '||m.last_name
"mname",m.hire_date from employees e,employees m where
(e.employee_id=m.manager_id) and (e.hire_date<m.hire _date);