Saturday 16 June 2012

Select Query

This article explains fundamental use of select query. It describes it’s select query syntax with different examples.

Select statement is used to retrieve data for viewing from SQL server. It is a powerful command, as it can retrieve data in any order, from any number of columns, from any table that we have the authority to retrieve data from, perform calculations on that data during data retrieval, and even include data from other tables.

 

Syntax for Select Query in SQL Server

SELECT [ ALL | DISTINCT ]
[ TOP expression [ PERCENT ] [ WITH TIES ] ]
{
*
| { table_name | view_name | alias_name }.*
| { column_name | [ ] expression | $IDENTITY | $ROWGUID }
[ [ AS ] column_alias ]
| column_alias = expression
} [ ,...n ]
FROM table_name | view_name alias_name
WHERE filter_criteria
ORDERBY ordering_criteria

Example for Select Query in SQL Server

SELECT * FROM shippers

Output
ShipperID   CompanyName         Phone
1        Speedy Express    (503) 555-9831
2        United Package    (503) 555-3199
3        Federal Shipping    (503) 555-9931

Above example shows that how can we retrieve all columns datafrom a single table. We can also retrieve datafrom more than one tablefor example 
SELECT * FROM shippers,products



SELECT productid,productname,unitprice FROM products

Output
ProductID    ProductName                    UnitPrice

1            Chai                            18.00
2            Chang                           19.00
3            Aniseed Syrup                   10.00
4            Chef Anton's Cajun Seasoning    22.00
5            Chef Anton's Gumbo Mix          21.35
6            Grandma's Boysenberry Spread    25.00
7            Uncle Bob's Organic Dried Pears 30.00
8            Northwoods Cranberry Sauce      40.00
9            Mishi Kobe Niku                 97.00
10           Ikura                           31.00

Above example explains how can we get selected columns/fields from particular table.We can also retrieve selected columns/fields from more than one tablefor 
example 
select ShipperID,CompanyName,ProductID,ProductName from Shippers,Products   

 

No comments:

Post a Comment