The SQL Select Statement

Introduction
 
The select clause can retrieve 0 or more rows from one or more tables from the database, or it can also retrieve rows from views. The select statement is used to select data from a table or database. Data from our select query is called a result set. Select is a commonly used statement in SQL. We use the Select statement to fetch data from the database  We can retrieve all rows or a selected row, according to the condition. If you want to specify a selected column or we want specific columns, you can specify in a select clause.
 
The select statement has many optional clauses, as follows:
 
1) Where: it specifies a condition to which rows to retrieve
2) Group By: used to group similar items based on conditions
3) Having: select the rows among the group
4) Order by: it will specify the order
5) As: it is used for an alias
 
Select Syntax:
 
Select column1, column2,…. From table_name;
 
Example
  1. Select * from Person;  
  
The above query will fetch all the data from the table along with all columns.
 
Distinct
 
To select a distinct value from column value you can use distinct.
 
General Syntax
 
Select distinct column_name from table_name;
 
Example
  1. SELECT distinct [genederID] FROM [dbo].[Person]  
 
When you specify multiple columns in a distinct keyword, you tell SQL server to check and get a distinct value from the column number you provided.
 
General Syntax:
 
Select distinct column1, column2 from table_name;
 
Example
  1. SELECT distinct [genederID],[email] FROM [dbo].[Person];  
 
How to Filter Values in Select statement?
You can filter the value by using where clause.
 
Where:
The where clause is used to filter records. By using where clause you can extract only those records which fulfilled our condition. The where clause not only used in a select statement but also used in Update, Delete also
 
SELECT column1, column2, ...
FROM table_name
WHERE condition;
 
Example 
  1. SELECT * FROM [dbo].[Person] where email='Dhanu@gmail.com'  
Select Query Evaluation
 
  1. select g.*  
  2. from users u inner join groups g on g.Userid = u.Userid  
  3. where u.LastName = 'Jaybhay'  
  4. and u.FirstName = 'Sagar'  
 
1) In the above query, the from clause is evaluated after the cross join or cartesian join is produced for these 2 tables. This from clause produces a virtual table that may be called Vtable1.
 
2) After this on clause is evaluated for Vtable1 and it checks to join condition g, Userid =u.userid, then the records which met these conditions or fulfill these conditions are inserted into another Vtable2.
 
3) If you specify the outer join then the rest or remaining records from Vtable2 are inserted into Vtable3.
 
4) After this, the where clause is applied and the lastname=’Jaybhay’ and firstname=’sagar’ are verified or taken and put it into Vtable4.
 
5) After this, the select list is evaluated and returned to Vtable4