Presentation is loading. Please wait.

Presentation is loading. Please wait.

I NTRODUCTION TO SQL II. T ABLE JOINS A simple SQL select statement is one that selects one or more columns from any single table There are two types.

Similar presentations


Presentation on theme: "I NTRODUCTION TO SQL II. T ABLE JOINS A simple SQL select statement is one that selects one or more columns from any single table There are two types."— Presentation transcript:

1 I NTRODUCTION TO SQL II

2 T ABLE JOINS A simple SQL select statement is one that selects one or more columns from any single table There are two types of table joins in SQL statements: Inner join Outer join

3 I NNER JOINS An inner join is the most common form of join used in SQL statements. It can be classified into: Equi-join Natural join Cross join

4 E QUI - JOIN This type of join happens when two tables are joined based on the equality of specified columns for example: SELECT * FROM EMPLOYEE, DEPARTMENT WHERE EMPLOYEE.WORKDEPT = DEPARTMENT.DEPTNO OR SELECT * FROM EMPLOYEE INNER JOIN DEPARTMENT ON EMPLOYEE.WORKDEPT = DEPARTMENT.DEPTNO

5 E QUI - JOIN, C ONT ’ D … Using Correlation Names : SELECT * FROM EMPLOYEE E, DEPARTMENT D WHERE E.WORKDEPT = D.DEPTNO OR SELECT * FROM EMPLOYEE E INNER JOIN DEPARTMENT D ON E.WORKDEPT = D.DEPTNO

6 N ATURAL JOIN A natural join is an improved version of an equi-join where the joining column does not require specification. The system automatically selects the column with same name in the tables and applies the equality operation on it. A natural join will remove all duplicate attributes. Below is an example. SELECT * FROM EMPLOYEE NATURAL JOIN EMPPROJACT

7 C ROSS JOIN A cross join is simply a Cartesian product of the tables to be joined. For example: SELECT * FROM DEPARTMENT, EMPLOYEE

8 O UTER JOINS An outer join is a specialized form of join used in SQL statements. In an outer joins, the first table specified in an SQL statement in the FROM clause is referred as the LEFT table and the remaining table is referred as the RIGHT table. An outer join is of the following three types: Left outer join Right outer join Full outer join

9 T HE DIFFERENT OUTER JOIN TYPES

10 I NPUT TABLES TO USE IN OUTER - JOIN EXAMPLES

11 L EFT OUTER JOIN In a left outer join, the result set is a union of the results of an equi-join, including any non- matching rows from the LEFT table SELECT * FROM STUDENT LEFT OUTER JOIN ENROLLMENT ON STUDENT.ENROLLMENT_NO = ENROLLMENT_NO

12 R IGHT OUTER JOIN In a right outer join, the result set is the union of results of an equi-join, including any non-matching rows from the RIGHT table SELECT * FROM STUDENT RIGHT OUTER JOIN ENROLLMENT ON STUDENT.ENROLLMENT_NO = ENROLLMENT.ENROLLMENT_NO

13 U NION OPERATIONS The Union operator can be used to join two data sets having the same column definitions and in the same order. The union operator removes any duplicate rows from the resulting data set Example : SELECT * FROM student_table_a UNION SELECT * FROM student_table_b

14 U NION OPERATIONS, C ONT ’ D …

15 U NION ALL There may be situations where duplicate removal is not required. In such a case, UNION ALL should be used instead of UNION, as follows: SELECT * from student_table_a UNION ALL SELECT * from student_table_b

16 I NTERSECTION The intersection operator INTERSECT returns a result set common to both data sets as shown in the following statement: select * from student_table_a INTERSECT select * from student_table_b

17 I NTERSECTION ALL The intersect operator will return all common data sets that exist in both tables A and B, however common data sets are listed only once, even if there are multiple duplicate rows in either table A or B. In order to return all data sets with duplicates in the result set, use the INTERSECT ALL operator. For example: select * from student_table_a INTERSECT ALL select * from student_table_b

18 D IFFERENCE (E XCEPT ) The difference operator (EXCEPT) returns the result set that exists only in the LEFT table Logically : A EXCEPT B = A MINUS [A INTERSECT B] For example: select * from student_table_a EXCEPT select * from student_table_b

19 EXCEPT ALL The EXCEPT operator will return a data set that exists in table A, but not in table B; however, common data sets are listed only once, even if there are multiple duplicate rows in table A. In order to return all data sets with duplicates in a result set, use the EXCEPT ALL operator. For example: select * from student_table_a EXCEPT ALL select * from student_table_b

20 R ELATIONAL OPERATORS Relational operators are basic tests and operations that can be performed on data. These operators include: Basic mathematical operations like ‘+’, ‘-‘, ‘*’ and ‘/’ Logical operators like ‘AND’, ‘OR’ and ‘NOT’ String manipulation operators like ‘CONCATENATE’, ‘LENGTH’, ‘SUBSTRING’ Comparative operators like ‘=’, ‘ ’, ‘>=’, ‘<=’ and ‘!=’ Grouping and aggregate operators Other miscellaneous operations like DISTINCT

21 G ROUPING OPERATORS Grouping operators perform operations on two or more rows of data, and provide a summarized output result set. For example, let’s say we have a table with a list of all students and the courses that they are enrolled in. Each student can enroll in multiple courses and each course has multiple students enrolled in the course. If we need a count of students that are enrolled for each course offered, then we need to order the table data by offered courses and then count the corresponding list of students. This can be achieved by using the GROUP BY operator as follows: select course_enrolled, count(*) from students_enrollment group by course_enrolled

22 E XAMPLE

23 GROUP BY MORE THAN ONE C OLUMN

24 T HE H ARDEST TO R EMEMBER R ULE IN A LL OF SQL!

25 E XAMPLE

26 A GGREGATION OPERATORS Operators, which perform on two or more tuples or rows, and return a scalar result set, are called aggregate operators.

27 SQL F UNCTIONS

28 E XAMPLE

29 A NOTHER E XAMPLE ( BEWARE OF NULLS )

30 C OLUMN F UNCTIONS B ASED ON S UBSET

31 HAVING C LAUSE HAVING is a special operator, which can be used only with a GROUP BY clause to filter the desired rows in grouped data. Example: list only those offered courses which have less than 5 enrollments SELECT course_enrolled, count(*) FROM students_enrollment GROUP BY course_enrolled HAVING count(*) < 5

32 E XAMPLE

33 A NOTHER E XAMPLE

34 R ESTRICTIONS Column functions may be specified only in SELECT HAVING SELECT may specify only Column functions Columns specified in 'GROUP BY’ HAVING may specify Any column function on any column in a table being queried. This column need not be in the SELECT. Column functions may not be nested

35 S UB Q UERY When a query is applied within a query, the outer query is referred to as the main query or parent query and the internal query is referred as the sub-query or inner query. Sub-queries are executed first, and then the parent query is executed utilizing data returned by the sub- queries.

36 R ESULT U SING S EPARATE S ELECTS

37 S AME R ESULT U SING A S UBQUERY SELECT EMPNO, LASTNAME FROM EMPLOYEE WHERE SALARY > (SELECT AVG(SALARY) FROM EMPLOYEE)

38 S UB - QUERIES RETURNING A SCALAR VALUE Scalar values represent a single value of any attribute or entity, for example Name, Age, Course, Year, and so on. Example: SELECT name FROM students_enrollment WHERE age = ( SELECT min(age) FROM students )

39 S UB - QUERIES RETURNING VECTOR VALUES When a sub-query returns a data set that represents multiple values for a column (like a list of names) or array of values for multiple columns (like Name, age and date of birth for all students), then the sub-query is said to be returning vector values. Example: SELECT name FROM students WHERE course_enrolled IN ( SELECT distinct course_name FROM courses WHERE department_name = ‘Computer Science’ )

40 C ORRELATED SUB - QUERY When a sub-query is executed for each row of the parent table, instead of once (as shown in the examples above) then the sub-query is referred to as a correlated sub-query. Example: SELECT dept, name, marks FROM final_result a WHERE marks = ( SELECT max(marks) FROM final_result WHERE dept = a.dept )

41 S UB - QUERY IN FROM C LAUSES A sub-query can be used in a FROM clause as well, as shown in following example: SELECT dept, max_marks, min_marks, avg_marks FROM ( SELECT dept, max(marks) as max_marks, min(marks) as min_marks, avg(marks) as avg_marks )FROM final_result GROUP BY dept ) WHERE (max_marks – min_marks) > 50 and avg_marks < 50

42 S UBQUERY U SING IN

43 S UBQUERY U SING NOT IN

44 S UBQUERY WITHIN HAVING C LAUSE

45 R ESTRICTIONS /R EMINDERS Subqueries: Must be on right side of search condition when used in WHERE clause or in HAVING clause Must be enclosed in parentheses Can return single or multiple values Number of values subquery can return must be compatible with operator in outer SELECT The subquery must return the same number of items as in the list to which it is compared

46


Download ppt "I NTRODUCTION TO SQL II. T ABLE JOINS A simple SQL select statement is one that selects one or more columns from any single table There are two types."

Similar presentations


Ads by Google