x

SQL – WHERE Clause

Prev     Next

SQL – WHERE Clause is used with SELECT, UPDATE and DELETE statements/queries to select, update and delete only particular records from a table those meet the condition given in WHERE   clause. I.e. It filters records from a table as per the where condition. Syntax for SQL WHERE clause is given below.


SQL Syntax for SQL – WHERE Clause:

Syntax for SQL WHERE clause SELECT column_name1, column_name2, etc
FROM table_name
WHERE  [condition];

Where [condition] should be in the following format.

[column_name] [Operator] [Value];

Where,
[column_name] – Any one of the column names in the table.
[Operator] – Any one of the following (>, <, =, >=, <=, NOT, LIKE etc)
[Value] – User defined value.

Example for how to use the SQL WHERE clause in SELECT queries:

Please consider the following table with few records as given below.

Table name (for example): student
Column names in this table: Student_ID, Student_name, City and Age
Available records: 4 rows


sql-table1

Example1: Select SQL query with WHERE clause:

SQL query:

SELECT Student_ID, Student_name, City, Age from student
WHERE City=‘Pune’;

SQL query Output:

sql-table14

Example2: Select SQL query with WHERE clause:

SQL query:

SELECT Student_ID, Student_name, City, Age from student
WHERE Age=26;

SQL query Output:

sql-table15

Please note that in above 2 select SQL queries, only particular records were fetched, which were matched with WHERE conditions. Other records were ignored by SELECT queries.

Prev     Next



Like it? Please Spread the word!