akhuwat-db-lab-07_more_sql_joins
akhuwat-db-lab-07_more_sql_joins
Spring 2023
Introduction to Lab
This lab introduces students to selecting data from multiple tables. A join is used to view
information from multiple tables. Hence, you can join tables together to view information from
more than one table. Various types and joins are explained and examined with the use of examples.
The main topics of this lab include:
1. Write SELECT statements to access data from more than one table using SQL:1999 joins
2. Join a table to itself by using a self-join
3. View data that does not meet the join condition by using outer joins
4. Generate a Cartesian product of all rows from two or more tables
In this example, the deptno columns in the emp and dept tables are joined using the ON clause.
Wherever a department ID in the emp table equals a department ID in the dept table, the row is
returned.
You can also use the ON clause to join columns that have different names.
The below example is a self-join of the emp table, based on the empno and mgr columns.
SELECT e.ename "Employee", m.ename "Manager" FROM emp e JOIN emp m ON
(e.mgr = m.empno);
The ON clause can also be used to join columns that have different names, within the same table or
in a different table.
A three-way join is a join of three tables. In SQL:1999–compliant syntax, joins are performed from
left to right. So, the first join to be performed is emp JOIN dept. The first join condition can
reference columns in emp and dept but cannot reference columns in salgrade. The second join
condition can reference columns from all three tables.
5. Non-Equijoins
The above example creates a non-equijoin to evaluate an employee’s salary grade. The salary must
be between any pair of the low and high salary ranges.
It is important to note that all employees appear exactly once when this query is executed. No
employee is repeated in the list. There are two reasons for this:
None of the rows in the job grade table contain grades that overlap. That is, the salary value
for an employee can lie only between the low salary and high salary values of one of the
rows in the salary grade table.
All of the employees’ salaries lie within the limits that are provided by the job grade table.
That is, no employee earns less than the lowest value contained in the losal column or
more than the highest value contained in the hisal column.
Note: Other conditions (such as <= and >=) can be used, but BETWEEN is the simplest. Remember
to specify the low value first and the high value last when using BETWEEN. Table aliases have been
specified in the slide example for performance reasons, not because of possible ambiguity.
7. Outer Joins
If a row does not satisfy a join condition, the row does not appear in the query result. For example,
in the equijoin condition of emp and dept tables, deptno 50 does not appear because there are
no employees with that deptno recorded in the emp table. Instead of seeing 15 employees in the
result set, you see 14 records.
To return the department record that does not have any employees, you can use an outer join.
Any unmatched rows are not displayed in the output. To return the unmatched rows, you can use
an outer join. An outer join returns all rows that satisfy the join condition and also returns some or
all of those rows from one table for which no rows from the other table satisfy the join condition.
There are three types of outer joins:
LEFT OUTER
RIGHT OUTER
FULL OUTER
A subquery is used to return data that will be used in the main query as a condition to further
restrict the data to be retrieved.
Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along with
the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
There are a few rules that subqueries must follow −
Subqueries must be enclosed within parentheses.
A subquery can have only one column in the SELECT clause, unless multiple columns are in
the main query for the subquery to compare its selected columns.
An ORDER BY command cannot be used in a subquery, although the main query can use an
ORDER BY. The GROUP BY command can be used to perform the same function as the
ORDER BY in a subquery.
Subqueries that return more than one row can only be used with multiple value operators
such as the IN operator.
A subquery cannot be immediately enclosed in a set function.
The BETWEEN operator cannot be used with a subquery. However, the BETWEEN operator
can be used within the subquery.
Example:
Write a query to display the name ( first name and last name ) for those employees who gets more
salary than the employee whose ID is 7499.
SELECT emp.ename FROM emp WHERE sal > (SELECT sal FROM emp WHERE
empno = 7499);
6. 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.
7. Modify query # 6 to display all employees including King, who has no manager. Order the
results by the employee number.
8. Create a query that displays employee name, department numbers, and all the employees who
work in the same department as a given employee. Label the columns Department, Employee,
Colleague.
9. Create a query that displays the name, job title for all employees
10. Create a query to display the name, department_name and hire date of all employees
11. Write a query to display the name ( first name and last name ), salary, department id, job id for
those employees who works in the same designation as the employee works whose id is 169
The End