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 SELECT Statement SELECT [ALL|DISTINCT] column_list FROM tbl_1 [tbl_alias1] [{, tbl_alias2]}…] o Column_name [AS] column_heading

3 WHERE Clause SELECT select_list [INTO new_tbl] FROM table [WHERE search_condition] [GROUP BY group_by_expression] [HAVING search_condition] [ORDER BY order_expression [ASC|DESC]] o See P115 for more operator.

4 WHERE Clause (Boolean Operators) SELECT emp_no, project_no FROM works_on WHERE project_no = ‘p2’ AND job = ‘Clerk’ SELECT project_no, emp_no FROM works_on WHERE project_no = ‘p1’ OR project_no = ‘p2’

5 WHERE Clause (IN and BETWEEN) SELECT * FROM employee WHERE emp_no IN (29346, 28559, 25348) SELECT * FROM employee WHERE emp_no NOT IN (10102, 9031) SELECT project_name, budget FROM project WHERE budget BETWEEN 95000 and 120000

6 WHERE Clause (NULL and NOT NULL) SELECT emp_no, project_no FROM works_on WHERE project_no =‘p2’ AND job IS NULL SELECT emp_no, ISNULL (job, ‘job unknown’) task FROM works_on WHERE project_no = ‘p1’

7 WHERE Clause (LIKE Operator) Culumn [NOT] LIKE ‘pattern’ o Two wildcard o % = any sequence of zero or more characters. o _ = any single character. SELECT emp_fname, emp_lname, emp_no FROM employee WHERE emp_lname LIKE ‘J%’

8 Simple Subqueries o There are two types of subqueries o Simple – The inner query is evaluated exactly once o Correlated – The inner query is evaluated each time the system retrieves a new row. SELECT emp_fname, emp_lname FROM employee WHERE dept_no = (SELECT dept_no FROM department WHERE dept_name =‘Research’)

9 Subqueries and IN Operator SELECT * FROM employee WHERE dept_no IN (SELECT dept_no FROM department WHERE location = ‘Dallas’)

10 Queries in the FROM Clause o Get the names of all employee with employee numbers greater than or equal to 10000 SELECT emp_fname, emp_lname FROM (SELECT * FROM employee WHERE emp_no >= 10000) AS empno_10000

11 GROUP BY Clause SELECT job FROM works_on GROUP BY job

12 Aggregate Functions o All aggregate functions can be divided into three groups o Convenient aggregate functions o MIN o MAX o SUM o AVG o COUNT o Statistical aggregate functions o Superaggregates o In SELECT statement, no simple columns can be included except to be used with GROUP BY clause.

13 Aggregate Functions (Cont.) SELECT MIN (emp_no) min_employee_number FROM employee SELECT emp_no, emp_lname FROM employee WHERE emp_no = (SELECT MIN(emp_no) FROM employee) SELECT job, COUNT(*) job_count FROM works_on GROUP BY job

14 HAVING Clause HAVING condition SELECT project_no FROM works_on GROUP BY project_no HAVING COUNT(*) < 4

15 ORDER BY Clause ORDER BY {[col_name | col_number [ASC | DESC ]]}, … SELECT emp_no, emp_fname, emp_lname FROM employee ORDER BY emp_no

16 Set Operators o UNION select_1 UNION [ALL] select_2 { [UNION [ALL] select_3]}… o INTERSECTION o Shows the record that belong to both tables o DIFFERENCE

17 Create/Update employee_enh table USE sample SELECT * INTO employee_enh FROM employee ALTER TABLE employee_enh ADD domicile CHAR(25) NULL

18 Create/Update employee_enh table UPDATE employee_enh set domicile = ‘San Antonio’ WHERE emp_no = 25348 OR emp_no = 18316 UPDATE employee_enh set domicile = ‘Houston’ WHERE emp_no = 10102 OR emp_no = 28559 UPDATE employee_enh set domicile = ‘Seattle’ WHERE emp_no = 29346 UPDATE employee_enh set domicile = ‘Portland’ WHERE emp_no = 9031 UPDATE employee_enh set domicile = ‘Tacoma’ WHERE emp_no = 2581

19 UNION USE sample SELECT domicile FROM employee_enh UNION SELECT location FROM department o To use UNION operator, two table must have the same number of columns, and the corresponding columns have the compatible data types. o GROUP BY and HAVING cannot be used with the UNION

20 INTERSECTION USE sample SELECT emp_no FROM employee WHERE dept_no = ‘d1’ INTERSECT SELECT emp_no FROM works_on WHERE enter_date < ‘01.01.1999’

21 CASE Expressions o Simple CASE Expression o CASE Expression_1 o {WHEN expression_2 THEN result_1} o [ELSE result_n] o END

22 CASE Expression (2) o Searched CASE expression o CASE o {WHEN condition_1 THEN result_1} o [ELSE result_n] o END

23 CASE Expression (3) o SELECT project_name, o CASE o WHEN budget > 0 AND budget < 100000 THEN 1 o WHEN budget >= 100000 AND budget < 200000 THEN 2 o ELSE 4 o END budget_weight o FROM project

24 COMPUTE o To use with MIN, MAX, AVG, SUM, COUNT o SELECT emp_no, project_no, enter_date o FROM works_on o WHERE project_no = ‘p1’ o OR project_no =‘p2’ o COMPUTE MIN(enter_date)

25 COMPUTE (2) o SELECT emp_no, project_no, enter_date o FROM works_on o WHERE project_no = ‘p1’ o OR project_no =‘p2’ o ORDER BY project_no o COMPUTE MIN(enter_date) BY project_no

26 Temporary Table o Local temporary table will be removed at the end of the current session o # - #tbl_name o Global temporary table will be dropped at the end of the session that created this table o ##

27 Temporary Table (2) o CREATE TABLE #project_temp o (project_no CHAR(4) NOT NULL, o Project_name CHAR(25) NOT NULL) o SELECT project_no, project_name o INTO #project_temp1 o FROM project

28 Computed columns o CREATE TABLE orders o (orderid INT NOT NULL, o Price FLOAT NOT NULL, o Quantity INT NOT NULL, o Orderdate DATETIME NOT NULL, o Total AS price * quantity PERSISTED o Shippeddate AS DATEADD(DAY,7,orderdate))

29 Exercise o P166 o E.5.1 – E.5.12 o *Note* E.5.3, do only one SELECT o E.5.15 – E.5.17


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

Similar presentations


Ads by Google