Presentation is loading. Please wait.

Presentation is loading. Please wait.

DQL Statements Lab - 3 COMP 353 Summer 2013-14.

Similar presentations


Presentation on theme: "DQL Statements Lab - 3 COMP 353 Summer 2013-14."— Presentation transcript:

1 DQL Statements Lab - 3 COMP 353 Summer

2 JOINS : Joining Multiple Tables
Tables are joined on columns that have the same data type and data width in the tables. After joining multiple tables we can work with them as though they were a single entity. Types of Joins: NATURAL JOIN INNER JOIN OUTERJOIN (LEFT, RIGHT) CROSS JOIN

3 SYNTAX : SELECT <Col1> , <Col2>, <ColN>
JOINS : NATURAL JOIN A NATURAL JOIN is a JOIN operation that creates an implicit join clause for you based on the common columns in the two tables being joined [1]. Common columns are columns that have the same name in both tables [1]. SYNTAX : SELECT <Col1> , <Col2>, <ColN> FROM <TableName1> NATURAL JOIN <TableName2> WHERE <Condition> ORDER BY <Col1>, <Col2>, …. <ColN>

4 JOINS : NATURAL JOIN EXAMPLE: mysql> SELECT *
FROM EMPLOYEE NATURAL JOIN DEPARTMENT; | Ssn | Fname | Lname | Bdate | Address | Salary | Dname | Dnumber | Mgr_start_date | | | TIM | DUGAY | | Montreal | | Finance | | | | | Anna | DUGAY | | Toronto | | Marketing | | | | | Jai | Singh | | Montreal | | Human Resource | | | 3 rows in set (0.00 sec)

5 JOINS : INNER JOIN Inner joins by far the most commonly used joins.
This type of join can be used in situations where it is required to select only those rows that have values in common in the column specified in the ON clause. Inner join returns all rows from both tables where there is a match. Specifies a join between two tables with an explicit join clause [1]. SYNTAX : SELECT <Col1> , <Col2>, <ColN> FROM <TableName1> INNER JOIN <TableName2> ON <TableName1>.<Col> = <TableName2>.<Col> WHERE <Condition> ORDER BY <Col1>, <Col2>, …. <ColN>

6 JOINS : INNER JOIN EXAMPLE:
mysql> SELECT CONCAT(E.Fname,' ',E.Lname) AS Employee, E.Ssn, E.Salary, D.Dname AS 'Department-Manager' FROM EMPLOYEE AS E INNER JOIN DEPARTMENT AS D ON E.Ssn = D.Ssn; ORDER BY Salary DESC; | Employee | Ssn | Salary | Department-Manager | | TIM DUGAY | | | Finance | | Anna DUGAY | | | Marketing | | Jai Singh | | | Human Resource | 3 rows in set (0.00 sec) Key Points (Apart from INNER JOIN): Renaming an Attribute. Concatenating 2 Attributes. Table Aliases to remove column ambiguity (To remove following error). (ERROR 1052 (23000): Column 'Ssn' in field list is ambiguous)

7 JOINS : LEFT OUTER JOIN Outer joins gives bit more flexibility when selecting data from related tables. This type of join can be used in situations where it is desired, to select all rows from the table on the left regardless of whether the other table has values in common and enter null where data is missing. A LEFT OUTER JOIN is one of the JOIN operations that allow you to specify a join clause. It preserves the unmatched rows from the first (left) table, joining them with a NULL row in the shape of the second (right) table [1]. SYNTAX : SELECT <Col1> , <Col2>, <ColN> FROM <TableName1> LEFT JOIN <TableName2> ON <TableName1>.<Col> = <TableName2>.<Col> WHERE <Condition> ORDER BY <Col1>, <Col2>, …. <ColN>

8 JOINS : LEFT OUTER JOIN EXAMPLE: mysql> SELECT Fname,Lname,EMPLOYEE.Ssn, Salary, Dname AS 'Manager?' FROM EMPLOYEE LEFT JOIN DEPARTMENT ON EMPLOYEE.Ssn = DEPARTMENT.Ssn ORDER BY Salary DESC; | Fname | Lname | Ssn | Salary | Manager? | | Tristan | McDonald | | | NULL | | Mathew | Munger | | | NULL | | Anna | DUGAY | | | Marketing | | TIM | DUGAY | | | Finance | | Jai | Singh | | | Human Resource | 5 rows in set (0.01 sec)

9 JOINS : CROSS JOIN A CROSS JOIN is a JOIN operation that produces the Cartesian product of two tables. SYNTAX : SELECT <Col1> , <Col2>, <ColN> FROM <TableName1> CROSS JOIN <TableName2> WHERE <Condition> ORDER BY <Col1>, <Col2>, …. <ColN> OR SELECT <Col1> , <Col2>, <ColN> FROM <TableName1> , <TableName2>

10 JOINS : CROSS JOIN EXAMPLE: mysql> SELECT *
FROM EMPLOYEE CROSS JOIN DEPARTMENT; OR SELECT * FROM EMPLOYEE,DEPARTMENT; | Fname | Lname | Ssn | Bdate | Address | Salary | Dname | Dnumber | Ssn | Mgr_start_date | | Tristan | McDonald | | | Montreal | | Finance | | | | | Tristan | McDonald | | | Montreal | | Human Resource | | | | | Tristan | McDonald | | | Montreal | | Marketing | | | | | Mathew | Munger | | | OTTAWA | | Finance | | | | | Mathew | Munger | | | OTTAWA | | Human Resource | | | | | Mathew | Munger | | | OTTAWA | | Marketing | | | | | TIM | DUGAY | | | Montreal | | Finance | | | | | TIM | DUGAY | | | Montreal | | Human Resource | | | | | TIM | DUGAY | | | Montreal | | Marketing | | | | | Anna | DUGAY | | | Toronto | | Finance | | | | | Anna | DUGAY | | | Toronto | | Human Resource | | | | | Anna | DUGAY | | | Toronto | | Marketing | | | | | Jai | Singh | | | Montreal | | Finance | | | | | Jai | Singh | | | Montreal | | Human Resource | | | | | Jai | Singh | | | Montreal | | Marketing | | | | 15 rows in set (0.00 sec)

11 GROUPING DATA FROM TABLES IN SQL
Till Now, all SQL SELECT statements have: Retrieved all the rows from tables. Retrieved selected rows from tables with the use of a WHERE clause which returns only rows that meet the condition specified. Retrieved unique rows from tables using DISTINCT clause. Retrieved sorted rows using ORDER BY clause. GROUP BY and HAVING Clauses Other than above mentioned clauses these 2 clauses also facilitate selective retrieval of rows. These 2 clauses act on result set rather than individual records.

12 GROUP BY Clause GROUP BY clause is another section of the SELECT statement. It is an optional clause that tells MYSQL to group rows based on distinct values that exist for specified columns. SYNTAX : SELECT <Col1> , <Col2>, <ColN>, AGGREGATE_FUNCTION(exp) FROM <TableName1> WHERE <Condition> GROUP BY <Col1>, <Col2>, …. <ColN> ORDER BY <Col1>, <Col2>, …. <ColN> ;

13 GROUP BY Clause EXAMPLE: mysql> SELECT City, COUNT(*) FROM EMPLOYEE GROUP BY City; | City | COUNT(*) | | Montreal | 4 | | OTTAWA | 1 | | Toronto | 1 | 3 rows in set (0.00 sec)

14 HAVING Clause The HAVING clause can be used in conjunction with the GROUP BY Clause. HAVING clause imposes a condition on the GROUP BY clause, which further filters the groups created by the GROUP BY clause. SYNTAX : SELECT <Col1> , <Col2>, <ColN>, AGGREGATE_FUNCTION(exp) FROM <TableName1> WHERE <condition> GROUP BY <Col1>, <Col2>, …. <ColN> HAVING <condition> ORDER BY <Col1>, <Col2>, …. <ColN> ;

15 HAVING Clause EXAMPLE: mysql> SELECT City, COUNT(*) FROM EMPLOYEE GROUP BY City HAVING COUNT(*)>2; | City | COUNT(*) | | Montreal | 4 | 1 row in set (0.00 sec) EXAMPLE: mysql> SELECT City, COUNT(*) AS EmployeeCount FROM EMPLOYEE GROUP BY City HAVING EmployeeCount>2; | City | EmployeeCount | | Montreal | | 1 row in set (0.00 sec)

16 DQL Statement : Logical Operator (AND)
The AND operator requires that each condition must be met for the record to be included in the Result Set. SYNTAX : SELECT * from <TableName> WHERE <condition1> AND <condition2>…; EXAMPLE: mysql> SELECT * from EMPLOYEE WHERE Ssn> AND Salary>23000; | Fname | Lname | Ssn | Bdate | Address | Salary | | TIM | DUGAY | | | Montreal | | | Anna | DUGAY | | | Toronto | | 2 rows in set (0.01 sec)

17 DQL Statement : Logical Operator (OR)
The OR operator requires that any of the conditions must be met for the record to be included in the Result Set. SYNTAX : SELECT * from <TableName> WHERE <condition1> OR <condition2>…; EXAMPLE: mysql> SELECT * from EMPLOYEE WHERE Ssn> OR Salary>23000; | Fname | Lname | Ssn | Bdate | Address | Salary | | Tristan | McDonald | | | Montreal | | | Mathew | Munger | | | OTTAWA | | | TIM | DUGAY | | | Montreal | | | Anna | DUGAY | | | Toronto | | | Jai | Singh | | | Montreal | | 5 rows in set (0.00 sec)

18 DQL Statement : Range Searching (BETWEEN Operator)
The BETWEEN operator is used to select data that is within a range of values. Range Specified after the BETWEEN operator is inclusive. SYNTAX : SELECT * from <TableName> WHERE <colName> BETWEEN <val1> AND <val2>; EXAMPLE: mysql> SELECT * from EMPLOYEE WHERE Salary BETWEEN AND 34000; | Fname | Lname | Ssn | Bdate | Address | Salary | | Tristan | McDonald | | | Montreal | | | Mathew | Munger | | | OTTAWA | | 2 rows in set (0.00 sec)

19 DQL Statement : (IN and NOT IN Predicate)
In case a value needs to be compared to a list of values then the IN predicate is used. SYNTAX : SELECT * from <TableName> WHERE <colName> IN (val1>,<val2>,<val3>,….,<valn>); EXAMPLE: mysql> SELECT * from EMPLOYEE WHERE Address IN ('Toronto','Vancouver'); | Fname | Lname | Ssn | Bdate | Address | Salary | | Anna | DUGAY | | | Toronto | | 1 row in set (0.00 sec) mysql> SELECT * from EMPLOYEE WHERE Address NOT IN ('Toronto','Vancouver'); | Fname | Lname | Ssn | Bdate | Address | Salary | | Tristan | McDonald | | | Montreal | | | Mathew | Munger | | | OTTAWA | | | TIM | DUGAY | | | Montreal | | | Jai | Singh | | | Montreal | | 4 rows in set (0.00 sec)

20 DQL Statement : (ALL Operator)
The word ALL, which must follow a comparison operator, means “return TRUE if the comparison is TRUE for ALL of the values in the column that the subquery returns [1] SYNTAX : operand comparison_operator ALL (subquery) EXAMPLE: mysql> SELECT * from EMPLOYEE WHERE Salary > ALL (SELECT min(Salary) from EMPLOYEE); | Fname | Lname | Ssn | Bdate | Address | Salary | | Tristan | McDonald | | | Montreal | | | Mathew | Munger | | | OTTAWA | | | TIM | DUGAY | | | Montreal | | | Anna | DUGAY | | | Toronto | | 4 rows in set (0.00 sec)

21 DQL Statement : (ANY Operator)
The ANY keyword, which must follow a comparison operator, means “return TRUE if the comparison is TRUE for ANY of the values in the column that the subquery returns.” [1] SYNTAX : operand comparison_operator ANY (subquery) EXAMPLE: mysql> SELECT * from EMPLOYEE WHERE Ssn = ANY(Select Mgr_ssn from DEPARTMENT WHERE Ssn > ' '); | Fname | Lname | Ssn | Bdate | Address | Salary | | Jai | Singh | | | Montreal | | 1 row in set (0.00 sec)

22 References 1 2.

23


Download ppt "DQL Statements Lab - 3 COMP 353 Summer 2013-14."

Similar presentations


Ads by Google