SELECT statement (2)

Outline

Queries with simple conditions

  • 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

Queries with simple conditions

  • Find the titles of all 6 credit points courses
      
      SELECT title
      FROM COURSE
      WHERE credits = 6;
  • Find the titles of all 6 or 12 credit points courses
      
      SELECT title
      FROM COURSE
      WHERE credits IN (6, 12);
  • Find the titles and numbers of all courses that have a word "database" in its title
      
      SELECT title, cnum
      FROM COURSE
      WHERE UPPER(title) LIKE '%DATABASE%';
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 (2)

Outline

Queries with Boolean expressions

  • Find the titles of all 6 credit points courses offered by a department of Physics
      
      SELECT title
      FROM COURSE
      WHERE (credits = 6) AND (offered_by ='Physics');
  • Find the titles of all 6 credit points courses or the titles of all courses offered by a department of Physics
      
      SELECT title
      FROM COURSE
      WHERE (credits = 6) OR (offered_by ='Physics');
  • Find the titles of all 6 credit points courses that are not offered by a department of Physics
      
      SELECT title
      FROM COURSE
      WHERE NOT (offered_by = 'Physics') AND (credits = 6);
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Queries with Boolean expressions

  • Find the titles of all 6 credit points courses that are not offered by a department of Physics
      
      SELECT title
      FROM COURSE
      WHERE (offered_by != 'Physics') AND (credits = 6);
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Queries with Boolean expressions

  • Find the titles of all courses offered by a department of Physics or offered by a department of Mathematics
      
      SELECT title
      FROM COURSE
      WHERE (offered_by = 'Physics') OR (offered_by = 'Mathematics');
      
      SELECT title
      FROM COURSE
      WHERE offered_by IN ('Physics', 'Mathematics');
  • Find the same titles of all courses offered by a department of Physics and offered by a department of Mathematics
      SELECT title
      FROM COURSE
      WHERE(offered_by = 'Physics') AND (offered_by = 'Mathematics');
    • WRONG !!!
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 (2)

Outline

Set algebra queries

  • Set operations: UNION, INTERSECTION, and DIFFERENCE

    SET OPERATIONS

  • A UNION B: all elements in A or in B
  • A INTERSECTION B: all elements in A and in B
  • A DIFFERENCE B: all elements in A and not in B
  • B DIFFERENCE A: all elements in B and not in A
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Set algebra queries

  • UNION operation
  • Find the credit points of courses offered by a department of Physics or a department of Mathematics and do not list duplicated rows
      
      SELECT DISTINCT credits
      FROM COURSE
      WHERE (offered_by = 'Physics') OR (offered_by = 'Mathematics');
      
      SELECT credits
      FROM COURSE
      WHERE offered_by = 'Physics'
      UNION
      SELECT credits
      FROM COURSE
      WHERE offered_by = 'Mathematics';
  • UNION operation automatically eliminates duplicated rows !!!
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Set algebra queries

  • UNION ALL operation
  • Find the credit points of courses offered by a department of Physics or a department of Mathematics
      
      SELECT credits
      FROM COURSE
      WHERE (offered_by = 'Physics') OR (offered_by = 'Mathematics');
      
      SELECT credits
      FROM COURSE
      WHERE offered_by = 'Physics'
      UNION ALL
      SELECT credits
      FROM COURSE
      WHERE offered_by = 'Mathematics';
  • UNION operation does NOT eliminate duplicated rows !!!
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Set algebra queries

  • What about other set operations like intersection (INTERSECT) and difference (MINUS) ?
  • MySQL does not implement INTERSECT and MINUS set operations :( !!!
  • So, how do we implement in MySQL queries with INTERSECT and MINUS set operations ?
  • Find the credit points that can be earned both for a course offered by Physics and for a course offered by Mathematics
      
      SELECT DISTINCT credits
      FROM COURSE
      WHERE offered_by = 'Physics' AND
            credits IN (SELECT credits
                        FROM COURSE
                        WHERE offered_by = 'Mathematics');
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Set algebra queries

  • What about other set operations like intersection (INTERSECT) and difference (MINUS) ?
  • MySQL does not implement INTERSECT and MINUS set operations :( !!!
  • So, how do we implement in MySQL queries with INTERSECT and MINUS set operations ?
  • Find the credit points that can be earned for a course offered by Physics and cannot be earned for a course offered by Mathematics
      
      SELECT DISTINCT credits
      FROM COURSE
      WHERE offered_by = 'Physics' AND
            credits NOT IN (SELECT credits
                            FROM COURSE
                            WHERE offered_by = 'Mathematics');
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 (2)

Outline

Sorting

  • Sorting re-orders the rows in the results if a query
  • SORTING

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

Sorting

  • Find the numbers and titles of all 6 credit points courses sorted in the ascending order of titles
      
      SELECT cnum, title
      FROM COURSE
      WHERE credits = 6
      ORDER BY title ASC;
      
      SELECT cnum, title
      FROM COURSE
      WHERE credits = 6
      ORDER BY 2 ASC;
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Sorting

  • Find the numbers and titles of all 6 credit point courses sorted in a descending order of titles
      
      SELECT cnum, title
      FROM COURSE
      WHERE credits = 6
      ORDER BY title DESC;
      
      SELECT cnum, title
      FROM COURSE
      WHERE credits = 6
      ORDER BY 2 DESC;
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Sorting

  • Find the numbers, titles, and credits of all courses sorted in an ascending order of credits and for all courses with the same credits sorted in descending order by titles
      
      	      
      SELECT cnum, title, credits
      FROM COURSE
      ORDER BY credits ASC, title DESC;
      
      	      
      SELECT cnum, title, credits
      FROM COURSE
      ORDER BY 3 ASC, 2 DESC;
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 (2)

Outline

Queries about lack of values (NULLs)

  • Find the titles of all courses which are not offered now
      
      SELECT title
      FROM COURSE
      WHERE offered_by IS NULL;
      
      SELECT title
      FROM COURSE
      WHERE offered_by = NULL;
    • WRONG !!! Lack of value cannot be compared with a value
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Queries about lack of values (NULLs)

  • Find the titles of all courses offered now
      
      SELECT title
      FROM COURSE
      WHERE offered_by IS NOT NULL;
      
      SELECT title
      FROM COURSE
      WHERE offered_by <> NULL;
    • WRONG !!! Lack of value cannot be compared with a value
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 (2)

Outline

Grouping

  • Grouping groups the rows before application of aggregation function
  • GROUPING

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

Grouping

  • Next, aggregation function is applied to each group and the results are associated with the values taken from a column used for grouping
  • GROUPING

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

Grouping

  • Aggregation function can be applied to an empty group of values
  • GROUPING

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

Grouping

  • Find the names of all departments that offer at least one course, do not list the same names of departments
      SELECT DISTINCT offered_by
      FROM COURSE;
      SELECT offered_by
      FROM COURSE
      GROUP BY offered_by;
  • GROUP BY sorts a relational table over the attributes included in GROUP BY list and creates the groups of rows with identical values of the attributes in GROUP BY list
  • In the example above a table COURSE is sorted over the values of an attribute offered_by
  • The groups of rows with the identical values of an attribute offered_by are created
  • Finally, the values of an attribute offered_by are selected, one value from each group
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Grouping

  • Grouping allows for application of group functions to each group created by GROUP BY clause
  • Find the names of all departments together with the total number of all courses offered by each department
      
      SELECT offered_by, COUNT(*)
      FROM COURSE
      GROUP BY offered_by;
    • Before sorting
    • 
      +---------+------------+---------+------------------+
      | cnum    | title      | credits | offered_by       |
      +---------+------------+---------+------------------+
      | CSCI111 | C++        |       6 | Computer Science |
      | PHYS312 | Relativity |       6 | Physics          |
      | MATH111 | Calculus   |      12 | Mathematics      |
      | CSCI235 | Databases  |       6 | Computer Science |
      | MECH111 | Mechanics  |      12 | Physics          |
      +---------+------------+---------+------------------+
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Grouping

  • Grouping allows for application of group functions to each group created by GROUP BY clause
  • Find the names of all departments together with the total number of all courses offered by each department
      
      SELECT offered_by, COUNT(*)
      FROM COURSE
      GROUP BY offered_by;
    • After grouping by offered_by
    • 
      +---------+------------+---------+------------------+
      | cnum    | title      | credits | offered_by       |
      +---------+------------+---------+------------------+
      | CSCI111 | C++        |       6 | Computer Science |
      | CSCI235 | Databases  |       6 | Computer Science |
      | MATH111 | Calculus   |      12 | Mathematics      |
      | MECH111 | Mechanics  |      12 | Physics          |
      | PHYS312 | Relativity |       6 | Physics          |
      +---------+------------+---------+------------------+
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Grouping

  • Grouping allows for application of group functions to each group created by GROUP BY clause
  • Find the names of all departments together with the total number of all courses offered by each department
      
      SELECT offered_by, COUNT(*)
      FROM COURSE
      GROUP BY offered_by;
    • After counting rows in each group
    • 
      +------------------+----------+
      | offered_by       | count(*) |
      +------------------+----------+
      | Computer Science |        2 |
      | Mathematics      |        1 |
      | Physics          |        2 |
      +------------------+----------+
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Grouping

  • For each value of credit points find the total number of courses that have the respective credits
      
      SELECT credits, COUNT(*)
      FROM COURSE
      GROUP BY credits;
  • For each department find the total number of credit points offered
      
      SELECT offered_by, SUM(credits)
      FROM COURSE
      GROUP BY offered_by;
  • Find the largest number of courses offered by any department
      
      SELECT MAX(total)
      FROM (SELECT COUNT(*) total
            FROM COURSE
            GROUP BY offered_by) T;
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 (2)

Outline

Grouping with selections

  • Find the names of all departments that offer more than 1 course
      
      SELECT offered_by, COUNT(*)
      FROM COURSE
      GROUP BY offered_by;
      
      +------------------+----------+
      | offered_by       | count(*) |
      +------------------+----------+
      | Computer Science |        2 |
      | Mathematics      |        1 |
      | Physics          |        2 |
      +------------------+----------+
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

Grouping with selections

  • Find the names of all departments that offer more than 1 course
      
      SELECT offered_by, count(*)
      FROM COURSE
      GROUP BY offered_by
      HAVING count(*) > 1;
      
      +------------------+----------+
      | offered_by       | count(*) |
      +------------------+----------+
      | Computer Science |        2 |
      | Physics          |        2 |
      +------------------+----------+
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