Upload
richard-bruce
View
233
Download
0
Embed Size (px)
Citation preview
DatabaseDatabase
cs453
Lab8
1Ins.Ebtesam AL-Etowi
Objectives
2Ins.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 andSecond query, to find who earns more than that amount
Ins.Ebtesam AL-Etowi 4
SubqueriesSELECT select_listFROM tableWHERE 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 5
SELECT Fnamefrom Employee
WHERE salary> ( SELECT salary
FROM employee WHERE Ssn = '123456789)
Ins.Ebtesam AL-Etowi 6
Guidelines for Using SubqueriesEnclose 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 rowUse single-row comparison operators.
Operator)>< ,=< ,< ,=> , > , =(
SELECT Fname, Dnofrom Employee
WHERE Dno=( SELECT Dno
FROM employee WHERE Ssn= '888665555)‘
Ins.Ebtesam AL-Etowi 8
Executing Single-Row SubqueriesDisplay 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, Dnofrom Employee
WHERE Dno=( SELECT Dno
FROM employee WHERE Ssn= '888665555) '
AND salary> ( SELECT salary
FROM employee WHERE Ssn = '987654321)'
Ins.Ebtesam AL-Etowi 9
Ins.Ebtesam AL-Etowi 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, SalaryFROM 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 employeeGROUP 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 employeeGROUP 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 rowUse Multiple-row comparison operators
OperatorMeaning
IN Equal to any member in the list
ANYCompare value to each value returned by the subquery
ALLCompare value to every value returned by the subquery
SELECT Fname, Dno, SalaryFROM employeeWHERE 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, SalaryFROM employeeWHERE Salary IN (30000, 25000,2500)
Ins.Ebtesam AL-Etowi 15
Using ANY Operator in Multiple-Row Subqueries
SELECT Ssn, Fname, Dno, SalaryFROM employeeWHERE 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, SalaryFROM employeeWHERE Salary < ALL
( SELECT AVG(Salary) FROM employee
GROUP BY Dno)
17Ins.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 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.