SQL SELECT Statement

SQL SELECT Statement

To select all the columns from a Table, so it is converted into

SELECT *
FROM Production.Product;

In Microsoft SQL Server, you follow the convention as –

FROM DatabaseName.SchemaName.TableName

Suppose your database name is AdventureWorks2017, Schema Name is Production and Table name is Product and you are already connected to the database, then you do not need to type – 

FROM AdventureWorks2017.Production.Product

because you are already connected to AdventureWorks2017 database.

So, you can simply code –

FROM Production.Product

 

Order Of execution in SQL

Even though the SELECT statement comes first but the SQL server Engine considers the FROM clause first and then processes the SELECT clause.

So, when you code –

SELECT *
FROM Production.Product;

Microsoft SQL Server engine will process the FROM clause first and then followed by SELECT clause.

i.e. 1.FROM -> 2.SELECT

So, when you code –

SELECT *
FROM Production.Product;

It is same as –

Select *
FROM Production.Product;

or, it is same as –

SELECt *
FroM Production.Product;

Tips
But, I strongly suggest to use Capital letters for all the SQL keywords like SELECT, FROM, etc.

Selecting Individual column or multiple columns

If you wish to select all the records of ‘CountryRegionCode’ column from ‘CountryRegion’ table which is present under the ‘Person’ schema, then you can code the SQL Query as –

SELECT CountryRegionCode
FROM Person.CountryRegion;

If you also wish to select all the records of ‘Name’ and ‘CountryRegionCode’ columns from ‘CountryRegion’, then you can code the SQL Query as –

SELECT CountryRegionCode, Name
FROM Person.CountryRegion;

Selecting column which has spaces in it

In Microsoft SQL Server, if you have a column name that contains a space, then, you have to surround that particular column name within square brackets.

For example, if you have a table with the name as ‘Customers’ under the Schema ‘Person’ and you have a column with the name as ‘First Name’
Then, you have to select the records from that particular column name as –

SELECT [First Name]
FROM Person.Customers;

Literal SELECT

It is important to understand that if you code column names inside 2 single quotes, then it is called Literal SELECT. Literal select means select ‘AS IT IS’. For Example –

Example

SELECT 'First Name' 
FROM Person.Person;

Tutorials for all brains!