Pages

Saturday 16 May 2015

Data Retrieval and Restriction

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 general form of SELECT statement is as shown below:

       
SELECT department_id, employee_id, first_name

FROM employees

WHERE department_id=60;




WHERE clause usage:

    The WHERE clause is used to restrict the rows returned by SELECT statement. It helps to filter the data as per your requirement.

    The general form of select statement that limits the retrieval of data by using WHERE clause is as shown below:

SELECT  *||([DISTINCT] column||expression [alias],...)

FROM tableName

WHERE condition();


   The WHERE clause is placed right after FROM clause as you can see in the above general form.

 Consider the below SQL query used to retrieve data from EMPLOYEE table:


SELECT department_id, employee_id, first_name

FROM employees;

  The output of the above query is as shown below:



Now let us use WHERE clause to filter the data as per our requirement. Suppose my requirement is to get data only for department_id=60, in that case I can modify above SQL query as shown below:

SELECT department_id, employee_id, first_name

FROM employees

WHERE department_id=60;

The output of the above query is as shown below:



You can see in the above query result, only data for department_id=60 is present.