C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » SQL

SQL - WHERE Clause (to filter data)

SQL - WHERE Clause: Here, we will learn how to filter data from a table (it is used to check conditions).
Submitted by Shubham Singh Rajawat, on November 14, 2017

WHERE clause is used to filter the data by means of conditions. It is used to fetch records that fulfil a certain criteria. WHERE clause can be used with SELECT, UPDATE, DELETE etc.

Syntax:

SELECT column1, column2, ... FROM table_name WHERE condition;

Here,

  • column1, column2,…: are the name of the fields in the table
  • table_name: name of the table from which we want to fetch data
  • condition: this condition is used to filter the data. It can be break into three parts (Column_name operator value).

Sample table (Student),

alter table in sql example

Examples:

1) Fetch Student whose physics marks are 91

SELECT * FROM Student WHERE physics=91;
WHERE CLAUSE in sql example

2) Fetch Student whose DOB is between 1997-06-01 and 1998-11-01

SELECT * FROM Student WHERE DOB BETWEEN '1997-06-01' and '1998-11-01';

Will Fetch student whose DOB lies in between the given dates both inclusive.

WHERE CLAUSE in sql example

3) Fetch Student where city is Delhi and Gwalior

SELECT * FROM Student WHERE City IN ('Gwalior', 'Delhi');

Will fetch students who live in Gwalior and Delhi.

WHERE CLAUSE in sql example

4) Fetch Student whose name start with 'a'

SELECT * FROM Student WHERE Student_name LIKE 'A%';

This will fetch students whose name starts with 'A'.

WHERE CLAUSE in sql example




Sponsored Links




COMMENTS