13.07.2015 Views

ASP.NET 3.5: A Beginner's Guide - www.mustafaof.com

ASP.NET 3.5: A Beginner's Guide - www.mustafaof.com

ASP.NET 3.5: A Beginner's Guide - www.mustafaof.com

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 11: A SQL Primer 279Looking into a Table with SELECT and FROMOnce you have a table and have added data, you can send a SQL query to look into a tableand pull out whatever you want. The first SQL <strong>com</strong>mand for making queries is SELECT.It specifies the fields that you want to query, that is, to find out what their values are. TheSELECT <strong>com</strong>mand needs a FROM clause to specify the table where the query will occur.The basic format isSELECT field1, field2 FROM TableNameYou can examine as many or as few fields as you want. In the example table shown, youmight haveSELECT LastName, Donation FROM CodeTableIf you want to look at all of them, you can use the * wildcard symbol. For example,SELECT * FROM CodeTableexamines all of the columns in the table, including the SupID field that was not specifiedin the first query of the table.The WHERE FilterOften a query is to locate certain elements in your database that have specific values ora range of values. The general format for the WHERE filter isSELECT field1, field2 FROM TableName WHERE field conditionFor example, you might want to find the donors in a political campaign who have donatedat least $200. Using the example table, the filtered query would beSELECT SupID, LastName FROM CodeTable WHERE Donation =>200The query returns not only the LastName value of all donors who gave at least $200, itprovides their ID. In this way, you can distinguish the Mary Smith who gave $50 from theMary Smith who gave $5,000.You can refine a query by adding an AND clause to the WHERE filter. For example,suppose you want to find all of the donors who gave $200 or more and have a last nameof Smith:SELECT SupID, LastName FROM CodeTable WHERE Donation >=200 AND LastName="Smith"

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!