subqueries - lecture 11 subsections 5.1.1 -...

Post on 12-Jul-2020

0 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

SubqueriesLecture 11

Subsections 5.1.1 - 5.1.5

Robb T. Koether

Hampden-Sydney College

Fri, Feb 7, 2014

Robb T. Koether (Hampden-Sydney College) Subqueries Fri, Feb 7, 2014 1 / 20

1 Subqueries

2 The IN Operator

3 The ALL Operator

4 Assignment

Robb T. Koether (Hampden-Sydney College) Subqueries Fri, Feb 7, 2014 2 / 20

Outline

1 Subqueries

2 The IN Operator

3 The ALL Operator

4 Assignment

Robb T. Koether (Hampden-Sydney College) Subqueries Fri, Feb 7, 2014 3 / 20

Subqueries

In MySQL queries may be nested.For example, suppose we have a table new_courses thatcontains tuples of new courses to be added to courses.The following query will insert the new courses into the coursestable.

Nested QueriesINSERT INTO employees (SELECT * FROM new_hires);

Robb T. Koether (Hampden-Sydney College) Subqueries Fri, Feb 7, 2014 4 / 20

Subqueries

Similarly, if we have a table old_courses that contains tuples ofold courses to be deleted from courses, then the following querywill delete the old courses from the courses table.

Nested QueriesDELETE FROM employeesWHERE ssn IN(SELECT ssn FROM fired);

Robb T. Koether (Hampden-Sydney College) Subqueries Fri, Feb 7, 2014 5 / 20

Outline

1 Subqueries

2 The IN Operator

3 The ALL Operator

4 Assignment

Robb T. Koether (Hampden-Sydney College) Subqueries Fri, Feb 7, 2014 6 / 20

The IN Operator

The IN Operatorattribute IN relation

or

(attribute_list) IN relation

The IN operator may be used in the WHERE clause to test whethera value or set of values is in a relation.The expression is true if the attribute or attribute list matches anyof the tuples in the relation.But it must match the entire tuple exactly.

Robb T. Koether (Hampden-Sydney College) Subqueries Fri, Feb 7, 2014 7 / 20

The IN Operator

The IN OperatorSELECT fname, lnameFROM employeeWHERE ssn IN

(SELECT ssnFROM dependants);

Find all employees who have dependants.

What is another way to write the query without using IN?

Robb T. Koether (Hampden-Sydney College) Subqueries Fri, Feb 7, 2014 8 / 20

The IN Operator

The IN OperatorSELECT fname, lnameFROM employeeWHERE ssn IN

(SELECT ssnFROM dependants);

Find all employees who have dependants.What is another way to write the query without using IN?

Robb T. Koether (Hampden-Sydney College) Subqueries Fri, Feb 7, 2014 8 / 20

The IN Operator

The IN OperatorSELECT fname, lnameFROM employeeWHERE ssn IN

(SELECT mgr_ssnFROM departments);

Find all employees who are managers.

Write this another way without using IN.

Robb T. Koether (Hampden-Sydney College) Subqueries Fri, Feb 7, 2014 9 / 20

The IN Operator

The IN OperatorSELECT fname, lnameFROM employeeWHERE ssn IN

(SELECT mgr_ssnFROM departments);

Find all employees who are managers.Write this another way without using IN.

Robb T. Koether (Hampden-Sydney College) Subqueries Fri, Feb 7, 2014 9 / 20

The IN Operator

The IN OperatorSELECT proj_name, sexFROM projects AS p NATURAL JOIN employeesWHERE ssn IN

(SELECT ssnFROM works AS wWHERE w.proj = p.proj)

Create a table of project names and sexes of all employeesworking on that project.

Robb T. Koether (Hampden-Sydney College) Subqueries Fri, Feb 7, 2014 10 / 20

The IN Operator

Find all projects, if any, on which at least one male is working.

Find all projects, if any, on which no male is working.Find all projects, if any, on which at least one male and at leastone female is working.

Robb T. Koether (Hampden-Sydney College) Subqueries Fri, Feb 7, 2014 11 / 20

The IN Operator

Find all projects, if any, on which at least one male is working.Find all projects, if any, on which no male is working.

Find all projects, if any, on which at least one male and at leastone female is working.

Robb T. Koether (Hampden-Sydney College) Subqueries Fri, Feb 7, 2014 11 / 20

The IN Operator

Find all projects, if any, on which at least one male is working.Find all projects, if any, on which no male is working.Find all projects, if any, on which at least one male and at leastone female is working.

Robb T. Koether (Hampden-Sydney College) Subqueries Fri, Feb 7, 2014 11 / 20

The IN Operator

Using the NATURAL JOIN OperatorSELECT proj_nameFROM projects NATURAL JOIN employees NATURAL JOIN worksWHERE sex = ’M’GROUP BY projHAVING COUNT(*) > 0;

Any query that can be accomplished by using IN can also beaccomplished by using joins.

Robb T. Koether (Hampden-Sydney College) Subqueries Fri, Feb 7, 2014 12 / 20

Outline

1 Subqueries

2 The IN Operator

3 The ALL Operator

4 Assignment

Robb T. Koether (Hampden-Sydney College) Subqueries Fri, Feb 7, 2014 13 / 20

The ALL Operator

The ALL Operatorattribute rel_op ALL relation

The ALL operator may be used to test a relationship between anattribute and all of the tuples in a relation.The rel_op is one of the relative operators =, <>, <, >, <=, and >=.The expression is true if the relationship holds between thespecified attribute and all of the tuples in the relation.

Robb T. Koether (Hampden-Sydney College) Subqueries Fri, Feb 7, 2014 14 / 20

The ALL Operator

The ALL OperatorSELECT fname, lname, salaryFROM employeesWHERE salary > AVG(salary);

Find the names and salaries of all employees who earn more thanthe average salary of all employees at the company.The above query will not work. Why not?

Robb T. Koether (Hampden-Sydney College) Subqueries Fri, Feb 7, 2014 15 / 20

The ALL Operator

The ALL OperatorSELECT fname, lname, salaryFROM employeesWHERE salary > ALL

(SELECT AVG(salary)FROM employees);

This query will work.

Robb T. Koether (Hampden-Sydney College) Subqueries Fri, Feb 7, 2014 16 / 20

The ALL Operator

The ALL OperatorSELECT fname, lname, salaryFROM employeesWHERE salary < ALL

(SELECT AVG(salary)FROM employees AS e, departments AS dWHERE e.dept = d.deptGROUP BY e.dept);

Find the names and salaries of all employees who earn less thanthe average salary of each department.

Find the names and salaries of all employees who earn less thanthe average salary of their own department.

Robb T. Koether (Hampden-Sydney College) Subqueries Fri, Feb 7, 2014 17 / 20

The ALL Operator

The ALL OperatorSELECT fname, lname, salaryFROM employeesWHERE salary < ALL

(SELECT AVG(salary)FROM employees AS e, departments AS dWHERE e.dept = d.deptGROUP BY e.dept);

Find the names and salaries of all employees who earn less thanthe average salary of each department.Find the names and salaries of all employees who earn less thanthe average salary of their own department.

Robb T. Koether (Hampden-Sydney College) Subqueries Fri, Feb 7, 2014 17 / 20

The ALL Operator

Find all projects that have more than the average number ofpeople working on them.

Robb T. Koether (Hampden-Sydney College) Subqueries Fri, Feb 7, 2014 18 / 20

Outline

1 Subqueries

2 The IN Operator

3 The ALL Operator

4 Assignment

Robb T. Koether (Hampden-Sydney College) Subqueries Fri, Feb 7, 2014 19 / 20

Assignment

AssignmentRead Subsections 5.1.1 - 5.1.5.

Robb T. Koether (Hampden-Sydney College) Subqueries Fri, Feb 7, 2014 20 / 20

top related