Presentation is loading. Please wait.

Presentation is loading. Please wait.

CN2180 MS SQL Server Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP.

Similar presentations


Presentation on theme: "CN2180 MS SQL Server Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP."— Presentation transcript:

1 CN2180 MS SQL Server Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP

2 Agenda o Chapter 6: Complex Queries o Assignment / Homework o Quiz

3 Preparation o SELECT * o INTO employee_enh o FROM employee o ALTER TABLE employee_enh o ADD domicile CHAR(25) NULL o UPDATE employee_enh set domicile = ‘San Antonio’ o WHERE emp_no = 25348 OR emp_no = 18316

4 Preparation (2) o UPDATE employee_enh set domicile = ‘Houston’ o WHERE emp_no = 10102 OR emp_no = 28559 o UPDATE employee_enh set domicile = ‘Seattle’ o WHERE emp_no = 29346 o UPDATE employee_enh set domicile = ‘Portland’ o WHERE emp_no = 9031 o UPDATE employee_enh set domicile = ‘Tacoma’ o WHERE emp_no = 2581

5 JOIN Operator o Two tables can be joined together in (at least) five different ways: o Using equijoin o Using Cartesian product (Cross join) o Using a natural join o Using a thetajoin o Using an outer join

6 Equijoin o ANSI join syntax: o SELECT employee.*, department.* o FROM employee INNER JOIN department o ON employee.dept_no = department.dept_no o SQL Server join syntax: o SELECT employee.*, department.* o FROM employee,department o WHERE employee.dept_no = department.dept_no

7 Equijoin (2) o Two tables will be match up and shows the matched result o SELECT * o FROM works_on JOIN project o ON project.project_no = works_on.project_no o WHERE project_name = ‘Gemini’ o OR See Page 175 for SQL syntax.

8 Cartesian Product o To match ALL possible pair from each tables o SELECT * o FROM employee CROSS JOIN department o SELECT * o FROM employee, department

9 Cartesian Product (2) o You still can use WHERE Clause to reduce the amount of result o SELECT * o FROM works_on CROSS JOIN project o WHERE works_on.project_no = ‘p3’

10 Natural Join o Natural join is the same as equijoin except that it has less column o SELECT employee.*, dept_name, location o FROM employee JOIN department o ON employee.dept_no = department.dept_no o SELECT employee.*,dept_name,location o FROM employee, department o WHERE employee.dept_no = department.dept_no

11 Thetajoin o A join operation that use comparison operator such as > or < o SELECT demployee_enh.*,department.* o FROM employee_enh JOIN department o ON domicile < location

12 Joining more than two o SELECT emp_fname, emp_lname o FROM works_on o JOIN employee ON works_on.emp_no = employee.emp_no JOIN employee.dept_no = department.dept_no o AND location = ‘Seattle’ o AND job = ‘analyst’

13 Joining a Table with itself o To get full details of all departments located at the same location o SELECT t1.dept_no, t1.dept_name, t1.location o FROM department t1 JOIN department t2 o ON t1.location = t2.location o WHERE t1.dept_no <> t2.dept_no

14 Outer Join o Like other join, except that outer join will add unmatched rows from one or both of the tables as well. o LEFT / RIGHT?

15 Outer Join (2) o SELECT employee_enh.*, department.location o FROM employee_enh LEFT OUTER JOIN department o ON domicile = location

16 Outer Join (3) o SELECT employee_enh.domicile, department.* o FROM employee_enh RIGHT OUTER JOIN department o ON domicile = location

17 Assignment o E.6.3 – E.6.10


Download ppt "CN2180 MS SQL Server Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP."

Similar presentations


Ads by Google