Subqueries - Lecture 11 Subsections 5.1.1 -...

25
Subqueries Lecture 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

Transcript of Subqueries - Lecture 11 Subsections 5.1.1 -...

Page 1: Subqueries - Lecture 11 Subsections 5.1.1 - 5.1people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · The following query will insert the new courses into the courses table. Nested

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

Page 2: Subqueries - Lecture 11 Subsections 5.1.1 - 5.1people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · The following query will insert the new courses into the courses table. Nested

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

Page 3: Subqueries - Lecture 11 Subsections 5.1.1 - 5.1people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · The following query will insert the new courses into the courses table. Nested

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

Page 4: Subqueries - Lecture 11 Subsections 5.1.1 - 5.1people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · The following query will insert the new courses into the courses table. Nested

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

Page 5: Subqueries - Lecture 11 Subsections 5.1.1 - 5.1people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · The following query will insert the new courses into the courses table. Nested

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

Page 6: Subqueries - Lecture 11 Subsections 5.1.1 - 5.1people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · The following query will insert the new courses into the courses table. Nested

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

Page 7: Subqueries - Lecture 11 Subsections 5.1.1 - 5.1people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · The following query will insert the new courses into the courses table. Nested

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

Page 8: Subqueries - Lecture 11 Subsections 5.1.1 - 5.1people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · The following query will insert the new courses into the courses table. Nested

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

Page 9: Subqueries - Lecture 11 Subsections 5.1.1 - 5.1people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · The following query will insert the new courses into the courses table. Nested

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

Page 10: Subqueries - Lecture 11 Subsections 5.1.1 - 5.1people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · The following query will insert the new courses into the courses table. Nested

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

Page 11: Subqueries - Lecture 11 Subsections 5.1.1 - 5.1people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · The following query will insert the new courses into the courses table. Nested

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

Page 12: Subqueries - Lecture 11 Subsections 5.1.1 - 5.1people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · The following query will insert the new courses into the courses table. Nested

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

Page 13: Subqueries - Lecture 11 Subsections 5.1.1 - 5.1people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · The following query will insert the new courses into the courses table. Nested

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

Page 14: Subqueries - Lecture 11 Subsections 5.1.1 - 5.1people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · The following query will insert the new courses into the courses table. Nested

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

Page 15: Subqueries - Lecture 11 Subsections 5.1.1 - 5.1people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · The following query will insert the new courses into the courses table. Nested

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

Page 16: Subqueries - Lecture 11 Subsections 5.1.1 - 5.1people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · The following query will insert the new courses into the courses table. Nested

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

Page 17: Subqueries - Lecture 11 Subsections 5.1.1 - 5.1people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · The following query will insert the new courses into the courses table. Nested

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

Page 18: Subqueries - Lecture 11 Subsections 5.1.1 - 5.1people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · The following query will insert the new courses into the courses table. Nested

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

Page 19: Subqueries - Lecture 11 Subsections 5.1.1 - 5.1people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · The following query will insert the new courses into the courses table. Nested

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

Page 20: Subqueries - Lecture 11 Subsections 5.1.1 - 5.1people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · The following query will insert the new courses into the courses table. Nested

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

Page 21: Subqueries - Lecture 11 Subsections 5.1.1 - 5.1people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · The following query will insert the new courses into the courses table. Nested

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

Page 22: Subqueries - Lecture 11 Subsections 5.1.1 - 5.1people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · The following query will insert the new courses into the courses table. Nested

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

Page 23: Subqueries - Lecture 11 Subsections 5.1.1 - 5.1people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · The following query will insert the new courses into the courses table. Nested

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

Page 24: Subqueries - Lecture 11 Subsections 5.1.1 - 5.1people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · The following query will insert the new courses into the courses table. Nested

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

Page 25: Subqueries - Lecture 11 Subsections 5.1.1 - 5.1people.hsc.edu/faculty-staff/robbk/Coms480/Lectures... · The following query will insert the new courses into the courses table. Nested

Assignment

AssignmentRead Subsections 5.1.1 - 5.1.5.

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