43
Using Subqueries to Solve Queries Using the Set Operators

Using Subqueries to Solve Queries Using the Set Operators

Embed Size (px)

DESCRIPTION

Using Subqueries to Solve Queries Using the Set Operators. Objectives. After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries can solve List the types of subqueries - PowerPoint PPT Presentation

Citation preview

Retrieving Data Using the SQL Select Statement and How to use SQL Developer

Using Subqueries to Solve QueriesUsing the Set Operators ObjectivesAfter completing this lesson, you should be able to do the following:Define subqueriesDescribe the types of problems that the subqueries can solveList the types of subqueriesWrite single-row and multiple-row subqueriesAfter completing this lesson, you should be able to do the following:Describe set operatorsUse a set operator to combine multiple queries into a single queryControl the order of rows returnedOracle Database 11g: SQL Fundamentals I 7 - 2Lesson AgendaSubquery: Types, syntax, and guidelinesSingle-row subqueries:Group functions in a subqueryHAVING clause with subqueriesMultiple-row subqueriesUse ALL or ANY operator.Null values in a subqueryOracle Database 11g: SQL Fundamentals I 7 - 3Using a Subquery to Solve a ProblemWho has a salary greater than Abels?Which employees have salaries greater than Abels salary?Main query:What is Abels salary?Subquery:

Oracle Database 11g: SQL Fundamentals I 7 - 4Subquery SyntaxThe subquery (inner query) executes before the main query (outer query).The result of the subquery is used by the main query.SELECTselect_listFROMtableWHEREexpr operator (SELECTselect_list FROMtable);Oracle Database 11g: SQL Fundamentals I 7 - 5SELECT last_name, salaryFROM employeesWHERE salary > (SELECT salary FROM employees WHERE last_name = 'Abel');Using a Subquery11000

Oracle Database 11g: SQL Fundamentals I 7 - 6Guidelines for Using SubqueriesEnclose subqueries in parentheses.Place subqueries on the right side of the comparison condition for readability (However, the subquery can appear on either side of the comparison operator.).Use single-row operators with single-row subqueries and multiple-row operators with multiple-row subqueries.Oracle Database 11g: SQL Fundamentals I 7 - 7Types of SubqueriesSingle-row subquery

Multiple-row subqueryMain querySubquery returnsST_CLERKST_CLERKSA_MANMain querySubquery returnsOracle Database 11g: SQL Fundamentals I 7 - 8Lesson AgendaSubquery: Types, syntax, and guidelinesSingle-row subqueries:Group functions in a subqueryHAVING clause with subqueriesMultiple-row subqueriesUse ALL or ANY operatorNull values in a subqueryOracle Database 11g: SQL Fundamentals I 7 - 9Single-Row SubqueriesReturn only one rowUse single-row comparison operatorsGreater than or equal to >=Less than (SELECT salary FROM employees WHERE last_name = Taylor);Executing Single-Row SubqueriesSA_REP8600

Oracle Database 11g: SQL Fundamentals I 7 - 11SELECT last_name, job_id, salaryFROM employeesWHERE salary = (SELECT MIN(salary) FROM employees);Using Group Functions in a Subquery2500

Oracle Database 11g: SQL Fundamentals I 7 - 12SELECT department_id, MIN(salary)FROM employeesGROUP BY department_idHAVING MIN(salary) > (SELECT MIN(salary) FROM employees WHERE department_id = 50);The HAVING Clause with SubqueriesThe Oracle server executes the subqueries first.The Oracle server returns results into the HAVING clause of the main query.2500

Oracle Database 11g: SQL Fundamentals I 7 - 13

SELECT employee_id, last_nameFROM employeesWHERE salary = (SELECT MIN(salary) FROM employees GROUP BY department_id);What Is Wrong with This Statement?Single-row operator with multiple-row subquery

Oracle Database 11g: SQL Fundamentals I 7 - 14SELECT last_name, job_idFROM employeesWHERE job_id = (SELECT job_id FROM employees WHERE last_name = 'Haas');No Rows Returned by the Inner QuerySubquery returns no rows because there is no employee named Haas.

Oracle Database 11g: SQL Fundamentals I 7 - 15Lesson AgendaSubquery: Types, syntax, and guidelinesSingle-row subqueries:Group functions in a subqueryHAVING clause with subqueriesMultiple-row subqueriesUse ALL or ANY operatorNull values in a subqueryOracle Database 11g: SQL Fundamentals I 7 - 16Multiple-Row SubqueriesReturn more than one rowUse multiple-row comparison operatorsMust be preceded by =, !=, >, ,