SQL Server – DML SELECT Statement

SELECT is the most extensively used word in SQL. Perhaps it is also the most flexible, and powerful word in SQL.

All the data in databases are saved in from of tables. If we want to retrieve the data from the tables we use SELECT statement.  The syntax of SELECT statement:

SELECT *, columns, OR expressions

[FROM table]

[JOIN table

ON condition]

[WHERE conditions]

[GROUP BY columns]

[HAVING conditions]

[ORDER BY Columns];

We shall use the Customer table for the demo purpose.

  • Display all the Column

To display all the content of a table use wild card character ‘*’.

SELECT * FROM Customer;

Customer Table

Customer Table

  • Display only selected Column

Generally it is not required to display all the columns of the table. To display only specific columns of a table, we need to replace ‘*’ with column name’s.

SELECT CustomerID, LastName, City FROM Customer;

Customer Table

Customer Table

We can also the change order in which column get displayed just by changing column order in the SELECT statement.

SELECT LastName, Phone, CustomerID FROM Customer;

Customer Table

Customer Table

  • Display Column name as Alias

We can also give column name alias as per the requirements.

SELECT CustomerID AS ‘ID’,

FirstName AS ‘Name’,

Phone AS ‘Contact Number’

FROM Customer;

Customer Table

Customer Table

See also:

SELECT Statement with WHERE clause

SELECT Statement with WHERE BETWEEN clause

SELECT Statement with WHERE IN clause

SELECT Statement with WHERE LIKE clause

SELECT Statement multiple WHERE conditions

SELECT Statement with ORDER BY clause

Good Luck!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: