2. Filtering Data
Where
(filter rows based on a specified condition)
SELECTstatement returns all rows from one or more columns in a table. To select rows that satisfy a specified condition, we use aWHEREclauseIt appears right after
FROMclause.The condition must evaluate to true, false or unkown. It can be used with boolean expressions like
ANDandORoperatorsIt order of evalutation is as follows
NOTE: You can't use COLUMN ALIAS with WHERE clause
Comparison Operators
=
Equal
>
Greater than
<
Less than
>=
Greater than or equal
<=
Less than or equal
<> or !=
Not equal
AND
logitcal operator AND
OR
logical operator OR
IN
Return true if a value matches any value in a list
BETWEEN
Retruns true if a value is between a range of values
LIKE
Returns true if a value matches a pattern
IS NULL
Returns true if a value is NULL
NOT
Negate the result of other operators
Example 1 (Using WHERE with IN operator)
Example 2 (Using WHERE with LIKE operator)
The % is called a wildcard that matches any string
Example 3 (Using WHERE with BETWEEN operator)
Limit
(get a subset of rows generated by a query)
It is the optional clause of the
SELECTstatement that contraints no of rows returned by the queryIf we want to skip number of rows before returning the limited rows, we can use the
OFFSETclause placed after theLIMITclause
Example 1
Example 2
Fetch
(limit the number of rows returned by a query)
LIMITclause is not a SQL-standard,FETCHis a SQL standardIf you plan to make your application compatible with other database systems, you should use the
FETCHclause because it follows the standard SQL.Its syntax is like this
ROWis the synonym forROWS,FIRSTis the synonym forNEXT. So you can use them interchangeablyThe
startis an integer that must be zero or positive. By default, it is zero if theOFFSETclause is not specified. In case thestartis greater than the number of rows in the result set, no rows are returned;The
row_countis 1 or greater. By default, the default value of row_count is 1 if you do not specify it explicitly.
Example 1

Example 2

In
(select data that matches any value in a list of values)
You use
INoperator in theWHEREclause to check if a value matches any value in a list of values.The list of values can be list of literal values such as numbers, strings or a result or
SELECTstatement like thisvalue IN (SELECT column_name FROM table_name);
Example 1
Example 2 (Get Customers information from customer table whose return date from rental table is '2005-05-27')
The following query returns a list of customer ids from the rental table with the return date is 2005-05-27
Because, it returns a list of values, we can use it in input of IN operator
Between
(select data that is a range of values)
You use the BETWEEN operator to match a value against a range of values. The following illustrates the syntax of the BETWEEN operator:
value BETWEEN low AND high;
Example 1

Like
(filter data based on pattern matching)
It is used for string pattern matching
Percent sign
%matches any sequence of zero or more characters.Underscore sign
_matches any single character.PostgreSQL supports the
ILIKEoperator that works like the LIKE operator. In addition, the ILIKE operator matches value case-insensitively.PostgreSQL also provides some operators that act like the
LIKE,NOT LIKE,ILIKEandNOT ILIKEoperator as shown below:
Example 1
Example 2

Is Null
(check if a value is null or not)
Used for NULL comparison
Example 1 (Find all contacts whose phone number is NULL)
Last updated

