1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

All About Select Statement Part 1

Discussion in 'SQL Server' started by Sagar Jaybhay, Dec 9, 2019.

Tags:
  1. Sagar Jaybhay

    Sagar Jaybhay New Member

    Joined:
    Jan 28, 2019
    Messages:
    29
    Likes Received:
    17
    Trophy Points:
    3
    Gender:
    Male
    Occupation:
    Sr. Software Developer
    Location:
    Pune
    Home Page:
    The SQL SELECT Statement

    The select clause can retrieve 0 or more rows from one or more tables from the database or it can retrieve rows from views also.

    The select statement is used to select data from a table or database. The data which is our of our select query is called a result set. The select is a commonly used statement in SQL. Means for fetching the data from the database we use Select statement. We can retrieve all rows or a selected row means this is according to 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 which are 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:
    Code:
    Select column1, column2,…. From table_name;  
    Ex
    Code:
    Select * from Person; 
    The above query will fetch all the data from the table along with all columns.


    Distinct Clause in Select Statement

    To select a distinct value from column value you can use distinct.


    General Syntax


    Code:
     Select distinct column_name from table_name; 
    Ex


    Code:
    SELECT distinct [genederID] FROM [dbo].[Person]  
    When you specify multiple columns in a distinct keyword you tell SQL server to check and get distinct value from that number of column you provides.


    General Syntax:


    Code:
     Select distinct column1, column2 from table_name; 
    Ex


    Code:
     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



    1. SELECT column1, column2, ...
    2. FROM table_name
    3. WHERE condition;


    Code:
     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 from clause is evaluated after that cross join or cartesian join is produced for these 2 tables and this from clause produced a virtual table might call as 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 full fill these conditions are inserted into another Vtable2.

    3) If you specify outer join then the rest or remaining records from Vtable2 are inserted into Vtable3.

    4) After this where clause is applied and the lastname=’Jaybhay’ and firstname=’sagar’ are verified or taken and put it into Vtable4.

    5) After this select list is evaluated and return Vtable4.
     
    shabbir likes this.

Share This Page