Database cs453 Lab8 Ins.Ebtesam AL-Etowi 1 Objectives Subqueries • Describe the types of problems that subqueries can solve • Define subqueries • List the types of subqueries • Write single-row and multiple-row subqueries. Ins.Ebtesam AL-Etowi 2 Using a Subquery to Solve a Problem “ 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 3 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 123456789. 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 4 SELECT Fname from Employee WHERE salary > (SELECT salary FROM employee WHERE Ssn = '123456789) Ins.Ebtesam AL-Etowi 5 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 Ins.Ebtesam AL-Etowi SELECT statement 6 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= '888665555‘) Ins.Ebtesam AL-Etowi 7 Executing Single-Row Subqueries Display employee whose depatement number is the same as that employee 888665555 and whose salary is greater than that of employee 987654321. 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= '888665555' ) AND salary > (SELECT salary FROM employee WHERE Ssn = '987654321') Ins.Ebtesam AL-Etowi 8 Ins.Ebtesam AL-Etowi 9 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 10 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 11 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) Ins.Ebtesam AL-Etowi Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. 12 Multiple-Row Subqueries Return more than one row Use Multiple-row comparison operators Operator Meaning IN Equal to any member in the list ANY Compare value to each value returned by the subquery ALL Compare value to every value returned by the subquery SELECT Fname, Dno, Salary FROM employee WHERE Salary IN (SELECT MIN(Salary) FROM employee GROUP BY Dno) Ins.Ebtesam AL-Etowi 13 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 14 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 15 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) Ins.Ebtesam AL-Etowi 16 Ins.Ebtesam AL-Etowi 17 Exercises Write a query to display the first name employee and birthdates for all employee in the same department as 4. 2- 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 ‘333445555’ 5- Display the Dno, Fname, and salary for all emplyee in the 5 Departement number. Ins.Ebtesam AL-Etowi 18