MS SQL Server
  Home arrow MS SQL Server arrow Page 2 - How to Retrieve Data from a Single Table
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MS SQL SERVER

How to Retrieve Data from a Single Table
By: Bryan Syverson
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 27
    2004-06-01

    Table of Contents:
  • How to Retrieve Data from a Single Table
  • SELECT Statement Examples
  • How to Code the SELECT Clause
  • How to Name the Columns in a Result Set
  • How to Code String Expressions
  • How to Code Arithmetic Expressions
  • How to Use Functions
  • How to Eliminate Duplicate Rows
  • How to Return a Subset of Selected Rows
  • How to Code the WHERE Clause
  • How to Use the AND, OR, and NOT Logical Operators
  • How to Use the IN Operator
  • How to Use the BETWEEN Operator
  • How to Use the LIKE Operator
  • How to Use the IS NULL Clause
  • How to Code the ORDER BY Clause
  • How to Sort a Result Set by an Alias, an Expression, or a Column Number

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    How to Retrieve Data from a Single Table - SELECT Statement Examples


    (Page 2 of 17 )

    Figure 2 presents five SELECT statement examples. All of these statements retrieve data from the Invoices table. If you aren't already familiar with this table, you should use the Enterprise Manager as described in the last chapter to review its definition. 

    The first statement in this figure retrieves all of the rows and columns from the Invoices table. Here, an asterisk (*) is used as a shorthand to indicate that all of the columns should be retrieved, and the WHERE clause is omitted so that there are no conditions on the rows that are retrieved. Notice that this statement doesn't include an ORDER BY clause, so the rows are in primary key sequence. You can see the results following this statement as they're displayed by the Query Analyzer. Notice that both horizontal and vertical scroll bars are displayed, indicating that the result set contains more rows and columns than can be displayed on the screen at one time.

    The second statement retrieves selected columns from the Invoices table. As you can see, the columns to be retrieved are listed in the SELECT clause. Like the first statement, this statement doesn't include a WHERE clause, so all the rows are retrieved. Then, the ORDER BY clause causes the rows to be sorted by the InvoiceTotal column in descending sequence. 

    The third statement also lists the columns to be retrieved. In this case, though, the last column is calculated from two columns in the base table, CreditTotal and PaymentTotal, and the resulting column is given the name TotalCredits. In addition, the WHERE clause specifies that only the invoice whose InvoiceID column has a value of 17 should be retrieved.  The fourth SELECT statement includes a WHERE clause whose condition specifies a range of values. In this case, only invoices with invoice dates between 05/01/2002 and 05/31/2002 will be retrieved. In addition, the rows in the result set will be sorted by invoice date.

    The last statement in this figure shows another variation of the WHERE clause. In this case, only those rows with invoice totals greater than 50,000 are retrieved. Notice that since none of the rows in the Invoices table satisfy this condition, the result set is empty.

    Figure 2: A SELECT statement that retrieves all the data from the Invoices table

    SELECT *
    FROM Invoices

    bryan

    (114 rows)

    A SELECT statement that retrieves three columns from each row, sorted in descending sequence by invoice total:

    SELECT InvoiceNumber, InvoiceDate, InvoiceTotal
    FROM Invoices
    ORDER BY InvoiceTotal DESC

    bryan
    (114 rows)

    A SELECT statement that retrieves two columns and a calculated value for a specific invoice:

    SELECT InvoiceID, InvoiceTotal, CreditTotal + PaymentTotal AS TotalCredits
    FROM Invoices WHERE InvoiceID = 17

     

    sysercon

    A SELECT statement that retrieves all invoices between given dates:

    SELECT InvoiceNumber, InvoiceDate, InvoiceTotal
    FROM Invoices
    WHERE InvoiceDate BETWEEN '2002-05-01' AND '2002-05-31'
    ORDER BY InvoiceDate

    bryan
    (70 rows)

    A SELECT statement that returns an empty result set:

    SELECT InvoiceNumber, InvoiceDate, InvoiceTotal
    FROM Invoices
    WHERE InvoiceTotal > 50000

    bryan 

    This is from chapter three of Murach's SQL for SQL Server by Syverson (Murach, ISBN 1-890774-16-2, 2002). Grab a copy at your favorite book store today! Buy this book now.

    More MS SQL Server Articles
    More By Bryan Syverson


     

    MS SQL SERVER ARTICLES

    - Completing the Introduction to Transact-SQL
    - A Brief Introduction to Transact-SQL
    - Lookups and Blocking Bad Data
    - Field Validation Rules for Blocking Bad Data
    - Using Masks to Block Bad Data
    - Blocking Bad Data
    - Using @@ROWCOUNT and TABLE Variables for Dat...
    - How to Use Variables, IF and CASE in Databas...
    - Creating Important Aspects of Notification S...
    - Working wth Variables in Database Interactio...
    - Delving Deeper into Notification Services
    - Notification Services
    - Building a Multi-table Report with SQL 2005 ...
    - A Secure Way of Building Connection Strings
    - Transferring a Database Using the SSIS Desig...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT