1 JOIN SUBQUERY Structured Query Language (SQL) - Part III.

Slides:



Advertisements
Similar presentations
Chapter 4 Joining Multiple Tables
Advertisements

A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
OUTLINE OF THE LECTURE PART I GOAL: Understand the Data Definition Statements in Fig 4.1 Step1: Columns of the Tables and Data types. Step2: Single column.
DatabaseDatabase cs453 Lab8 1 Ins.Ebtesam AL-Etowi.
SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
Multiple-Column Subqueries 12. Objectives After completing this lesson, you should be able to do the following: Write a multiple-column subquery Describe.
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
1 DDL – subquery Sen Zhang. 2 Objectives What is a subquery? Learn how to create nested SQL queries Read sample scripts and book for different kinds of.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Jyh-haw Yeh Dept. of Computer Science Boise State University
Database Systems More SQL Database Design -- More SQL1.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
Chapter 9 Joining Data from Multiple Tables
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
A Guide to MySQL 5. 2 Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables Use a subquery.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
1 CSE 480: Database Systems Lecture 11: SQL. 2 SQL Query SELECT FROM WHERE –In MySQL, FROM and WHERE clauses are optional –Example:
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-column subqueries, Multiple-row Subqueries, Correlated Subqueries 11/2/10,
Relational Algebra - Chapter (7th ed )
Query Processing and Optimization
Nested Queries (Sub Queries) A nested query is a form of a SELECT command that appears inside another SQL statement. It is also termed as subquery. The.
Retrieve the names of all employees in department 5 who work more than 10 hours per week on the ‘ProductX’ project. p10ssn ← (Π essn (σ hours > 10 (works-on.
Joins & Sub-queries. Oracle recognizes that you may want data that resides in multiple tables drawn together in some meaningful way. One of the most important.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
Database Management Systems. NESTING OF QUERIES  Some queries require that existing values in the database be retrieved and then used in a comparison.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
1 Multiple Table Queries. 2 Objectives  Retrieve data from more than one table by joining tables  Using IN and EXISTS to query multiple tables  Nested.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)
Structured Query Language
1 Querying a Single Table Structured Query Language (SQL) - Part II.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
Chapter 12 Subqueries and Merge Statements
Displaying Data from Multiple Tables (SQL99 Syntax with examples)
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Subqueries These slides are licensed under.
Slide 6- 1 Additional Relational Operations Aggregate Functions and Grouping A type of request that cannot be expressed in the basic relational algebra.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-row Subqueries, Correlated Subqueries.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
Slide 1 of 32ASH-Training Querying and Managing Data Using SQL Server 2014 By: Segla In this session, you will learn to: Query data by using joins Query.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
CS580 Advanced Database Topics Chapter 8 SQL Irena Pevac.
DQL Statements Lab - 3 COMP 353 Summer
Using Subqueries to Solve Queries
Chapter (6) The Relational Algebra and Relational Calculus Objectives
CS580 Advanced Database Topics
Multiple-Column Subqueries
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Multiple Table Queries
6/22/2018.
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Using Subqueries to Solve Queries
Writing Correlated Subqueries
Writing Basic SQL SELECT Statements
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Using Subqueries to Solve Queries
Advance Database Systems
Subqueries Schedule: Timing Topic 25 minutes Lecture
Using Subqueries to Solve Queries
Using Subqueries to Solve Queries
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
SQL: Set Operations & Nested Queries. Presented by: Dr. Samir Tartir
Presentation transcript:

1 JOIN SUBQUERY Structured Query Language (SQL) - Part III

2 JOIN l JOIN allows us to retrieve related data from more than one table l SELECT statement is used for joining tables l Attributes to be selected are listed following the keyword SELECT l Tables participating in the join operation are listed in the FROM clause l The join condition is specified in the WHERE clause u You may use any comparison operator (=, >, =. !=, <>, !>, !<) here, though equality is more commonly used

3 JOINING MORE THAN TWO TABLES EMPLOYEE EIDEnameDcodeRankSalary 1001John Joe Jack Jane Jill Jeb Jim DEPARTMENT DcodeDname 1Accounting 2Finance 3Sales RANKS RankTitleMin SalMax Sal 1Supervisor Jr. Mgr Manager Asst. Director Director

4 JOIN /* List employee ids and names with their department names */ SELECT eid, ename, dname FROM Employee, Department WHERE Employee.dcode = Department.dcode; EIDEnameDname 1001JohnAccounting 1002JoeFinance 1003JackSales 1004JaneFinance 1005JillFinance 1006JebSales 1007JimAccounting

5 JOIN l Qualifying column names in a query u when column names are not unique, they must be qualified by the table names to eliminate ambiguity u when column names are unique, qualifying is not required but may be done for documentation purpose u Example: SELECT eid, ename, dname FROM Employee, Department WHERE Employee.dcode = Department.dcode; /* This statement qualifies each column name by the appropriate table name. Note that here qualification is required only in the WHERE clause */ SELECT Employee.eid, Employee.ename, Department.dname FROM Employee, Department WHERE Employee.dcode = Department.dcode;

6 JOIN l Using aliases u an alias assigns a unique identifier to the table in a query u the alias must follow the table name u use a space to separate the table name from its alias u Example: SELECT Employee.eid, Employee.ename, Department.dname FROM Employee, Department WHERE Employee.dcode = Department.dcode; /* This statement uses table name aliases. */ SELECT e.eid, e.ename, d.dname FROM Employee e, Department d WHERE e.dcode = d.dcode;

7 JOIN /* List the employees and their department names for those that earn more than */ SELECT e.eid, e.ename, e.salary, d.dname FROM Employee e, Department d WHERE e.salary > AND e.dcode = d.dcode; EIDEnameSalaryDname 1002Joe40000Finance 1003Jack40000Sales 1005Jill45000Finance l You may use additional selection criteria with join u Example:

8 JOINING MORE THAN TWO TABLES SELECT e.eid, e.ename, d.dname, r.title FROM Employee e, Department d, Ranks r WHERE e.dcode = d.dcode ANDe.rank = r.rank ORDER By d.dname, e.rank desc; EIDEnameDnameTitle 1001JohnAccounting Supervisor 1007JimAccountingSupervisor 1005JillFinanceManager 1002JoeFinanceJr. Mgr 1004JaneFinanceSupervisor 1003JackSalesJr. Mgr 1006JebSalesSupervisor

9 JOINING A TABLE TO ITSELF EMP EIDEnameMgrID 1001John JoeNull 1003Jack JaneNull 1005Jill Jeb Jim1002 /* List the manager name for each employee Notice the use of column headings in the query */ SELECT e1.ename “Employee”, e2.ename “Manager” FROM Emp e1, Emp e2 WHERE e1.mgrid = e2.eid EmployeeManager JohnJoe JackJane JillJane JebJane JimJoe

10 OUTER JOIN l Outer join is indicated by a + sign in Oracle l Oracle supports left and right outer joins only l Left (right) outer join retains the unmatched rows from the left (right) table in the result /* This is an example of right outer join */ SELECT e.eid, e.ename, r.rank, r.title FROM Employee e, Ranks r WHERE e.rank(+) = r.rank; EIDEnameRankTitle 1001John1Supervisor 1002Joe2Jr. Mgr. 1003Jack2Jr. Mgr. 1004Jane1Supervisor 1005Jill3Manager 1006Jeb1Supervisor 1007Jim1Supervisor NullNull4Asst. Director NullNull5Director

11 JOINS with NULL Values l Null does not have a value. So it does not match with any value, including another null value. l Example: TABLE1 AB 1One NULLThree 4Join4 TABLE2 CD NULLTwo 4Four SELECT A, B, D FROM TABLE1, TABLE2 WHERE a = c; ABD 4Join4Four

12 JOINS with NULL Values l You can use outer join to detect null values in a column. l Example: /* Left outer join includes all the rows from the left table in the result */ SELECT * FROM TABLE1, TABLE2 WHERE a = c (+); ABCD 1OneNULLNULL NULLThreeNULLNULL 4Join44Four TABLE1 AB 1One NULLThree 4Join4 TABLE2 CD NULLTwo 4Four

13 SUBQUERIES l A subquery is a SELECT statement that is nested inside another SELECT statement or another subquery l There is no restriction on the depth of such nesting l A query containing subqueries is evaluated starting with the innermost subquery l Example: u Suppose we want to list employees that earn the lowest salary u This requires two queries, based on what we have learnt so far l First we will find the minimum salary earned by an employee l Then we will use the min salary value to list the names of employees who earn this salary u We can combine these two steps in to a single query by nesting a subquery within a query

14 SUBQUERIES l All examples in this section are based on the tables in Bordoloi (pp. 82, 83). /* Find the minimum salary */ SELECT min(salary) FROM Employee; Min(Salary) /* List names of employees that earn the min salary found in the previous step */ SELECT fname, lname FROM Employee WHERE salary = 25000; /* Use of subquery */ SELECT fname, lname FROM Employee WHERE salary = (SELECT min(salary) FROM Employee); FnameLname AliciaZelaya JoyceEnglish AhmadJabbar FnameLname AliciaZelaya JoyceEnglish AhmadJabbar

15 SUBQUERIES l Three basic types of subqueries u One that returns a single value (Example in previous slide) l normally uses aggregate functions l The returned value is compared in the outer query using a comparison operator such as =, !=, >, <, etc. u One that returns a list of values l these values are from a single column, such as a list of order numbers, list of majors, list of departments, etc. l Outer query uses a list comparison operator, such as IN, or an comparison operator modified by ANY or ALL clause. l SELECT clause of the subquery must contain a single column name u subqueries that are used to check for existence of data l Outer query uses the EXISTS keyword.

16 SUBQUERY SYNTAX l A subquery uses a SELECT statement l It is enclosed within parentheses l It may appear in the WHERE clause or the HAVING clause in the outer query l A subquery cannot use the ORDER BY clause l The result of a subquery must be join compatible with WHERE / HAVING clause of the outer query, i.e., the result must be comparable.

17 SUBQUERIES WITH IN l WHERE [NOT] IN (subquery) l Subquery returns a list l Example: /* List employees that have a male dependent */ SELECT lname, fname FROM Employee WHERE ssn IN ( SELECT essn FROM Dependent where sex = ‘M’); LnameFname SmithJohn WongFranklin WallaceJennifer

18 SUBQUERIES WITH IN l How is the previous query evaluated? /* First the subquery is evaluated */ SELECT essn FROM Dependent where sex = ‘M’; Essn /* Then the outer query is evaluated using the result of the subquery */ SELECT lname, fname FROM Employee WHERE ssn IN ( , , );

19 SUBQUERIES WITH IN l You can get the same result by writing a JOIN query l It is, however, desirable to use a subquery in this case because the result is from a single table /* The equivalent JOIN query */ SELECT lname, fname FROM Employee, Dependent WHERE employee.ssn = dependent.essn AND dependent.sex = ‘M’; /* Check out the subquery and the join query with sex = ‘F’ */ /* Are the results the same? Explain. */

20 MULTIPLE LEVELS OF NESTING l Oracle places no limit on the level of nesting /* List the names of all employees who worked more than 10 hours on the Newbenefits project. */ SELECT lname, fname FROM Employee WHERE ssn IN (SELECT essn FROM Works_on WHERE hours > 10 AND pno IN (SELECT pnumber FROM Project WHERE pname = ‘Newbenefits’) );

21 MULTIPLE LEVELS OF NESTING l How is the previous query evaluated? /* We start with the innermost subquery */ SELECT pnumber FROM Project WHERE pname = "Newbenefits”; Pnumber 30 /* The result is plugged into the next subquery */ SELECT essn FROM Works_on WHERE hours > 10 AND pno IN (30); Essn /* Then the final query is evaluated */ SELECT lname, fname FROM Employee WHERE ssn IN ( , );

22 USE OF ANY or ALL l WHERE [ANY | ALL] (subquery) l The result of the subquery in this case is a list /* List all employees that earn more than all of the employees in Dept. 5 */ SELECT lname, fname FROM Employee WHERE salary > ALL (SELECT salary FROM Employee WHERE dno = 5); /* Can you reformulate the subquery so that ALL is not required in the outer query? What will be the result if we replace > ALL by = ALL? */ LnameFname WallaceJennifer Borg James

23 USE OF ANY or ALL l = ANY is same as IN /* List the employees that have a female dependent Following two queries are equivalent */ SELECT lname, fname FROM Employee WHERE ssn = ANY (SELECT essn FROM Dependent WHERE sex = ‘F’); LnameFname SmithJohn WongFranklin SELECT lname, fname FROM Employee WHERE ssn IN (SELECT essn FROM Dependent WHERE sex = ‘F’);

24 USE OF ANY or ALL l != ANY is not the same as NOT IN u NOT IN (1,2,3) means != 1and != 2 and != 3 u != ANY (1,2,3) means != 1 or !=2 or != 3 l != ALL is same as NOT IN /* List all employees that don’t have a dependent */ SELECT lname, fname FROM Employee WHERE ssn != ALL (SELECT essn FROM Dependent); /* You will get the same result by replacing != ALL by NOT IN What will be the result if you simply replace ALL by ANY in the above query? */ LnameFname ZelayaAlicia NarayanRamesh EnglishJoyce JabbarAhmad BorgJames

25 CORRELATED SUBQUERIES l In correlated subqueries, the inner query depends on values provided by the outer query l The inner query is executed once for each row that might be selected by the outer query /* List all employees who have worked 10 hours on any project */ SELECT lname, fname FROM Employee WHERE 10 IN (SELECT hours FROM Works_on WHERE ssn = essn); /* What is the equivalent join query for the above? LnameFname WongFranklin ZelayaAlicia

26 USE of EXISTS l WHERE [NOT] EXISTS (subquery) l Checks for existence of data - returns TRUE or FALSE l A subquery introduced with EXISTS is always a corelated subquery l The SELECT list in the subquery is invariably *, because there is no need for selecting a column /* List all employees that have one or more dependents */ SELECT lname, fname FROM Employee WHERE EXISTS (SELECT * FROM Dependent WHERE ssn = essn); /* How many times does the subquery execute in the above query? */ LnameFname SmithJohn Wong Franklin WallaceJennifer

27 USE of EXISTS /* List all employees that do not have any dependent */ SELECT lname, fname FROM Employee WHERE NOT EXISTS (SELECT * FROM Dependent WHERE ssn = essn); LnameFname ZelayaAlicia NarayanRamesh EnglishJoyce JabbarAhmad BorgJames

28 SELF TEST l List student name, major and department name for all students l List student name, major, and department name for students that have a GPA > 3.0