SQL Constructs : WHERE Clause

Where clause is used to specify condition while retrieving data from table. If condition specified by where clause is true then only the result from table is returned. The Where clause is mostly used with Select, Update and Delete query. We should use the WHERE clause to filter the records and fetching only the necessary records. The basic syntax of the SELECT statement with the WHERE clause is :
 SELECT column_name1, column_name2, column_nameN FROM table_name WHERE [condition];
For example Consider the below table students:
 STUDENT_ID  STUDENT_NAME    CITY       STATE            ZIP
 ---------- --------------- ---------- ---------------- ----------
 101         John            Boston     Massachusetts    600123
 102         Mack            Chicago    Illinois         801022
 103         Bruce           Miami      Florida          700101
 104         Jack            San Jose   California       509342
The following query is an example, which would fetch Student_name, City and State fields from students table, where the student_id is greater then or equal to 102.
 SQL > SELECT student_name, city, state FROM students WHERE student_id >= 102;
The result would be :
 STUDENT_NAME   CITY        STATE               
 ------------- ----------- --------------------
 Mack           Chicago     Illinois            
 Bruce          Miami       Florida             
 Jack           San Jose    California    
Lets take another example, the below query will fetch a particular where student's City is equal to Boston.
 SQL > SELECT * FROM students WHERE City = 'Boston';
The result would be :
 STUDENT_ID   STUDENT_NAME     CITY       STATE             ZIP
 ---------- ----------------- --------- ---------------- ----------
 101           John            Boston     Massachusetts    600123



Next Topic :

No comments:

Post a Comment