Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Sorting output.

Similar presentations


Presentation on theme: "Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Sorting output."— Presentation transcript:

1 Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Sorting output (ORDER BY) Column aliases Concatenating columns Calculated columns 1 Microsoft Enterprise Consortium Microsoft Enterprise Consortium: http://enterprise.waltoncollege.uark.eduhttp://enterprise.waltoncollege.uark.edu Microsoft Faculty Connection/Faculty Resource Center http://www.facultyresourcecenter.comhttp://www.facultyresourcecenter.com

2 Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium What you’ll need … Log in to MEC for this lesson and into MSSMS (Microsoft SQL Server Management Studio). ◦Be sure to select your account ID under Database in the Object Explorer pane, similar to the example shown here. If there is something besides the topics for this lesson that you’re not familiar with, please review earlier lessons in this SQL Fundamental series. 2

3 Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Sorting output The more data your query displays the more likely you’ll want to sort the output. ORDER BY – You add the ORDER BY clause to the SELECT statement to sort output. SELECT,, FROM WHERE = ORDER BY, ; The trick is to remember to not use the word SORT but ORDER. 3

4 Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Sort output from the Student-Team database List students and their team ID. Sort by team ID. /* List students and their team ID. Sort by team ID. */ select std_teamID, stdmajor, stdfname, stdlname from students order by std_teamID ; 4

5 Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Sort by more than one column. List students by team and major. /* List students and their team ID. Sort by team ID and major. */ select std_teamID, stdmajor, stdfname, stdlname from students order by std_teamID, stdmajor ; /* Another way to identify which columns to sort by using numbers. */ select stdfname, stdlname, stdmajor, std_teamID from students order by 4, 3 ; 5

6 Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Column aliases improve readability Columns in the output display can show standard text rather than field names. /* Use column aliases column headings in output. */ select std_teamID "Team", stdmajor "Major", stdfname "First Name", stdlname "Last Name" from students order by std_teamID, stdmajor; 6

7 Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Combine columns into a single output column Sometimes you can improve output readability by combining columns, also known as concatenated columns. 7 /* Combine 2 columns into one (concatenate columns). */ select std_teamID "Team", stdmajor "Major", stdfname + ' ' + stdlname "Student" from students order by 1, 2; select std_teamID "Team", stdmajor "Major", stdlname + ', ' + stdfname "Student" from students order by 1, 2, 3;

8 Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Creating a calculated column The concatenated column we just created in one example of a calculated column. You can also actually do calculations. 8 /* Add a calculated column. */ select eval_id, eval_score_id, eval_item_id, score, score *.1 "Points Earned" from eval_items_scores order by eval_id, eval_score_id, eval_item_id;

9 Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium What was covered … Sorting query output using ORDER BY. Using column aliases to make easy-to-read column headings, Using calculated columns. ◦Concatenating text columns into a single display column. ◦Calculating values and displaying them. 9

10 Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Resources http://enterprise.waltoncollege.uark.edu/mec.asp Microsoft Faculty Connection—Faculty Resource Center http://www.facultyresourcecenter.com/ http://www.facultyresourcecenter.com/ Microsoft Transact-SQL Reference http://msdn.microsoft.com/en-us/library/aa299742(v=SQL.80).aspx AdventureWorks Sample Database http://msdn.microsoft.com/en-us/library/ms124659%28v=sql.100%29.aspx 10


Download ppt "Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Sorting output."

Similar presentations


Ads by Google