MySQL Subquery - Exercises, Practice, Solution
MySQL Subquery - Exercises, Practice, Solution
2. Write a query to find the name (first_name, last_name) of all employees who
works in the IT department.
3. Write a query to find the name (first_name, last_name) of the employees who
have a manager and worked in a USA based department.
4. Write a query to find the name (first_name, last_name) of the employees who
are managers.
5. Write a query to find the name (first_name, last_name), and salary of the
employees whose salary is greater than the average salary.
6. Write a query to find the name (first_name, last_name), and salary of the
employees whose salary is equal to the minimum salary for their job grade.
ACCENTURE BATCH 2 LABORATORY
7. Write a query to find the name (first_name, last_name), and salary of the
employees who earns more than the average salary and works in any of the IT
departments.
8. Write a query to find the name (first_name, last_name), and salary of the
employees who earns more than the earning of Mr. Bell.
9. Write a query to find the name (first_name, last_name), and salary of the
employees who earn the same salary as the minimum salary for all departments.
10. Write a query to find the name (first_name, last_name), and salary of the
employees whose salary is greater than the average salary of all departments.
11. Write a query to find the name (first_name, last_name) and salary of the
employees who earn a salary that is higher than the salary of all the Shipping
Clerk (JOB_ID = 'SH_CLERK'). Sort the results of the salary of the lowest to
highest.
Page | 3
Sample table: employees
12. Write a query to find the name (first_name, last_name) of the employees who
are not supervisors.
13. Write a query to display the employee ID, first name, last name, and
department names of all employees.
14. Write a query to display the employee ID, first name, last name, salary of all
employees whose salary is above average for their departments.
15. Write a query to fetch even numbered records from employees table.
16. Write a query to find the 5th maximum salary in the employees table.
17. Write a query to find the 4th minimum salary in the employees table.
Page | 4 19. Write a query to list the department ID and name of all the departments
where no employee is working.
Page | 5
ACCENTURE BATCH 2 LABORATORY
SOLUTION
Code:
SELECT FIRST_NAME, LAST_NAME, SALARY
FROM employees
Copy
Explanation :
- The subquery (inner query) executes once before the main query (outer query)
executes.
- The main query (outer query) use the subquery result.
ACCENTURE BATCH 2 LABORATORY
Page | 7
Write a query to find the name (first_name, last_name) of all employees who
Page | 8 works in the IT department.
FROM employees
WHERE department_id
Copy
Explanation :
Copy
-> 0
-> 1
Copy
When used with a subquery, the word IN is an alias for = ANY. Thus, these two
statements are the same:
SELECT s1 FROM t1 WHERE s1 = ANY (SELECT s1 FROM t2);
Copy
IN and = ANY are not synonyms when used with an expression list. IN can take
an expression list, but = ANY cannot.
ACCENTURE BATCH 2 LABORATORY
Page | 17
ACCENTURE BATCH 2 LABORATORY
Write a query to find the name (first_name, last_name) of the employees who
Page | 18 have a manager and worked in a USA based department.
Code:
SELECT first_name, last_name FROM employees
Copy
Explanation:
Copy
For example :
Page | 19
mysql> SELECT 2 IN (0,3,5,7);
-> 0
-> 1
Copy
When used with a subquery, the word IN is an alias for = ANY. Thus, these two
statements are the same:
SELECT s1 FROM t1 WHERE s1 = ANY (SELECT s1 FROM t2);
Copy
IN and = ANY are not synonyms when used with an expression list. IN can take
an expression list, but = ANY cannot.
ACCENTURE BATCH 2 LABORATORY
Page | 20
ACCENTURE BATCH 2 LABORATORY
Write a query to find the name (first_name, last_name) of the employees who are
Page | 21 managers.
Code:
SELECT first_name, last_name
FROM employees
Copy
Explanation:
Copy
For example :
mysql> SELECT 2 IN (0,3,5,7);
-> 0
-> 1
ACCENTURE BATCH 2 LABORATORY
Copy
When used with a subquery, the word IN is an alias for = ANY. Thus, these two
statements are the same:
Page | 22 SELECT s1 FROM t1 WHERE s1 = ANY (SELECT s1 FROM t2);
Copy
IN and = ANY are not synonyms when used with an expression list. IN can take
an expression list, but = ANY cannot.
ACCENTURE BATCH 2 LABORATORY
Write a query to find the name (first_name, last_name), and salary of the
Page | 23 employees whose salary is greater than the average salary.
Code:
SELECT first_name, last_name, salary FROM employees
Copy
Explanation:
- The subquery (inner query) executes once before the main query (outer query)
executes.
- The main query (outer query) use the subquery result.
ACCENTURE BATCH 2 LABORATORY
Page | 24
Write a query to find the name (first_name, last_name), and salary of the
Page | 25 employees whose salary is equal to the minimum salary for their job grade.
Code:
SELECT first_name, last_name, salary
FROM employees
FROM jobs
Copy
Explanation:
- The subquery (inner query) executes once before the main query (outer query)
executes.
- The main query (outer query) use the subquery result.
Page | 26
ACCENTURE BATCH 2 LABORATORY
Write a query to find the name (first_name, last_name), and salary of the
Page | 27 employees who earns more than the average salary and works in any of the IT
departments.
Code:
SELECT first_name, last_name, salary
FROM employees
WHERE department_id IN
Copy
Explanation:
Page | 28
- The subquery (inner query) executes once before the main query (outer query)
executes.
- The main query (outer query) use the subquery result.
ACCENTURE BATCH 2 LABORATORY
Page | 29
ACCENTURE BATCH 2 LABORATORY
Write a query to find the name (first_name, last_name), and salary of the
Page | 30 employees who earns more than the employee whose last name is Bell.
Code:
SELECT first_name, last_name, salary
FROM employees
Copy
Explanation:
- The subquery (inner query) executes once before the main query (outer query)
executes.
- The main query (outer query) use the subquery result.
Page | 31
ACCENTURE BATCH 2 LABORATORY
Write a query to find the name (first_name, last_name), and salary of the
Page | 32 employees who earn the same salary as the minimum salary for all departments.
Code:
SELECT * FROM employees
Copy
Explanation:
- The subquery (inner query) executes once before the main query (outer query)
executes.
- The main query (outer query) use the subquery result.
Write a query to find the name (first_name, last_name), and salary of the
Page | 33 employees whose salary is greater than the average salary of all departments.
Code:
SELECT * FROM employees
Copy
Explanation:
Syntax :
operand comparison_operator ALL (subquery)
The word ALL, which must follow a comparison operator, means "return TRUE if
the comparison is TRUE for ALL of the values in the column that the subquery
returns." For example:
SELECT s1 FROM t1 WHERE s1 > ALL (SELECT s1 FROM t2);
Copy
Suppose that there is a row in table t1 containing (10). The expression is TRUE if
table t2 contains (-5,0,+5) because 10 is greater than all three values in t2. The
expression is FALSE if table t2 contains (12,6,NULL,-100) because there is a
single value 12 in table t2 that is greater than 10. The expression is unknown
(that is, NULL) if table t2 contains (0,NULL,1).
ACCENTURE BATCH 2 LABORATORY
Finally, the expression is TRUE if table t2 is empty. So, the following expression
is TRUE when table t2 is empty :
SELECT * FROM t1 WHERE 1 > ALL (SELECT s1 FROM t2);
Page | 34 Copy
Write a query to find the name (first_name, last_name) and salary of the
Page | 35 employees who earn a salary that is higher than the salary of all the Shipping
Clerk (JOB_ID = 'SH_CLERK'). Sort the results of the salary of the lowest to
highest.
Code:
SELECT first_name,last_name, job_id, salary
FROM employees
Copy
Explanation :
Syntax :
operand comparison_operator ALL (subquery)
The word ALL, which must follow a comparison operator, means "return TRUE if
the comparison is TRUE for ALL of the values in the column that the subquery
returns." For example:
SELECT s1 FROM t1 WHERE s1 > ALL (SELECT s1 FROM t2);
Copy
Suppose that there is a row in table t1 containing (10). The expression is TRUE if
table t2 contains (-5,0,+5) because 10 is greater than all three values in t2. The
expression is FALSE if table t2 contains (12,6,NULL,-100) because there is a
ACCENTURE BATCH 2 LABORATORY
single value 12 in table t2 that is greater than 10. The expression is unknown
(that is, NULL) if table t2 contains (0,NULL,1).
Finally, the expression is TRUE if table t2 is empty. So, the following expression
Page | 36 is TRUE when table t2 is empty :
Copy
ACCENTURE BATCH 2 LABORATORY
Write a query to find the name (first_name, last_name) of the employees who are
Page | 37 not supervisors.
Code:
SELECT b.first_name,b.last_name
FROM employees b
Copy
Explanation :
If a subquery returns any rows at all, EXISTS subquery is TRUE, and NOT
EXISTS subquery is FALSE. For example :
SELECT column1 FROM t1 WHERE EXISTS (SELECT * FROM t2);
Traditionally, an EXISTS subquery starts with SELECT *, but it could begin with
SELECT 5 or SELECT column1 or anything at all. MySQL ignores the SELECT
list in such a subquery, so it makes no difference.
ACCENTURE BATCH 2 LABORATORY
Page | 38
ACCENTURE BATCH 2 LABORATORY
Write a query to display the employee ID, first name, last name, and department
Page | 39 names of all employees.
Code:
SELECT employee_id, first_name, last_name,
Copy
Explanation:
- The subquery (inner query) executes once before the main query (outer query)
executes.
- The main query (outer query) use the subquery result.
Page | 40
ACCENTURE BATCH 2 LABORATORY
Write a query to display the employee ID, first name, last name, salary of all
Page | 41 employees whose salary is above average for their departments.
Code:
SELECT employee_id, first_name
FROM employees AS A
Copy
Explanation:
- The subquery (inner query) executes once before the main query (outer query)
executes.
- The main query (outer query) use the subquery result.
Page | 42
Code:
SET @i = 0;
SELECT i, employee_id
a WHERE MOD(a.i, 2) = 0;
Copy
Write a query to find the 5th maximum salary in the employees table.
Page | 44
Sample table: employees
Code:
SELECT DISTINCT salary
FROM employees e1
FROM employees e2
Copy
Explanation:
- The subquery (inner query) executes once before the main query (outer query)
executes.
- The main query (outer query) use the subquery result.
ACCENTURE BATCH 2 LABORATORY
Page | 45
ACCENTURE BATCH 2 LABORATORY
Write a query to find the 4th minimum salary in the employees table.
Page | 46
Sample table: employees
Code:
SELECT DISTINCT salary
FROM employees e1
FROM employees e2
Copy
Explanation :
- The subquery (inner query) executes once before the main query (outer query)
executes.
- The main query (outer query) use the subquery result.
ACCENTURE BATCH 2 LABORATORY
Page | 47
ACCENTURE BATCH 2 LABORATORY
Code:
SELECT * FROM (
Copy
Explanation:
- The subquery (inner query) executes once before the main query (outer query)
executes.
- The main query (outer query) use the subquery result.
ACCENTURE BATCH 2 LABORATORY
Page | 49
ACCENTURE BATCH 2 LABORATORY
Write a query to list the department ID and name of all the departments where no
Page | 50 employee is working.
Code:
SELECT * FROM departments
WHERE department_id
Copy
Explanation:
- The subquery (inner query) executes once before the main query (outer query)
executes.
- The main query (outer query) use the subquery result.
ACCENTURE BATCH 2 LABORATORY
Page | 51
ACCENTURE BATCH 2 LABORATORY
Code:
SELECT DISTINCT salary
FROM employees a
FROM employees b
Copy
Explanation :
- The subquery (inner query) executes once before the main query (outer query)
executes.
- The main query (outer query) use the subquery result.
ACCENTURE BATCH 2 LABORATORY
Page | 53
ACCENTURE BATCH 2 LABORATORY
Code:
SELECT DISTINCT salary
FROM employees a
FROM employees b
Copy
Explanation :
- The subquery (inner query) executes once before the main query (outer query)
executes.
- The main query (outer query) use the subquery result.
ACCENTURE BATCH 2 LABORATORY
Page | 55
ACCENTURE BATCH 2 LABORATORY
Code:
SELECT *
WHERE (1) = (
SELECT COUNT(DISTINCT(emp2.salary))
Copy
Explanation:
- The subquery (inner query) executes once before the main query (outer query)
executes.
- The main query (outer query) use the subquery result.
ACCENTURE BATCH 2 LABORATORY
Page | 57
ACCENTURE BATCH 2 LABORATORY
Page | 58