Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 JOIN SUBQUERY Structured Query Language (SQL) - Part III.

Similar presentations


Presentation on theme: "1 JOIN SUBQUERY Structured Query Language (SQL) - Part III."— Presentation transcript:

1 1 JOIN SUBQUERY Structured Query Language (SQL) - Part III

2 2 JOIN l JOIN allows us to retrieve related data from more than one table l SELECT statement is used for joining tables l Attributes to be selected are listed following the keyword SELECT l Tables participating in the join operation are listed in the FROM clause l The join condition is specified in the WHERE clause u You may use any comparison operator (=, >, =. !=, <>, !>, !<) here, though equality is more commonly used

3 3 JOINING MORE THAN TWO TABLES EMPLOYEE EIDEnameDcodeRankSalary 1001John1130000 1002Joe2240000 1003Jack3240000 1004Jane2130000 1005Jill2345000 1006Jeb3135000 1007Jim1130000 DEPARTMENT DcodeDname 1Accounting 2Finance 3Sales RANKS RankTitleMin SalMax Sal 1Supervisor3000040000 2Jr. Mgr.3500045000 3Manager4000060000 4Asst. Director5000080000 5Director70000100000

4 4 JOIN /* List employee ids and names with their department names */ SELECT eid, ename, dname FROM Employee, Department WHERE Employee.dcode = Department.dcode; EIDEnameDname 1001JohnAccounting 1002JoeFinance 1003JackSales 1004JaneFinance 1005JillFinance 1006JebSales 1007JimAccounting

5 5 JOIN l Qualifying column names in a query u when column names are not unique, they must be qualified by the table names to eliminate ambiguity u when column names are unique, qualifying is not required but may be done for documentation purpose u Example: SELECT eid, ename, dname FROM Employee, Department WHERE Employee.dcode = Department.dcode; /* This statement qualifies each column name by the appropriate table name. Note that here qualification is required only in the WHERE clause */ SELECT Employee.eid, Employee.ename, Department.dname FROM Employee, Department WHERE Employee.dcode = Department.dcode;

6 6 JOIN l Using aliases u an alias assigns a unique identifier to the table in a query u the alias must follow the table name u use a space to separate the table name from its alias u Example: SELECT Employee.eid, Employee.ename, Department.dname FROM Employee, Department WHERE Employee.dcode = Department.dcode; /* This statement uses table name aliases. */ SELECT e.eid, e.ename, d.dname FROM Employee e, Department d WHERE e.dcode = d.dcode;

7 7 JOIN /* List the employees and their department names for those that earn more than 35000 */ SELECT e.eid, e.ename, e.salary, d.dname FROM Employee e, Department d WHERE e.salary > 35000 AND e.dcode = d.dcode; EIDEnameSalaryDname 1002Joe40000Finance 1003Jack40000Sales 1005Jill45000Finance l You may use additional selection criteria with join u Example:

8 8 JOINING MORE THAN TWO TABLES SELECT e.eid, e.ename, d.dname, r.title FROM Employee e, Department d, Ranks r WHERE e.dcode = d.dcode ANDe.rank = r.rank ORDER By d.dname, e.rank desc; EIDEnameDnameTitle 1001JohnAccounting Supervisor 1007JimAccountingSupervisor 1005JillFinanceManager 1002JoeFinanceJr. Mgr 1004JaneFinanceSupervisor 1003JackSalesJr. Mgr 1006JebSalesSupervisor

9 9 JOINING A TABLE TO ITSELF EMP EIDEnameMgrID 1001John1002 1002JoeNull 1003Jack1004 1004JaneNull 1005Jill1004 1006Jeb1004 1007Jim1002 /* List the manager name for each employee Notice the use of column headings in the query */ SELECT e1.ename “Employee”, e2.ename “Manager” FROM Emp e1, Emp e2 WHERE e1.mgrid = e2.eid EmployeeManager JohnJoe JackJane JillJane JebJane JimJoe

10 10 OUTER JOIN l Outer join is indicated by a + sign in Oracle l Oracle supports left and right outer joins only l Left (right) outer join retains the unmatched rows from the left (right) table in the result /* This is an example of right outer join */ SELECT e.eid, e.ename, r.rank, r.title FROM Employee e, Ranks r WHERE e.rank(+) = r.rank; EIDEnameRankTitle 1001John1Supervisor 1002Joe2Jr. Mgr. 1003Jack2Jr. Mgr. 1004Jane1Supervisor 1005Jill3Manager 1006Jeb1Supervisor 1007Jim1Supervisor NullNull4Asst. Director NullNull5Director

11 11 JOINS with NULL Values l Null does not have a value. So it does not match with any value, including another null value. l Example: TABLE1 AB 1One NULLThree 4Join4 TABLE2 CD NULLTwo 4Four SELECT A, B, D FROM TABLE1, TABLE2 WHERE a = c; ABD 4Join4Four

12 12 JOINS with NULL Values l You can use outer join to detect null values in a column. l Example: /* Left outer join includes all the rows from the left table in the result */ SELECT * FROM TABLE1, TABLE2 WHERE a = c (+); ABCD 1OneNULLNULL NULLThreeNULLNULL 4Join44Four TABLE1 AB 1One NULLThree 4Join4 TABLE2 CD NULLTwo 4Four

13 13 SUBQUERIES l A subquery is a SELECT statement that is nested inside another SELECT statement or another subquery l There is no restriction on the depth of such nesting l A query containing subqueries is evaluated starting with the innermost subquery l Example: u Suppose we want to list employees that earn the lowest salary u This requires two queries, based on what we have learnt so far l First we will find the minimum salary earned by an employee l Then we will use the min salary value to list the names of employees who earn this salary u We can combine these two steps in to a single query by nesting a subquery within a query

14 14 SUBQUERIES l All examples in this section are based on the tables in Bordoloi (pp. 82, 83). /* Find the minimum salary */ SELECT min(salary) FROM Employee; Min(Salary) 25000 /* List names of employees that earn the min salary found in the previous step */ SELECT fname, lname FROM Employee WHERE salary = 25000; /* Use of subquery */ SELECT fname, lname FROM Employee WHERE salary = (SELECT min(salary) FROM Employee); FnameLname AliciaZelaya JoyceEnglish AhmadJabbar FnameLname AliciaZelaya JoyceEnglish AhmadJabbar

15 15 SUBQUERIES l Three basic types of subqueries u One that returns a single value (Example in previous slide) l normally uses aggregate functions l The returned value is compared in the outer query using a comparison operator such as =, !=, >, <, etc. u One that returns a list of values l these values are from a single column, such as a list of order numbers, list of majors, list of departments, etc. l Outer query uses a list comparison operator, such as IN, or an comparison operator modified by ANY or ALL clause. l SELECT clause of the subquery must contain a single column name u subqueries that are used to check for existence of data l Outer query uses the EXISTS keyword.

16 16 SUBQUERY SYNTAX l A subquery uses a SELECT statement l It is enclosed within parentheses l It may appear in the WHERE clause or the HAVING clause in the outer query l A subquery cannot use the ORDER BY clause l The result of a subquery must be join compatible with WHERE / HAVING clause of the outer query, i.e., the result must be comparable.

17 17 SUBQUERIES WITH IN l WHERE [NOT] IN (subquery) l Subquery returns a list l Example: /* List employees that have a male dependent */ SELECT lname, fname FROM Employee WHERE ssn IN ( SELECT essn FROM Dependent where sex = ‘M’); LnameFname SmithJohn WongFranklin WallaceJennifer

18 18 SUBQUERIES WITH IN l How is the previous query evaluated? /* First the subquery is evaluated */ SELECT essn FROM Dependent where sex = ‘M’; Essn 333445555 987654321 123456789 /* Then the outer query is evaluated using the result of the subquery */ SELECT lname, fname FROM Employee WHERE ssn IN (333445555, 987654321, 123456789);

19 19 SUBQUERIES WITH IN l You can get the same result by writing a JOIN query l It is, however, desirable to use a subquery in this case because the result is from a single table /* The equivalent JOIN query */ SELECT lname, fname FROM Employee, Dependent WHERE employee.ssn = dependent.essn AND dependent.sex = ‘M’; /* Check out the subquery and the join query with sex = ‘F’ */ /* Are the results the same? Explain. */

20 20 MULTIPLE LEVELS OF NESTING l Oracle places no limit on the level of nesting /* List the names of all employees who worked more than 10 hours on the Newbenefits project. */ SELECT lname, fname FROM Employee WHERE ssn IN (SELECT essn FROM Works_on WHERE hours > 10 AND pno IN (SELECT pnumber FROM Project WHERE pname = ‘Newbenefits’) );

21 21 MULTIPLE LEVELS OF NESTING l How is the previous query evaluated? /* We start with the innermost subquery */ SELECT pnumber FROM Project WHERE pname = "Newbenefits”; Pnumber 30 /* The result is plugged into the next subquery */ SELECT essn FROM Works_on WHERE hours > 10 AND pno IN (30); Essn 999887777 987654321 /* Then the final query is evaluated */ SELECT lname, fname FROM Employee WHERE ssn IN ( 999887777, 987654321);

22 22 USE OF ANY or ALL l WHERE [ANY | ALL] (subquery) l The result of the subquery in this case is a list /* List all employees that earn more than all of the employees in Dept. 5 */ SELECT lname, fname FROM Employee WHERE salary > ALL (SELECT salary FROM Employee WHERE dno = 5); /* Can you reformulate the subquery so that ALL is not required in the outer query? What will be the result if we replace > ALL by = ALL? */ LnameFname WallaceJennifer Borg James

23 23 USE OF ANY or ALL l = ANY is same as IN /* List the employees that have a female dependent Following two queries are equivalent */ SELECT lname, fname FROM Employee WHERE ssn = ANY (SELECT essn FROM Dependent WHERE sex = ‘F’); LnameFname SmithJohn WongFranklin SELECT lname, fname FROM Employee WHERE ssn IN (SELECT essn FROM Dependent WHERE sex = ‘F’);

24 24 USE OF ANY or ALL l != ANY is not the same as NOT IN u NOT IN (1,2,3) means != 1and != 2 and != 3 u != ANY (1,2,3) means != 1 or !=2 or != 3 l != ALL is same as NOT IN /* List all employees that don’t have a dependent */ SELECT lname, fname FROM Employee WHERE ssn != ALL (SELECT essn FROM Dependent); /* You will get the same result by replacing != ALL by NOT IN What will be the result if you simply replace ALL by ANY in the above query? */ LnameFname ZelayaAlicia NarayanRamesh EnglishJoyce JabbarAhmad BorgJames

25 25 CORRELATED SUBQUERIES l In correlated subqueries, the inner query depends on values provided by the outer query l The inner query is executed once for each row that might be selected by the outer query /* List all employees who have worked 10 hours on any project */ SELECT lname, fname FROM Employee WHERE 10 IN (SELECT hours FROM Works_on WHERE ssn = essn); /* What is the equivalent join query for the above? LnameFname WongFranklin ZelayaAlicia

26 26 USE of EXISTS l WHERE [NOT] EXISTS (subquery) l Checks for existence of data - returns TRUE or FALSE l A subquery introduced with EXISTS is always a corelated subquery l The SELECT list in the subquery is invariably *, because there is no need for selecting a column /* List all employees that have one or more dependents */ SELECT lname, fname FROM Employee WHERE EXISTS (SELECT * FROM Dependent WHERE ssn = essn); /* How many times does the subquery execute in the above query? */ LnameFname SmithJohn Wong Franklin WallaceJennifer

27 27 USE of EXISTS /* List all employees that do not have any dependent */ SELECT lname, fname FROM Employee WHERE NOT EXISTS (SELECT * FROM Dependent WHERE ssn = essn); LnameFname ZelayaAlicia NarayanRamesh EnglishJoyce JabbarAhmad BorgJames

28 28 SELF TEST l List student name, major and department name for all students l List student name, major, and department name for students that have a GPA > 3.0


Download ppt "1 JOIN SUBQUERY Structured Query Language (SQL) - Part III."

Similar presentations


Ads by Google