Pages

Thursday 17 October 2013

SELECT command of SQL



      The select command of SQL is used to retrieve data from the tables of the database and display those data in a table format.

      The capabilities of SQL select statements are as mentioned below

1.      Projection
2.      Selection
3.      Join

      The general form of select statement is shown below. 

               
SELECT  *|{[DISTINCT] column|expression [alias],...}
  
FROM tableName;
        
       
      In above general form:

1.      The SELECT indentifies which column to select from the table specified.

2.      The FROM indentifies which table to chose from the database.

      Now lets’ analyze the statement SELECT  *|{[DISTINCT] column|expression [alias],...}, first we will start with the ‘ * ‘ operator. 

 The ‘ * ‘ operator in the SELECT statement:

     When the ‘ * ‘ operator is used all the columns and its respective values present in the table are retrieved. The below example shows you the use of ‘ * ‘ operator.


select * from departments;
       

     Here, all the columns and its respective data from the departments table is retrieve and displayed as shown in the below table image.




The ‘ DISTINCT ‘ keyword used in the SELECT statement:

     The DISTINCT keyword is used to eliminate the duplicate rows in the result of the select statement.
 
      Consider the below query which is written without DISTINCT keyword. 

        
select department_id

from employees;
  

      You can see the output where the ‘ department_id ‘ column displays many rows with duplicate values.
       
      


                   
      Now consider the same query where duplicate rows are eliminated using DISTINCT keyword.

select DISTINCT department_id

from employees;
  
     In the result you can see all the duplicate values displayed in previous result is eliminated, only the distinct values are displayed.