/
SQL Fundamentals Sorting output (ORDER BY) SQL Fundamentals Sorting output (ORDER BY)

SQL Fundamentals Sorting output (ORDER BY) - PowerPoint Presentation

luna
luna . @luna
Follow
30 views
Uploaded On 2024-02-03

SQL Fundamentals Sorting output (ORDER BY) - PPT Presentation

Column aliases Concatenating columns C alculated columns 1 Microsoft Enterprise Consortium Microsoft Enterprise Consortium httpenterprisewaltoncollegeuarkedu Microsoft Faculty ConnectionFaculty Resource Center ID: 1044587

column team columns select team column select columns output std sort stdmajor teamid eval studentsorder stdfname sql order calculated

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "SQL Fundamentals Sorting output (ORDER B..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

1. SQL FundamentalsSorting output (ORDER BY)Column aliasesConcatenating columns Calculated columns 1Microsoft Enterprise ConsortiumMicrosoft Enterprise Consortium: http://enterprise.waltoncollege.uark.eduMicrosoft Faculty Connection/Faculty Resource Center http://www.facultyresourcecenter.com

2. 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. Sorting outputThe 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 <column1>, <column2>, <column3>FROM <tablename>WHERE <column1> = <criterion>ORDER BY <column1>, <column2>;The trick is to remember to not use the word SORT but ORDER.3

4. Sort output from the Student-Team databaseList students and their team ID. Sort by team ID. /* List students and their team ID. Sort by team ID. */select std_teamID, stdmajor, stdfname, stdlnamefrom studentsorder by std_teamID;4

5. 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, stdlnamefrom studentsorder by std_teamID, stdmajor;/* Another way to identify which columns to sort by using numbers. */select stdfname, stdlname, stdmajor, std_teamIDfrom studentsorder by 4, 3;5

6. Column aliases improve readabilityColumns 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 studentsorder by std_teamID, stdmajor;6

7. Combine columns into a single output columnSometimes 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 studentsorder by 1, 2;select std_teamID "Team", stdmajor "Major", stdlname + ', ' + stdfname "Student"from studentsorder by 1, 2, 3;

8. Creating a calculated columnThe 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_scoresorder by eval_id, eval_score_id, eval_item_id;

9. 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. Resourceshttp://enterprise.waltoncollege.uark.edu/mec.aspMicrosoft Faculty Connection—Faculty Resource Center http://www.facultyresourcecenter.com/Microsoft Transact-SQL Referencehttp://msdn.microsoft.com/en-us/library/aa299742(v=SQL.80).aspxAdventureWorks Sample Databasehttp://msdn.microsoft.com/en-us/library/ms124659%28v=sql.100%29.aspx10