DBII_Lab_2 (1)
DBII_Lab_2 (1)
Subqueries in MySQL
• Define subqueries
• List the types of subqueries
• Write single-row and multiple-row subqueries
Using a Subquery to Solve a Problem
• Who has a salary greater than Azad?
Main Query
Subquery
• The result of the subquery is used by the main query (outer query).
SELECT select_list
FROM table
WHERE expr operator
(SELECT select_list
FROM table);
Using a Subquery
• In the following example, the inner query determines the salary of employee 1010.
• The outer query takes the result of the inner query and uses this result to display all
the employees who earn more than this amount.
SQL> SELECT ename
2 FROM emp 4500
3 WHERE sal >
4 (SELECT sal
5 FROM emp
6 WHERE empno=1010);
ENAME
----------
Jwan
Ziyad
Lava
Guidelines for Using Subqueries
• Enclose subqueries in parentheses.
• Multiple-row subquery: Queries that return more than one row from the inner
SELECT statement.
• Multiple-column subquery: Queries that return more than one column from the
inner SELECT statement.
Types of Subqueries
Main query
• Single-row subquery returns
Subquery Assistant Professor
Main query
• Multiple-row subquery returns Assistant Professor
Subquery
Lecturer
Main query
• Multiple-column subquery returns Assistant Professor 4500
Subquery
Lecturer 2000
Single-Row Subqueries
• Return only one row
= Equal to
• Example: Display the employees whose
> Greater than
job title is the same as that of employee >= Greater than or equal to
ENAME JOB
---------- ---------
Azad Chair
Newroz Chair
Haji Chair
…….
Single-Row Subqueries (Example 2)
• The example in the next slide displays employees whose job title is the same as
that of employee 1009 and whose salary is greater than that of employee 1009.
Single-Row Subqueries (Example 2)
SQL> SELECT ename, job
2 FROM emp
3 WHERE job = Chair
4 (SELECT job
5 FROM emp
6 WHERE empno = 1009)
7 AND sal > 4500
8 (SELECT sal
9 FROM emp
10 WHERE empno = 1009);
ENAME JOB
---------- ---------
Azad Chair
Newroz Chair
Haji Chair
Using Group Functions in a Subquery
• Display the employee name, job title, and salary of all employees whose salary is
equal to the minimum salary.
ENAME
----------
Alend 3500
Adam 3500
HAVING Clause with Subqueries
• This query displays all the departments that have a minimum salary greater than that
of department 3 (GE).
ERROR:
single-row subquery returns more than one row
no rows selected
Errors with Subqueries
• One common error with subqueries is more than one row returned for a single-row subquery.
• In the SQL statement on the previous slide, the subquery contains a GROUP BY (deptno) clause, which
implies that the subquery will return multiple rows, one for each group it finds.
• The outer query takes the results of the subquery (multiple rows) and uses these results in its WHERE
clause.
• The WHERE clause contains an equal (=) operator, a single-row comparison operator expecting only
one value.
• The = operator cannot accept more than one value from the subquery and hence generates the error.
• To correct this error, change the = operator to IN.
IN Clause with Subqueries
• Find employees working in departments that have a "Chair" role.
no rows selected
Problems with Subqueries
• A common problem with subqueries is no rows being returned by the inner query.
• In the SQL statement on the slide, the subquery contains a WHERE (ename='Jihan') clause.
• The intention is to find the employee whose name is Jihan.
• The statement seems to be correct but selects no rows when executed.
• The problem is that Jihan is not inserted or misspelled.
• There is no employee named Jihan, So the subquery returns no rows.
• The outer query takes the results of the subquery (null) and uses these results in its WHERE
clause.
• The outer query finds no employee with a job title equal to null and so returns no rows.
Exercises
• Write a query to display the employee name, job, and department name for all employees
in the same department as Lina.
• Write a query to display the employee name, job, and department name for all employees
more than the average salary. Sort the results in descending order of salary.
Multiple-Row Subqueries
• Return more than one row
Operator Meaning
• These operators are typically used with subqueries and are useful when you want to
• ANY returns true if the comparison is true for at least one value in the set.
• ALL returns true if the comparison is true for all values in the set.
Using ANY Operator in Multiple-Row Subqueries
• Display the employees whose salary is less than any Associate Professor and not Associate Professor.