Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

CCINFOM

Module 2 Lesson 11: Subqueries


Estimated Time to consume material – 1.5 Hours

Requirements for this lesson


1. Completed the review of Lessons 1-10 of Module 2
2. Installed MYSQL and MYSQL Workbench
3. Downloaded MYSQL Script File (dbprojects.sql)

Lesson 11: Subqueries

You can create the EER Model from the database through the Home Page of MySQL Workbench:

Follow the step-by-step instruction that will appear on screen until the creation of the model for DBPROJECTS is complete.

page
1
CCINFOM
Module 2 Lesson 11: Subqueries
Estimated Time to consume material – 1.5 Hours

The SQL statement below will get the departments that actually ordered in 2010. Without DISTINCT, you will produce a list of departments with
redundancies.

SELECT DISTINCT(DNAME)
FROM ORDERS
WHERE YEAR(DATEPROC) = 2010

What if I want to get the departments that did not order in 2010?
Will simple query (query on a single table) be able to solve it? You may be thinking of doing this:

SELECT DNAME
FROM ORDERS
WHERE YEAR(DATEPROC) <> 2010

But you will not get the right answer. By investigation, you should be getting CT Department.
If we attempt to join Department and Orders as shown below, we are still not getting the right answer:

SELECT D.DNAME
FROM DEPARTMENT D JOIN ORDERS O
ON D.DNAME=O.DNAME
WHERE YEAR(DATEPROC) = 2010

The query problem: Get the departments that did not order in 2010 is a kind of query that could be answered by doing a list comparison. And the only
way to do that is to use a sub-query.

Try this:

SELECT DNAME
FROM DEPARTMENT
WHERE DNAME NOT IN ( SELECT DISTINCT(DNAME)
FROM ORDERS
WHERE YEAR(DATEPROC) = 2010 );

Remember that earlier the statement in RED produced a list of departments that ordered in 2010.
The statement in BLUE produces a list of departments. So what do you think you will get from the list of departments that are not in the list of
departments that ordered in 2010? Is it not the departments that did not order in 2010? Try the statement

Let us have another example:


Get the list of employees (Name and Department they belong to) who were never part of any project.
(You should be getting CHU, ALLAN ST as the answer)
1. Definitely, a single table query will not do since the (name and department) is found in EMPLOYEE table and employees working on a project is
found in the EMPLOYEE_PROJECT table.
2. Try joining the two tables together?

SELECT E.NAME, E.DNAME


FROM EMPLOYEE E JOIN EMPLOYEE_PROJECT EP
ON E.ENUM=EP.ENUM

Or attempt (this is worse)

SELECT E.NAME, E.DNAME


FROM EMPLOYEE E JOIN EMPLOYEE_PROJECT EP
ON E.ENUM<>EP.ENUM

page
2
CCINFOM
Module 2 Lesson 11: Subqueries
Estimated Time to consume material – 1.5 Hours

The only way to get this is:

SELECT NAME, DNAME


FROM EMPLOYEE
WHERE ENUM NOT IN ( SELECT ENUM
FROM EMPLOYEE_PROJECT )

The BLUE generates the list of employees. The RED statement produces the list of employees working or worked on projects. The statement above is like
saying, employees that are not in the list of employees working or worked on projects. Should that not give you the right answer?

page
3
CCINFOM
Module 2 Lesson 11: Subqueries
Estimated Time to consume material – 1.5 Hours

Let us have a final example before your exercises on SUB-QUERIES:


The manager of the company suspects that the records of the database were not updated frequently such that records of engineers are not properly
updated. He wanted a list of employees (Employee Number only) that are given the title of engineers but were not in the list of engineers. (You should be
getting employee 8009 as the answer)

SELECT ENUM
FROM EMPLOYEE_JOBS
WHERE JOBTITLE = 'J1'
AND ENUM NOT IN ( SELECT ENUM FROM EMPLOYEE_ENGINEERS )
What if the problem above wants the name and department of the employee and not just the ENUM?

SELECT E.ENUM, E.NAME, E.DNAME


FROM EMPLOYEE_JOBS EJ JOIN EMPLOYEE E
ON E.ENUM=EJ.ENUM
WHERE EJ.JOBTITLE = 'J1'
AND EJ.ENUM NOT IN ( SELECT ENUM FROM EMPLOYEE_ENGINEERS );

page
4
CCINFOM
Module 2 Lesson 11: Subqueries
Estimated Time to consume material – 1.5 Hours

EXERCISE
Let us have an exercise on what we have learned from the lessons above. Using the dbprojects DB, write the SQL Statement that will satisfy each data
requirement below using subqueries. Collect your SQL Statements in an SQL Script file and name the file using your Lastname_Firstname – SQL Lesson
11.sql. You may upload your output in the corresponding practice exercise link posted in AnimoSpace. Answers to the practice exercise will also be posted.

1. Get the employee number, names and departments of employees whose spouse is not working on any projects.
2. Get the vendors (vendor name and address) which were not ordered from by any department.
3. What job title was never occupied by any employee?
4. Which employees (Name and Department) are listed both as secretaries and engineers?
5. Get the names and departments of employees whose spouse is not an engineer nor a secretary but worked on projects.

ADDITIONAL SQL STATEMENTS TO INVESTIGATE: What do you think each of these SQL Statements is getting? Do not just run the statement, try to
understand it.

page
5

You might also like