Writing a select statement is the first step towards learning SQL,
for each task to do there are million ways to do. let's see the most commonly
used ways to write a select statement.
SQL SELECT statements are used to fetch the
data from an object (table, view or function, etc.) and results are shown by
the criteria you select.
1.Selecting everything from a table, most common syntax and
highly used syntax
SELECT * From dbo.TableA
2.Selecting a set of specific columns you need from a table
SELECT Col1, Col2,
Col3,...ColN From dbo.TableA
3.Selecting Top N rows, here you can fetch top 'N' Number of rows,
this gives you a specific number of rows in the result set. So, that it can
give you a sample look of what the table looks like instead of getting all the
rows into the result set.
SELECT TOP 10 *
FROM dbo.TableA
4.Selecting everything by specifying 100 percent as qualifier in
selecting the rows from a table, this is useful when you want to order by in a view,
subquery, in-line functions, derived tables and common table expressions.
SELECT TOP 100 PERCENT * FROM dbo.TableA
5. Select using four part naming convention or fully qualified naming standards, this query shows how to reference a table you want to access from specified server, specified database ,schema and table.
SELECT TOP 100 PERCENT
FROM dbo.TableA
Sample Query to execute all the five cases:
CREATE TABLE dbo.TableA
(
[Col1] VARCHAR(9)
,[Col2] INT
,[Col3] INT
);
INSERT INTO dbo.TableA
([Col1], [Col2],
[Col3])
VALUES
( 'Zone1',
1, 100),
( 'Zone2',
1, 200),
( 'Zone3',
2, 1250),
( 'Zone4',
3, 1440),
( 'Zone5',
4, 1445),
( 'Zone6',
4, 3250),
( 'Zone7',
5, 4440);
SELECT * FROM dbo.TableA
SELECT
Col1,
Col2,
Col3
FROM dbo.TableA
SELECT TOP 10 * FROM dbo.TableA
SELECT TOP 100 PERCENT * FROM dbo.TableA
SELECT * FROM [yourServerName].[DatabaseName].dbo.TableA
0 comments:
Post a Comment