SELECT statement (5)

Outline

Nested queries

  • Nested query is a query with another query embedded in WHERE or SELECT or FROM clauses
  • NESTED

In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Nested queries

  • Nested query is a query with another query embedded in WHERE or SELECT or FROM clauses
  • NESTED

In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Nested queries

  • Sample database
      CREATE TABLE DEPARTMENT(
       name               VARCHAR(50)        NOT NULL,
       code               CHAR(5)            NOT NULL,
       total_staff_number DECIMAL(2)         NOT NULL,
       chair              VARCHAR(50)            NULL,
       budget             DECIMAL(9,1)       NOT NULL,
        CONSTRAINT dept_pkey PRIMARY KEY(name),
        CONSTRAINT dept_ckey1 UNIQUE(code),
        CONSTRAINT dept_ckey2 UNIQUE(chair),
        CONSTRAINT dept_check1 CHECK (total_staff_number BETWEEN 1 AND 50) );
      CREATE TABLE COURSE(
       cnum               CHAR(7)           NOT NULL,
       title              VARCHAR(200)      NOT NULL,
       credits            DECIMAL(2)        NOT NULL,
       offered_by         VARCHAR(50)           NULL,
        CONSTRAINT course_pkey PRIMARY KEY(cnum),
        CONSTRAINT course_check1 CHECK (credits IN (6, 12)),
        CONSTRAINT course_fkey1 FOREIGN KEY(offered_by)
                              REFERENCES DEPARTMENT(name) ON DELETE CASCADE );
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Nested queries

  • A class of nested queries is based on a concept of inline views
  • Inline views can be used to reduce the complexity of query implementation
  • An inner query is created first and its outcomes are used as a relational table (inline view) in an outer query
  • For example, a query find the titles of courses offered by a department chaired by Peter is decomposed into the following two queries:
    • Q1: Find a department chaired by Peter
    • Q2: Find the titles of courses offered by a department found in Q1
  • A query Q1 is implemented first and then it is used in WHERE clause of query Q2
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Nested queries

  • In nested queries SELECT statements are nested to theoretically unlimited level in WHERE clause
      
      SELECT title
      FROM COURSE
      WHERE offered_by IN ( SELECT name
                            FROM DEPARTMENT
                            WHERE chair = 'Peter' );
  • If an inner query returns more than one row then we must use IN istead of =
  • However, it does not mean that we can always use IN instead of =
  • If an inner query returns zero or at most one row then using = instead of IN allows a query processor to find more efficient query processing plan
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Nested queries

  • Implementation of a query find the titles of courses offered by a department chaired by Peter consists of the following steps
  • Create a inner query as inline view Q
      ( SELECT name
      FROM DEPARTMENT
      WHERE chair = 'Peter' ) Q
  • Create a temporary outer query, that references an inline view Q created earlier
      SELECT title
      FROM COURSE
      WHERE offered_by IN Q.name
  • Replace a reference Q to an inline view with the inline view itself
      SELECT title
      FROM COURSE
      WHERE offered_by IN ( SELECT name
                            FROM DEPARTMENT
                            WHERE chair = 'Peter' );
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Nested queries

  • Another example, find the chairs of all departments, that offer 12 credit point courses
  • Create an inner query as inline view Q
      ( SELECT offered_by
      FROM COURSE
      WHERE credits = 12) Q;
  • Next, create an intermediate from of outer query, that references an inline view Q created earlier
      SELECT chair
      FROM DEPARTMENT
      WHERE name IN Q.offered_by
  • Replace a reference to an inline view Q with the inline view itself
      SELECT chair
      FROM DEPARTMENT
      WHERE name IN ( SELECT offered_by
                      FROM COURSE
                      WHERE credits = 12 );
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Nested queries

  • A query find the chairs of all departments that offer no courses is an example of ANTIJOIN operation
  • It is equivalent to a query find all rows in a relational table DEPARTMENT that cannot be joined with any row in a relational table COURSE
  • An inner query is implemented as an inline view finds all departments, that offer at least one course
      
      ( SELECT offered_by
      FROM COURSE ) Q;
  • An outer query finds all chairs of departments that are NOT included in the results of an inner query
      
      SELECT chair
      FROM DEPARTMENT
      WHERE name NOT IN Q.offered_by
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Nested queries

  • Finally, we replace a reference to an inline view Q with the inline view itself
      
      SELECT chair
      FROM DEPARTMENT
      WHERE name NOT IN ( SELECT offered_by
                          FROM COURSE );
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

SELECT statement (5)

Outline

Correlated nested queries

  • In a correlated nested query an inline view may reference the names of relational tables used in SELECT statements outer to the inline view
  • NESTED

In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Correlated nested queries

  • In a correlated nested query an inline view may reference the names of relational tables used in SELECT statemens outer to the inline view
  • NESTED

In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Correlated nested queries

  • In a correlated nested query an inline view may reference the names of relational tables used in SELECT statements outer to the inline view
  • NESTED

In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Correlated nested queries

  • For example, consider a query, that finds the chairs of departments, that offer 12 credit point courses
  • Such query can be expressed as an equivalent query, that finds the chairs of departments, such that there exists at least one course worth 12 credit points offered by a department we are looking for
  • An inner query ... course worth 12 credit points offered by a department we are looking for is implemented as the following inline view Q
      ( SELECT *
      FROM COURSE
      WHERE credits = 12 AND offered_by = DEPARTMENT.name ) Q
  • A temporary outer query, that finds the chairs of departments such that there exists at least one course found in the inner quer is implemented in the folowing way
      
      SELECT chair
      FROM DEPARTMENT
      WHERE EXISTS Q
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Correlated nested queries

  • Finally, we replace a reference to an inline view Q with the inline view itself
      
      SELECT chair
      FROM DEPARTMENT
      WHERE EXISTS ( SELECT *
                     FROM COURSE
                     WHERE credits = 12 AND offered_by = DEPARTMENT.name );
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Correlated nested queries

  • In a correlated nested query an inline view may reference the names of relational tables used in SELECT statement outer to the inline view
  • NESTED

In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Correlated nested queries

  • Another example where inline view references a name of relational table used in SELECT statement outer to the inline view is the following
  • Find the chairs of all departments, that offer no courses
  • It is equivalent to a query find the chairs of departments, such that does not exist a course offered by a department we are looking for
  • An inner query finds ... a course offered by a department we are looking for and it is implemented as an inline view Q
      ( SELECT *
      FROM COURSE
      WHERE offered_by = DEPARTMENT.name ) Q;
  • A temporary outer query finds all chairs of deparments such that does not exist a course found by an inner query
      
      SELECT chair
      FROM DEPARTMENT
      WHERE NOT EXISTS Q
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Correlated nested queries

  • Finally, we replace a reference to an inline view Q with the inline view itself
      
      SELECT chair
      FROM DEPARTMENT
      WHERE NOT EXISTS( SELECT *
                        FROM COURSE
                        WHERE offered_by = DEPARTMENT.name );
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

References

TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023