Alkérdések II. Copyright 2004, Oracle. All rights reserved.

Similar documents
EXISTS NOT EXISTS WITH

Displaying Data from Multiple Tables. Copyright 2004, Oracle. All rights reserved.

Join, Sub queries and set operators

Retrieving Data from Multiple Tables

Database Programming with SQL

Database Foundations. 6-9 Joining Tables Using JOIN. Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Intermediate SQL: Aggregated Data, Joins and Set Operators

KORA. RDBMS Concepts II

ÇALIŞMA TEST SORULARI

GIFT Department of Computing Science. CS-217/224: Database Systems. Lab-5 Manual. Displaying Data from Multiple Tables - SQL Joins

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

Creating Other Schema Objects

Introduction to Oracle9i: SQL

Additional Practice Solutions

Database Programming with SQL

Oracle Database SQL Basics

Manipulating Data. Copyright 2004, Oracle. All rights reserved.

Database Foundations. 6-4 Data Manipulation Language (DML) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Using the Set Operators. Copyright 2004, Oracle. All rights reserved.

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

Objectives. After completing this lesson, you should be able to do the following:

Database Programming with SQL

RETRIEVING DATA USING THE SQL SELECT STATEMENT

HR Database. Sample Output from TechWriter 2007 for Databases

Updating Column Values and Deleting Rows. Copyright 2008, Oracle. All rights reserved.

Retrieving Data Using the SQL SELECT Statement. Copyright 2009, Oracle. All rights reserved.

Creating Other Schema Objects. Copyright 2004, Oracle. All rights reserved.

2. What privilege should a user be given to create tables? The CREATE TABLE privilege

Using the Set Operators. Copyright 2006, Oracle. All rights reserved.

SQL and PL/SQL New Features in Oracle Database 12c Czinkóczki László

Database Programming with SQL

Three types of sub queries are supported in SQL are Scalar, Row and Table sub queries.

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

DUE: 9. Create a query that will return the average order total for all Global Fast Foods orders from January 1, 2002, to December 21, 2002.

DEFAULT Values, MERGE, and Multi-Table Inserts. Copyright 2009, Oracle. All rights reserved.

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

Database Programming with PL/SQL

1Z0-007 ineroduction to oracle9l:sql

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

Oracle Database 10g: SQL Fundamentals II

Lab # 6. Using Subqueries and Set Operators. Eng. Alaa O Shama

Data Manipulation Language

Introduction to Explicit Cursors. Copyright 2008, Oracle. All rights reserved.

Limit Rows Selected. Copyright 2008, Oracle. All rights reserved.

Oracle 1Z Oracle Database 11g : SQL Fundamentals I. Download Full Version :

Introduction to Oracle9i: SQL

Oracle Database 10g: SQL Fundamentals II

Táblák tartalmának módosítása. Copyright 2004, Oracle. All rights reserved.

A subquery is a nested query inserted inside a large query Generally occurs with select, from, where Also known as inner query or inner select,

What Are Group Functions? Reporting Aggregated Data Using the Group Functions. Objectives. Types of Group Functions

CBO SQL TRANSFORMER Document describes a few examples of transformations made by CBO.

Misc. Triggers Views Roles Sequences - Synonyms. Eng. Mohammed Alokshiya. Islamic University of Gaza. Faculty of Engineering

Database Programming - Section 3. Instructor Guide

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Assignment Grading Rubric

Bsc (Hons) Software Engineering. Examinations for / Semester 1. Resit Examinations for BSE/15A/FT & BSE/16A/FT

Database Programming with PL/SQL

CS 275 Winter 2011 Problem Set 3

Database Systems CSE 303. Outline. Lecture 06: SQL. What is Sub-query? Sub-query in WHERE clause Subquery

Lab # 6. Data Manipulation Language (DML)

Exam: 1Z Title : Introduction to Oracle9i: SQL. Ver :

Database Programming with SQL

Série n 6 Bis : Ateliers SQL Data Modeler (Oracle)

Working with Columns, Characters and Rows. Copyright 2008, Oracle. All rights reserved.

20 Essential Oracle SQL and PL/SQL Tuning Tips. John Mullins

ITCertMaster. Safe, simple and fast. 100% Pass guarantee! IT Certification Guaranteed, The Easy Way!

Subquery: There are basically three types of subqueries are:

Course Overview. Copyright 2010, Oracle and/or its affiliates. All rights reserved.

Sun Certified MySQL Associate

Institute of Aga. Network Database LECTURER NIYAZ M. SALIH

Querying Data with Transact-SQL (761)

Testing Masters Technologies

Advance Database Systems. Joining Concepts in Advanced SQL Lecture# 4

Database Programming - Section 7. Instructor Guide

Exploring Best Practices and Guidelines for Tuning SQL Statements

Institute of Aga. Microsoft SQL Server LECTURER NIYAZ M. SALIH

Database Programming with PL/SQL

Querying Data with Transact SQL

SELF TEST. List the Capabilities of SQL SELECT Statements

Exam : 1Z Title : Introduction to Oracle9i: SQL

Database Programming with PL/SQL

Oracle Syllabus Course code-r10605 SQL

Database Programming - Section 11. Instructor Guide

LECTURE10: DATA MANIPULATION IN SQL, ADVANCED SQL QUERIES

Using DDL Statements to Create and Manage Tables. Copyright 2004, Oracle. All rights reserved.

CSE 530A SQL. Washington University Fall 2013

Using DDL Statements to Create and Manage Tables. Copyright 2004, Oracle. All rights reserved.

CIS 363 MySQL. Chapter 12 Joins Chapter 13 Subqueries

Database Programming with PL/SQL

1 Writing Basic SQL SELECT Statements 2 Restricting and Sorting Data

Joining tables. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

Lecture10: Data Manipulation in SQL, Advanced SQL queries

1z Exam Code: 1z Exam Name: Oracle Database SQL Expert

Oracle 1Z0-007 Introduction to Oracle9i: SQL 210 Q&A

Set theory is a branch of mathematics that studies sets. Sets are a collection of objects.

School of Computing and Information Technology Session: Spring CSCI835 Database Systems (Bridging Subject) Sample class test 23 July 2018

Fravo.com. Certification Made Easy. World No1 Cert Guides. Introduction to Oracle9i: SQL Exam 1Z Edition 1.0

Advanced SQL Processing Prepared by Destiny Corporation

Exam Code: 1z Exam Name: Ineroduction to oracle9l:sql. Vendor: Oracle. Version: DEMO

Transcription:

Alkérdések II. Copyright 2004, Oracle. All rights reserved.

Objectives After completing this lesson, you should be able to do the following: Write a multiple-column subquery Use scalar subqueries in SQL Solve problems with correlated subqueries Update and delete rows using correlated subqueries Use the EXISTSand NOTEXISTSoperators Use the WITHclause 6-2 Copyright 2004, Oracle. All rights reserved.

Multiple-Column Subqueries Main query WHERE (MANAGER_ID, DEPARTMENT_ID) IN Subquery 100 90 102 60 124 50 Each row of the main query is compared to values from a multiple-row and multiple-column subquery. 6-3 Copyright 2004, Oracle. All rights reserved.

Column Comparisons Column comparisons in a multiple-column subquery can be: Pairwise comparisons Nonpairwise comparisons 6-4 Copyright 2004, Oracle. All rights reserved.

Pairwise Comparison Subquery Display the details of the employees who are managed by the same manager andwork in the same department as the employees with EMPLOYEE_ID199 or 174. SELECT employee_id, manager_id, department_id FROM employees WHERE (manager_id, department_id) IN (SELECT manager_id, department_id FROM employees WHERE employee_id IN (199,174)) AND employee_id NOT IN (199,174); 6-5 Copyright 2004, Oracle. All rights reserved.

Nonpairwise Comparison Subquery Display the details of the employees who are managed by the same manager as the employees with EMPLOYEE_ID174 or 199 andwork in the same department as the employees with EMPLOYEE_ID174 or 199. SELECT employee_id, manager_id, department_id FROM employees WHERE manager_id IN (SELECT manager_id FROM employees WHERE employee_id IN (174,199)) AND department_id IN (SELECT department_id FROM employees WHERE employee_id IN (174,199)) AND employee_id NOT IN(174,199); 6-6 Copyright 2004, Oracle. All rights reserved.

Scalar Subquery Expressions A scalar subquery expression is a subquery that returns exactly one column value from one row. Scalar subqueries can be used in: Condition and expression part of DECODE and CASE All clauses of SELECTexcept GROUPBY 6-7 Copyright 2004, Oracle. All rights reserved.

Scalar Subqueries: Examples Scalar subqueries in CASE expressions SELECT employee_id, last_name, (CASE 20 WHEN department_id = (SELECT department_id FROM departments WHERE location_id = 1800) THEN 'Canada' ELSE 'USA' END) location FROM employees; Scalar subqueries in ORDER BY clause SELECT employee_id, last_name FROM employees e ORDER BY (SELECT department_name FROM departments d WHERE e.department_id = d.department_id); 6-8 Copyright 2004, Oracle. All rights reserved.

Correlated Subqueries Correlated subqueries are used for row-by-row processing. Each subquery is executed once for every row of the outer query. GET candidate row from outer query EXECUTE inner query using candidate row value USE values from inner query to qualify or disqualify candidate row 6-9 Copyright 2004, Oracle. All rights reserved.

Correlated Subqueries The subquery references a column from a table in the parent query. SELECT column1, column2,... FROM table1 outer WHERE column1 operator (SELECT column1, column2 FROM table2 WHERE expr1 = outer.expr2); 6-10 Copyright 2004, Oracle. All rights reserved.

Using Correlated Subqueries Find all employees who earn more than the average salary in their department. SELECT last_name, salary, department_id FROM employees outer WHERE salary > (SELECT AVG(salary) FROM employees WHERE department_id = outer.department_id); Each time a row from the outer query is processed, the inner query is evaluated. 6-11 Copyright 2004, Oracle. All rights reserved.

Using Correlated Subqueries Display details of those employees who have changed jobs at least twice. SELECT e.employee_id, last_name,e.job_id FROM employees e WHERE 2 <= (SELECT COUNT(*) FROM job_history WHERE employee_id = e.employee_id); 6-12 Copyright 2004, Oracle. All rights reserved.

Using the EXISTS Operator The EXISTSoperator tests for existence of rows in the results set of the subquery. If a subquery row value is found: The search does not continue in the inner query The condition is flagged TRUE If a subquery row value is not found: The condition is flagged FALSE The search continues in the inner query 6-13 Copyright 2004, Oracle. All rights reserved.

Find Employees Who Have at Least One Person Reporting to Them SELECT employee_id, last_name, job_id, department_id FROM employees outer WHERE EXISTS ( SELECT 'X' FROM employees WHERE manager_id = outer.employee_id); 6-14 Copyright 2004, Oracle. All rights reserved.

Find All Departments That Do Not Have Any Employees SELECT department_id, department_name FROM departments d WHERE NOT EXISTS (SELECT 'X' FROM employees WHERE department_id = d.department_id); 6-15 Copyright 2004, Oracle. All rights reserved.

The WITH Clause Using the WITHclause, you can use the same query block in a SELECTstatement when it occurs more than once within a complex query. The WITHclause retrieves the results of a query block and stores it in the user s temporary tablespace. The WITH clause improves performance. 6-16 Copyright 2004, Oracle. All rights reserved.

WITH Clause: Example Using the WITHclause, write a query to display the department name and total salaries for those departments whose total salary is greater than the average salary across departments. 6-17 Copyright 2004, Oracle. All rights reserved.

WITH Clause: Example WITH dept_costs AS ( SELECT d.department_name, SUM(e.salary) AS dept_total FROM employees e JOIN departments d ON e.department_id = d.department_id GROUP BY d.department_name), avg_cost AS ( SELECT SUM(dept_total)/COUNT(*) AS dept_avg FROM dept_costs) SELECT * FROM dept_costs WHERE dept_total > (SELECT dept_avg FROM avg_cost) ORDER BY department_name; 6-18 Copyright 2004, Oracle. All rights reserved.

Summary In this lesson, you should have learned the following: A multiple-column subquery returns more than one column. Multiple-column comparisons can be pairwise or nonpairwise. A multiple-column subquery can also be used in the FROM clause of a SELECT statement. 6-19 Copyright 2004, Oracle. All rights reserved.

Summary Correlated subqueries are useful whenever a subquery must return a different result for each candidate row. The EXISTSoperator is a Boolean operator that tests the presence of a value. Correlated subqueries can be used with SELECT, UPDATE, and DELETE statements. You can use the WITHclause to use the same query block in a SELECTstatement when it occurs more than once. 6-20 Copyright 2004, Oracle. All rights reserved.