Functions and JoinsObjectivesIn this lesson, you will learn to: Use functions Use different types of joins©NIIT           ...
Functions and Joins3.D.1 Displaying Data in Upper Case Using StringFunctions A report containing the newspaper name, the n...
Functions and JoinsTask List Create a format for the query output Draft the query Execute the query Verify that the query ...
Functions and JoinsCreate a format for the query output Result:     The output required in the report is the newspaper nam...
Functions and JoinsDraft the query String Functions: Are used to format data as per specific  requirements     Syntax     ...
Functions and JoinsExecute the query Action:     In the Query Analyzer window, type:     Execute the query©NIIT           ...
Functions and JoinsVerify that the query output is as per the requiredresults Check whether:     All the required rows are...
Functions and JoinsJust a Minute... The names, addresses, and phone numbers of the  recruitment agencies in Houston are re...
Functions and Joins3.D.2 Adding Days to a Date Using Date Functions The proposed deadline for campus recruitment is 10 day...
Functions and JoinsTask List Create a format for the query output Draft the query Execute the query Verify that the query ...
Functions and JoinsCreate a format for the query output Result:     The output required in the report is the college code,...
Functions and JoinsDraft the query Date functions: Are used to manipulate datetime values,  perform arithmetic operations,...
Functions and JoinsDraft the query (Contd.)     Therefore, the query using the SELECT statement should      be:        SEL...
Functions and JoinsExecute the query Action:     In the Query Analyzer window, type the query     Execute the query©NIIT  ...
Functions and JoinsVerify that the query output is as per the requiredresults Check whether:     All the required rows are...
Functions and JoinsJust a Minute...A schedule for interviews is required. The name of the candidate,the interviewer’s empl...
Functions and Joins3.D.3 Rounding off Values Using MathematicalFunctions The test scores of candidates have been declared....
Functions and JoinsTask List Create a format for the query output Draft the query Execute the query Verify that the query ...
Functions and JoinsCreate a format for the query output Result:     The output required in the report is the first name,  ...
Functions and JoinsDraft the query Mathematical Functions: Are used to perform numerical  operations on mathematical data ...
Functions and JoinsDraft the query (Contd.)     Therefore, the query using the SELECT statement should      be:        SEL...
Functions and JoinsExecute the query Action:     In the Query Analyzer window, type:     Execute the query©NIIT           ...
Functions and JoinsVerify that the query output is as per the requiredresults Check whether the marks are rounded off to t...
Functions and JoinsSystem Functions System functions provide a method of querying the system  tables of SQL Server     The...
Functions and JoinsData Conversion The CONVERT function is used to change data from one  type to another when SQL Server c...
Functions and JoinsJoins A join can be defined as an operation that includes the  retrieval of data from more than one tab...
Functions and JoinsJoins (Contd.) The various types of joins are:     Inner Join     Outer Join     Cross Join     Equi Jo...
Functions and Joins3.D.4 Displaying Data From Two Tables Using Inner  Joins The names of the candidates and their recruitm...
Functions and JoinsTask List Create a format for the query output Draft the query Execute the query Verify that the query ...
Functions and JoinsCreate a format for the query output Result:     The required output from the query is a list of candid...
Functions and JoinsDraft the query Inner Joins     Syntax        SELECT column_name, column_name        [,column_name]    ...
Functions and JoinsDraft the query (Contd.) Result:     The required information is available in the      ExternalCandidat...
Functions and JoinsExecute the query Action:     In the Query Analyzer window, type the query     Execute the query©NIIT  ...
Functions and JoinsVerify that the query output is as per the requiredresults Check whether:     The required columns from...
Functions and JoinsJust a Minute… The names of candidates and the newspapers they referred  for recruitment advertisements...
Functions and Joins3.D.5 Displaying Data From Two Tables Using Outer  Joins The names of all the external candidates along...
Functions and JoinsTask List Create a format for the query output Draft the query Execute the query Verify that the query ...
Functions and JoinsCreate a format for the query output Result:     The required output from the query is the names of all...
Functions and JoinsDraft the query Outer Joins     A join can be termed an outer join when the result set      contains al...
Functions and JoinsDraft the query (Contd.) Result:     The required information is available in the      ExternalCandidat...
Functions and JoinsExecute the query Action:     In the Query Analyzer window, type:     Execute the query©NIIT           ...
Functions and JoinsVerify that the query output is as per the requiredresults Action:     Check whether:        ® The    r...
Functions and JoinsCross Join A join that includes more than one table using the keyword  CROSS is called a cross join. Ex...
Functions and JoinsEqui Join A join that uses an asterisk (*) sign in the SELECT list and  displays redundant column data ...
Functions and JoinsNatural Join A join that restricts the redundant column data from the  result set is known as a natural...
Functions and JoinsSelf Join A join is said to be a self join when one row in a table  correlates with other rows in the s...
Functions and JoinsSummaryIn this lesson you learned that: SQL Server uses string functions, which can be used as part   o...
Functions and JoinsSummary (Contd.) In inner join, data from multiple tables is displayed after  comparing values present ...
Functions and JoinsSummary (Contd.) A join that restricts the redundant column data from the  result set is known as a nat...
Upcoming SlideShare
Loading in …5
×

Sql xp 03

632 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
632
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
43
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Sql xp 03

  1. 1. Functions and JoinsObjectivesIn this lesson, you will learn to: Use functions Use different types of joins©NIIT SQL/Lesson 3/Slide 1 of 49
  2. 2. Functions and Joins3.D.1 Displaying Data in Upper Case Using StringFunctions A report containing the newspaper name, the name of the contact person, and the telephone numbers is required to contact various newspapers to place an advertisement. The newspaper name should be displayed in uppercase.©NIIT SQL/Lesson 3/Slide 2 of 49
  3. 3. Functions and JoinsTask List Create a format for the query output Draft the query Execute the query Verify that the query output is as per the required results©NIIT SQL/Lesson 3/Slide 3 of 49
  4. 4. Functions and JoinsCreate a format for the query output Result: The output required in the report is the newspaper name (in upper case), the contact person, and the telephone number from the Newspaper table The column headings in the report are cNewspaperName, vContactPerson, and cPhone©NIIT SQL/Lesson 3/Slide 4 of 49
  5. 5. Functions and JoinsDraft the query String Functions: Are used to format data as per specific requirements Syntax SELECT function_name (parameters) Result: The information is available in the Newspaper table The string function to be used to display the names of newspapers in uppercase is UPPER Therefore, the query using the SELECT statement should be: SELECT Newspaper Name=UPPER (cNewspaperName), vContactPerson, cPhone FROM Newspaper©NIIT SQL/Lesson 3/Slide 5 of 49
  6. 6. Functions and JoinsExecute the query Action: In the Query Analyzer window, type: Execute the query©NIIT SQL/Lesson 3/Slide 6 of 49
  7. 7. Functions and JoinsVerify that the query output is as per the requiredresults Check whether: All the required rows are displayed The newspaper names are displayed in upper case©NIIT SQL/Lesson 3/Slide 7 of 49
  8. 8. Functions and JoinsJust a Minute... The names, addresses, and phone numbers of the recruitment agencies in Houston are required. However, only the first 10 characters of the address should be displayed.©NIIT SQL/Lesson 3/Slide 8 of 49
  9. 9. Functions and Joins3.D.2 Adding Days to a Date Using Date Functions The proposed deadline for campus recruitment is 10 days from the start date of the recruitment process. A report containing the college code, the start date for recruitment, and the proposed deadline for all colleges the company is visiting needs to be displayed.©NIIT SQL/Lesson 3/Slide 9 of 49
  10. 10. Functions and JoinsTask List Create a format for the query output Draft the query Execute the query Verify that the query output is as per the required results©NIIT SQL/Lesson 3/Slide 10 of 49
  11. 11. Functions and JoinsCreate a format for the query output Result: The output required in the report is the college code, the recruitment start date, and the proposed deadline The column headings of the report are cCollegeCode, dRecruitmentStartDate, and ProposedDeadline, which is 10 days from the start date©NIIT SQL/Lesson 3/Slide 11 of 49
  12. 12. Functions and JoinsDraft the query Date functions: Are used to manipulate datetime values, perform arithmetic operations, and perform date parsing (extract components like the day, the month, and the year) Syntax SELECT date_function (parameters) Result: The required information is available in the CampusRecruitment table The date function to be used is DATEADD©NIIT SQL/Lesson 3/Slide 12 of 49
  13. 13. Functions and JoinsDraft the query (Contd.) Therefore, the query using the SELECT statement should be: SELECT cCollegeCode, dRecruitmentStartDate, Proposed Deadline = DATEADD(dd, 10, dRecruitmentStartDate) FROM CampusRecruitment©NIIT SQL/Lesson 3/Slide 13 of 49
  14. 14. Functions and JoinsExecute the query Action: In the Query Analyzer window, type the query Execute the query©NIIT SQL/Lesson 3/Slide 14 of 49
  15. 15. Functions and JoinsVerify that the query output is as per the requiredresults Check whether: All the required rows are displayed The proposed deadline is 10 days from the start date©NIIT SQL/Lesson 3/Slide 15 of 49
  16. 16. Functions and JoinsJust a Minute...A schedule for interviews is required. The name of the candidate,the interviewer’s employee code, the date, and the weekday ofthe interview are to be printed in alphabetical order of candidatenames in the following format: Candidate Name Interviewer Code Date Week Day        ©NIIT SQL/Lesson 3/Slide 16 of 49
  17. 17. Functions and Joins3.D.3 Rounding off Values Using MathematicalFunctions The test scores of candidates have been declared. Helen White has scored 79.9 marks, and she is to be informed of the same. Her first name, telephone number, and score have to be displayed. The score should be rounded off to the nearest integer.©NIIT SQL/Lesson 3/Slide 17 of 49
  18. 18. Functions and JoinsTask List Create a format for the query output Draft the query Execute the query Verify that the query output is as per the required results©NIIT SQL/Lesson 3/Slide 18 of 49
  19. 19. Functions and JoinsCreate a format for the query output Result: The output required in the report is the first name, telephone number, and score of the candidate The column headings of the report are vFirstName, cPhone, and Score©NIIT SQL/Lesson 3/Slide 19 of 49
  20. 20. Functions and JoinsDraft the query Mathematical Functions: Are used to perform numerical operations on mathematical data Syntax ROUND (numeric_expression, length) Result: The required information is available in the ExternalCandidate table The function to be used is ROUND©NIIT SQL/Lesson 3/Slide 20 of 49
  21. 21. Functions and JoinsDraft the query (Contd.) Therefore, the query using the SELECT statement should be: SELECT vFirstName, cPhone,Score= ROUND(79.9,0) FROM ExternalCandidate WHERE vFirstName=Helen©NIIT SQL/Lesson 3/Slide 21 of 49
  22. 22. Functions and JoinsExecute the query Action: In the Query Analyzer window, type: Execute the query©NIIT SQL/Lesson 3/Slide 22 of 49
  23. 23. Functions and JoinsVerify that the query output is as per the requiredresults Check whether the marks are rounded off to the nearest numeric value ©NIIT SQL/Lesson 3/Slide 23 of 49
  24. 24. Functions and JoinsSystem Functions System functions provide a method of querying the system tables of SQL Server These functions are used to access SQL Server, databases, or user-related information They enable quick conversion of system and object information without writing several queries©NIIT SQL/Lesson 3/Slide 24 of 49
  25. 25. Functions and JoinsData Conversion The CONVERT function is used to change data from one type to another when SQL Server cannot implicitly understand a conversion Syntax CONVERT (datatype [(length)], expression [, style])©NIIT SQL/Lesson 3/Slide 25 of 49
  26. 26. Functions and JoinsJoins A join can be defined as an operation that includes the retrieval of data from more than one table at a time Syntax SELECT column_name, column_name [,column_name] FROM table_name [CROSS|INNER|[LEFT | RIGHT]OUTER] JOIN table_name [ON table_name.ref_column_name join_operator table_name.ref_column_name] [WHERE search_condition]©NIIT SQL/Lesson 3/Slide 26 of 49
  27. 27. Functions and JoinsJoins (Contd.) The various types of joins are: Inner Join Outer Join Cross Join Equi Join Natural Join Self Join©NIIT SQL/Lesson 3/Slide 27 of 49
  28. 28. Functions and Joins3.D.4 Displaying Data From Two Tables Using Inner Joins The names of the candidates and their recruitment agencies are required for an analysis by senior management. A report displaying these details is to be generated.©NIIT SQL/Lesson 3/Slide 28 of 49
  29. 29. Functions and JoinsTask List Create a format for the query output Draft the query Execute the query Verify that the query output is as per the required results©NIIT SQL/Lesson 3/Slide 29 of 49
  30. 30. Functions and JoinsCreate a format for the query output Result: The required output from the query is a list of candidate names and the names of the recruitment agencies that sent them©NIIT SQL/Lesson 3/Slide 30 of 49
  31. 31. Functions and JoinsDraft the query Inner Joins Syntax SELECT column_name, column_name [,column_name] FROM table_name JOIN table_name ON table_name.ref_column_name join_operator table_name.ref_column_name Cartesian Product: A join that includes more than one table without any condition in the ON clause creates a cartesian product between the two tables©NIIT SQL/Lesson 3/Slide 31 of 49
  32. 32. Functions and JoinsDraft the query (Contd.) Result: The required information is available in the ExternalCandidate and RecruitmentAgencies tables Therefore, the query using the SELECT statement should be: SELECT Candidate Name = vFirstName, Recruitment Agency = cName FROM ExternalCandidate JOIN RecruitmentAgencies ON ExternalCandidate.cAgencyCode = RecruitmentAgencies.cAgencyCode©NIIT SQL/Lesson 3/Slide 32 of 49
  33. 33. Functions and JoinsExecute the query Action: In the Query Analyzer window, type the query Execute the query©NIIT SQL/Lesson 3/Slide 33 of 49
  34. 34. Functions and JoinsVerify that the query output is as per the requiredresults Check whether: The required columns from different tables are displayed The required rows are displayed©NIIT SQL/Lesson 3/Slide 34 of 49
  35. 35. Functions and JoinsJust a Minute… The names of candidates and the newspapers they referred for recruitment advertisements are required for an analysis. A report displaying these details is to be generated.©NIIT SQL/Lesson 3/Slide 35 of 49
  36. 36. Functions and Joins3.D.5 Displaying Data From Two Tables Using Outer Joins The names of all the external candidates along with the names of their recruitment agencies, wherever applicable, are required for an analysis. A report displaying these details is to be generated.©NIIT SQL/Lesson 3/Slide 36 of 49
  37. 37. Functions and JoinsTask List Create a format for the query output Draft the query Execute the query Verify that the query output is as per the required results©NIIT SQL/Lesson 3/Slide 37 of 49
  38. 38. Functions and JoinsCreate a format for the query output Result: The required output from the query is the names of all the external candidates and their recruitment agencies (wherever applicable)©NIIT SQL/Lesson 3/Slide 38 of 49
  39. 39. Functions and JoinsDraft the query Outer Joins A join can be termed an outer join when the result set contains all rows from one table and the matching rows from another Syntax SELECT column_name, column_name [,column_name] FROM table_name [LEFT | RIGHT] OUTER JOIN table_name ON table_name.ref_column_name join_operator table_name.ref_column_name©NIIT SQL/Lesson 3/Slide 39 of 49
  40. 40. Functions and JoinsDraft the query (Contd.) Result: The required information is available in the ExternalCandidate and RecruitmentAgencies tables Therefore, the query using the SELECT statement should be: SELECT vFirstName, vLastName, cName FROM ExternalCandidate LEFT OUTER JOIN RecruitmentAgencies ON ExternalCandidate.cAgencyCode = RecruitmentAgencies.cAgencyCode©NIIT SQL/Lesson 3/Slide 40 of 49
  41. 41. Functions and JoinsExecute the query Action: In the Query Analyzer window, type: Execute the query©NIIT SQL/Lesson 3/Slide 41 of 49
  42. 42. Functions and JoinsVerify that the query output is as per the requiredresults Action: Check whether: ® The required columns are displayed ® All rows from the first table are displayed ® The require rows from the second table are displayed©NIIT SQL/Lesson 3/Slide 42 of 49
  43. 43. Functions and JoinsCross Join A join that includes more than one table using the keyword CROSS is called a cross join. Example SELECT * FROM Titles CROSS JOIN Publishers©NIIT SQL/Lesson 3/Slide 43 of 49
  44. 44. Functions and JoinsEqui Join A join that uses an asterisk (*) sign in the SELECT list and displays redundant column data in the result set is termed as an equi join Example SELECT * FROM Sales s JOIN Titles t ON s.Title_Id = t.Title_Id JOIN Publishers p ON t.Pub_Id = p.Pub_Id©NIIT SQL/Lesson 3/Slide 44 of 49
  45. 45. Functions and JoinsNatural Join A join that restricts the redundant column data from the result set is known as a natural join Example SELECT t.Title, p.Pub_Name FROM Titles t JOIN Publishers p ON t.Pub_Id = p.Pub_Id©NIIT SQL/Lesson 3/Slide 45 of 49
  46. 46. Functions and JoinsSelf Join A join is said to be a self join when one row in a table correlates with other rows in the same table Example SELECT t1.title,t2.title , t1.price FROM titles t1 JOIN titles t2 ON t1.price=t2.price WHERE t1.price=2.99©NIIT SQL/Lesson 3/Slide 46 of 49
  47. 47. Functions and JoinsSummaryIn this lesson you learned that: SQL Server uses string functions, which can be used as part of any character expression. SQL Server includes date functions for performing date parsing and date arithmetic. The CONVERT function is used to change data from one type to another when SQL Server cannot implicitly understand a conversion. SQL Server provides a method of retrieving data from more than one table using joins.©NIIT SQL/Lesson 3/Slide 47 of 49
  48. 48. Functions and JoinsSummary (Contd.) In inner join, data from multiple tables is displayed after comparing values present in a common column. Only rows with values satisfying the join condition in the common column are displayed. A join can be termed an outer join when the result set contains all rows from one table and the matching rows from another. A join that includes more than one table using the keyword CROSS is called a cross join. A join that uses an asterisk (*) sign in the SELECT list and displays redundant column data in the result set is termed as an equi join.©NIIT SQL/Lesson 3/Slide 48 of 49
  49. 49. Functions and JoinsSummary (Contd.) A join that restricts the redundant column data from the result set is known as a natural join. A join is said to be a self join when one row in a table correlates with other rows in the same table.©NIIT SQL/Lesson 3/Slide 49 of 49

×