MS SQLSERVER:Retrieving Data From A Database

716 views

Published on

MS SQLSERVER:Retrieving Data From A Database

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

No Downloads
Views
Total views
716
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

MS SQLSERVER:Retrieving Data From A Database

  1. 1. 8<br />SQL SERVER: RETREIVINGDATA FROM A DATABASE<br />
  2. 2. Retrieving Data from Database<br />How to retrieve data from the given database?<br />After creating the database structure, customizing it to fit the needs and feeding it with data, the next step is to retrieve required data for performing various operations.<br />So how do we get these data?<br />The answer: <br />Just like in English language, Microsoft SQL Server also provides us the basic keyword for retrieval of data called “Select”<br />SELECT<br />
  3. 3. Select Syntax<br />The Syntax of the select statement is simple:<br />The basic syntax for this DML ( Data manipulation Language) command is <br />select &lt;fieldName1, fieldName2,…&gt; from &lt;table name&gt;;<br />This will select the appropriate columns for the given table. As it is a DML command, it is not necessary to use the keyword TABLE(,as in ‘alter table’)<br />
  4. 4. Displaying the entire table<br />What if it is needed to display all the columns?<br />The Syntax is:<br />select * from tablename;<br />‘*’, the most widely used wildcard comes to our aid. Isn’t it much better than remembering all the column names.<br />Example: select * from student; displays the entire student table<br />
  5. 5. Selecting selective records<br />How to display only certain rows containing the appropriate column?<br />SQL Server answers this question with two simple commands: <br /><ul><li>where
  6. 6. like</li></ul>select * from tablename where &lt;conditions&gt;;<br />select * from tablename where fieldNamelike &lt;conditions&gt;;<br />
  7. 7. LIKE explained<br />SQL Command:<br />Select * from cartoon<br /> where cartoonName like ‘Laughs and Gags’<br />The Where clause:<br /><ul><li> Where is used to specify a condition for the Query
  8. 8. For string matching, use the ‘like’ keyword. Here, wild card characters(% and _)</li></ul>% -character represents a group(or single) of characters<br /> _ -character represents a single character<br />Illustration:<br />To Update cartoons whose names start with the letter ‘a’ must be given as <br />Update cartoon set rating=100 <br /> where cartoonName = ‘a%’<br />
  9. 9. LIKE explained<br />Illustration:<br />To Update cartoons whose names start with the letter ‘a’ and the name must of 3 characters in length:<br />Update cartoon set rating=100 <br /> where cartoonName = ‘a__’<br />
  10. 10. Filter-out duplicate selections<br />Now, we have a problem. What if there are two records have the same data? The select command will select both the records. To avoid this the distinct keyword is used.<br />select distinct &lt;col&gt; from tablename;<br />Note:<br /> Use distinct only for a single column.<br />
  11. 11. Grouping of records<br />In SQL, it is possible to group records based on the values of a particular field.<br />Syntax:<br />Select * from &lt;tableName&gt; group by &lt;fieldName&gt;;<br />Example: <br />To display the details of students, grouped based on their class, the command will be:<br />Select * from student group by class;<br />
  12. 12. Ordering of records<br />In SQL, it is possible to sort records based on the values of a particular field.<br />Syntax:<br />Select * from &lt;tableName&gt; order by &lt;fieldName&gt;;<br />Example: <br />To display the details of students, sorted based on their GPAs, the command will be:<br />Select * from student order bygpa;<br />The default ordering is ascending in nature. To make it into descending, use the desc keyword. Eg:<br />Select * from student order bygpadesc;<br />
  13. 13. Summary<br />8. Retrieving data from a database:<br /><ul><li> Select Syntax
  14. 14. Where
  15. 15. Like
  16. 16. Distinct
  17. 17. Group by
  18. 18. Order by</li></li></ul><li>Visit more self help tutorials<br />Pick a tutorial of your choice and browse through it at your own pace.<br />The tutorials section is free, self-guiding and will not involve any additional support.<br />Visit us at www.dataminingtools.net<br />

×