Professional Documents
Culture Documents
Module 2 Lesson 11 - Subqueries-1
Module 2 Lesson 11 - Subqueries-1
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
page
2
CCINFOM
Module 2 Lesson 11: Subqueries
Estimated Time to consume material – 1.5 Hours
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
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?
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