Objectives 2 Ins.Ebtesam AL-Etowi Subqueries Describe the types of problems that subqueries can solve Define subqueries List the types of subqueries Write single-row and multiple-row subqueries.
Using a Subquery to Solve a Problem Ins.Ebtesam AL-Etowi 3 “ Which employees have a salary greater than John salary?” “what is John salary?” To solve this problem, you need two query: First, one query to find what Jones earns and Second query, to find who earns more than that amount
Ins.Ebtesam AL-Etowi 4 Subqueries SELECT select_list FROM table WHERE expr operator (SELECT select_list FROM table) The subquery (inner query) executes once before the main query. The result of the subquery is used by the main query (outer query). Using a Subquery The inner query determines the salary of employee 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
Ins.Ebtesam AL-Etowi 5 SELECT Fname from Employee WHERE salary > (SELECT salary FROM employee WHERE Ssn = ' )
Ins.Ebtesam AL-Etowi 6 Guidelines for Using Subqueries Enclose subqueries in parentheses. Place subqueries on the right side of the comparison operator. Don not add an ORDER BY clause to a subquery. Using single-row operators with single-row subqueries. Use multiple-row operators with multiple-row subqueries Types of Subqueries Single-row subquery Queries that return only one row from the inner SELECT statement 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
Ins.Ebtesam AL-Etowi 7 Single-Row Subqueries Return only one row Use single-row comparison operators. Operator (=, >, >=, ) SELECT Fname, Dno from Employee WHERE Dno= (SELECT Dno FROM employee WHERE Ssn= ' ‘)
Ins.Ebtesam AL-Etowi 8 Executing Single-Row Subqueries Display employee whose depatement number is the same as that employee and whose salary is greater than that of employee The example consists of three query blocks: the outer query and two inner queries. The inner query blocks are executed first. SELECT Fname, Dno from Employee WHERE Dno= (SELECT Dno FROM employee WHERE Ssn= ' ' ) AND salary > (SELECT salary FROM employee WHERE Ssn = ' ')
Ins.Ebtesam AL-Etowi 9
10 Using Group Functions in a Subquery You can display data from a main query by using a group function in a subquery to return a single row. Displays the employee name, department number and salary of all employee whose salary is equal to the minimum salary SELECT Fname, Dno, Salary FROM employee WHERE Salary = (SELECT MIN(Salary) FROM employee)
Ins.Ebtesam AL-Etowi 11 Having Clause with Subqueries The SQL Server executes subqueries first. The SQL Server returns results into the HAVING clause of the main query SELECT Dno, MIN(Salary) FROM employee GROUP BY Dno HAVING MIN(salary) > (SELECT MIN(Salary) FROM employee WHERE Dno = 5)
Ins.Ebtesam AL-Etowi 12 What is Wrong with this Statement SELECT Dno, avg(Salary) FROM employee GROUP BY Dno HAVING avg(salary) > (SELECT avg(Salary) FROM employee GROUP BY Dno) Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=,, >= or when the subquery is used as an expression.
Multiple-Row Subqueries Ins.Ebtesam AL-Etowi 13 Return more than one row Use Multiple-row comparison operators MeaningOperator Equal to any member in the list IN Compare value to each value returned by the subquery ANY Compare value to every value returned by the subquery ALL SELECT Fname, Dno, Salary FROM employee WHERE Salary IN (SELECT MIN(Salary) FROM employee GROUP BY Dno)
Ins.Ebtesam AL-Etowi 14 Find the employee salary in (30000, 5000, 2500), first name, and departement number SELECT Fname, Dno, Salary FROM employee WHERE Salary IN (30000, 25000,2500)
Ins.Ebtesam AL-Etowi 15 Using ANY Operator in Multiple-Row Subqueries SELECT Ssn, Fname, Dno, Salary FROM employee WHERE Salary < ANY (SELECT Salary FROM employee WHERE Dno=4) AND Dno <>4
Ins.Ebtesam AL-Etowi 16 Using ALL Operator in Multiple-Row Subqueries SELECT Ssn, Fname, Dno, Salary FROM employee WHERE Salary < ALL (SELECT AVG(Salary) FROM employee GROUP BY Dno)
17 Ins.Ebtesam AL-Etowi
Exercises Ins.Ebtesam AL-Etowi 18 Write a query to display the first name employee and birthdates for all employee in the same department as create a query to display the Ssn and Fname for all employees who salary more than the average salary. Sort the results in descending order of salary. 3-Write a query that will display the employee number and name for all employees who work in a department with any employee whose name contains a o. 4-Display the Fname and salary of all employee who supervisor ‘ ’ 5- Display the Dno, Fname, and salary for all emplyee in the 5 Departement number.