10. Built In Functions : String Functions Returns part of a string. Start s pecifies the character position at which the substring begins. length specifies the number of characters in the substring. substring ( expression , start , length ) Returns the reverse of the character or binary expression reverse ( expression ) Returns an integer representing the number of characters in a character expression or text value. char_length ( char_expr ) Returns the ASCII code for the first character in the expression. ascii ( char_expr ) Descriptions Functions
11. Built In Functions : Date Functions Returns date produced by adding datepart to given date. dateadd ( datepart , number, date ) Returns the time between the first and second date. You can specify the datepart as months, years, hours, etc. datediff (datepart, date, date) Returns part of date as an integer. datepart ( datepart, date ) Returns Current system date and time. getdate () Descriptions Functions
12. Built In Functions : System Functions Returns the length of the column datalength ( expression ) Returns the computer name Host_name () Returns the process id of the process. host_id ( ) Descriptions Functions
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31. Joins – Outer Join SELECT * FROM titles RIGHT JOIN titileauthor ON titiles.title_id = titleauthor.title_id SELECT * FROM titles FULL OUTER JOIN titileauthor ON titiles.title_id = titleauthor.title_id
Faculty Notes: Select - CHOOSE the columns in a table that you want returned by your query Where - FILTER the rows in a table that you want returned by your query Order By - SORT data selected from the table in a specified order
Faculty Notes : Char_length function returns the number of character for variable-length data in a table column. However for fixed-length data, it returns the defined length of the column. Reverse(expression) returns reverse of the characters for example if expression is “abcd”, it returns “dcba” Substring(expression, start, length) Returns part of a string where start specifies the character position at which the substring begins. length specifies the number of characters in the substring.
Faculty Notes : Datepart notations are given below- Year abbreviated as yy expects values 1753–9999 Quarter abbreviated as qq expected values 1–4 Month abbreviated as mm expected values 1–12 Week abbreviated as wk expected values 1–54 Day abbreviated as dd expected values 1–31 Dayofyear abbreviated as dy expected values 1–366 Weekday abbreviated as dw expected values 1– 7 (Sunday–Saturday) hour abbreviated as hh expected values 0–23 Minute abbreviated as mi expected values 0–59 Second abbreviated as ss expected values 0–59 millisecond abbreviated as ms expected values 0–999
Faculty Notes: The aggregate functions work with any type of column; however there are few exceptions: sum and avg functions work only with the numeric columns of int, smallint, tinyint, decimal, numeric, float, and money. Min and max functions will not work with bit data types Only the count(*) works with text and image data types. Example : Use pubs go select sum(ytd_sales) [Total Sales] from titles select count (*)[Total Records], avg (price*2)[Average Price] from titles --counts null values as well select count (price)[Total Not Null Records], avg (price*2) [Average Price] from titles --doesn't count null values Select max (ytd_sales)[Max Sales], min (ytd_sales) [Max Sales] from titles
Faculty Notes: WHERE Clause Specifies the condition for the rows returned by a query. Syntax WHERE < search_condition > Arguments <search_condition> Defines the condition to be met for the rows to be returned. There is no limit to the number of predicates in <search_condition>.
Faculty Notes: Read the above slide. Read both the select statements. Explain the output of each statement as follows: The first statement displays the last name and salary of employees whose salary is equal to 30000 The second statement displays the Employee Id and Last name for employees whose manager name is ‘RAYMOND’
Faculty Notes: Read the Select statements. Explain the output of each statement as follows: The first statement displays the last name of employees whose salary is greater than 30000 The second statement displays the Employee Id and Last Name of employees whose salary is less than equal to 30000 The third statement displays the Employee Id whose Status is not ‘ACTIVE’
Faculty Notes: Between condition takes into account the lower and the upper limit In the above example the select statement will display last name of employees whose salary is between 30000 and 50000 including 30000 and 50000 IN condition takes into account on the values supplied In the above example the select statement will display employee id of employees whose manager id is 100 or 200 or 300
Faculty Notes: % Matches any string of zero or more characters. This wildcard character can be used as either a prefix or a suffix. The above Select statement displays the last name of all employees ending with ‘ram’ from the employee table Example: ‘%ram’ will fetch all those values which end in ‘ram’ ram , program, monogram, gram _ (under score) Matches any single character, and can be used as either a prefix or suffix. The above Select statement displays the last name of all employees ending with ‘at’ from the employee table and whose length is three characters. Example: bat , rat, sat, hat You can search for wildcard characters (% , _) also. There are two methods for specifying a character that would ordinarily be a wildcard: Use the ESCAPE keyword to define an escape character. When the escape character is placed in front of the wildcard in the pattern, the wildcard is interpreted as a character. For example, to search for the string 5% anywhere in a string, use: WHERE ColumnA LIKE '%5/%%' ESCAPE '/' In this LIKE clause, the leading and ending percent signs (%) are interpreted as wildcards, and the percent sign preceded by a slash (/) is interpreted as the % character.
Faculty Notes: In the above examples: Select statement will display last name , job id of all employees whose salary is less than equal to 15000 AND job id is ‘SE’ Select statement will display last name , job id of all employees whose salary is less than equal to 15000 OR job id is ‘SE’
Faculty Notes: Select statement will display last name , job id of all employees whose salary is less than equal to 15000 and NOT job id is ‘SE’
Faculty Notes : Distinct returns non repeating values To returns the first n rows mentioned in the function Percent returns the n percent rows from the total rows. This n value can range only between 0-100
Faculty Notes: Step through and explain the slide.
Faculty Notes: Joins Join conditions can be specified in either the FROM or WHERE clauses; specifying them in the FROM clause is recommended. WHERE and HAVING clauses can also contain search conditions to further filter the rows selected by the join conditions.
Faculty Notes : A cross join that does not have a WHERE clause produces the Cartesian product of the tables involved in the join. The size of a Cartesian product result set is the number of rows in the first table multiplied by the number of rows in the second table. The following example shows a Transact-SQL cross join. See ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/udb9/html/c4f98374-e8a7-4f73-8d0e-309bd94ad1ae.htm for more information.
Faculty Notes : Joins can be categorized as: Inner joins (the typical join operation, which uses some comparison operator like = or <>). These include equi-joins and natural joins. Inner joins use a comparison operator to match rows from two tables based on the values in common columns from each table. For example, retrieving all rows where the student identification number is the same in both the students and courses tables. The above stored procedure contains a select statement with INNER JOINS In above example, you can use a self-join to find out which authors in Oakland, California, live in the same postal code area. Since this query involves a join of the authors table with itself, the authors table appears in two roles. To distinguish these roles, you can temporarily and arbitrarily give the authors table two different correlation names—such as au1 and au2—in the from clause. These correlation names qualify the column names in the rest of the query.
Faculty Notes : Outer joins : Outer joins can be a left, a right, or full outer join. Outer joins are specified with one of the following sets of keywords when they are specified in the FROM clause: LEFT JOIN or LEFT OUTER JOIN The result set of a left outer join includes all the rows from the left table specified in the LEFT OUTER clause, not just the ones in which the joined columns match. When a row in the left table has no matching rows in the right table, the associated result set row contains null values for all select list columns coming from the right table. RIGHT JOIN or RIGHT OUTER JOIN. A right outer join is the reverse of a left outer join. All rows from the right table are returned. Null values are returned for the left table any time a right table row has no matching row in the left table. FULL JOIN or FULL OUTER JOIN. A full outer join returns all rows in both the left and right tables. Any time a row has no match in the other table, the select list columns from the other table contain null values. When there is a match between the tables, the entire result set row contains data values from the base tables. Cross joins. Cross joins return all rows from the left table, each row from the left table is combined with all rows from the right table. Cross joins are also called Cartesian products.
Faculty Notes : RIGHT JOIN or RIGHT OUTER JOIN. A right outer join is the reverse of a left outer join. All rows from the right table are returned. Null values are returned for the left table any time a right table row has no matching row in the left table. FULL JOIN or FULL OUTER JOIN. A full outer join returns all rows in both the left and right tables. Any time a row has no match in the other table, the select list columns from the other table contain null values. When there is a match between the tables, the entire result set row contains data values from the base tables. Cross joins. Cross joins return all rows from the left table, each row from the left table is combined with all rows from the right table. Cross joins are also called Cartesian products.
Faculty Notes: Review the key points on the slide. The key points should be used to summarize the content covered throughout this presentation.