Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • PostgreSQL - Get Started
  • Install PostgreSQL
  • Connect to PostgreSQL DB
  • Create Database
  • Create Table
  • Copy Table
  • Drop Table
  • Drop Database
  • Truncate Table
  • ALTER Table
  • Rename Table
  • Rename Columns
  • Add Columns
  • Modify Column Type
  • Set Default Value of Column
  • Remove Columns
  • Add Constraints to Table
  • Insert Data
  • Upsert Data
  • Update Data
  • Delete Data
  • SELECT Statement
  • WHERE Clause
  • GROUP BY Clause
  • HAVING Clause
  • ORDER BY Clause
  • DISTINCT Clause
  • Inner Join
  • Left Outer Join
  • Right Outer Join
  • Full Outer Join
  • Self Join
  • Natural Join
  • Cross Join
  • LIMIT OFFSET Clause
  • GROUPING SETS
  • GROUPING() Function
  • GROUP BY CUBE
  • GROUP BY ROLLUP
  • Sub Query
  • ALL Operator
  • ANY Operator
  • UNION Operator
  • INTERSECT Operator
  • EXCEPT Operator
  • IS NULL Operator
  • BETWEEN Operator
  • LIKE Operator
  • CAST Operator
  • CASE Expressions
  • NULLIF()
  • COALESCE()
  • GREATEST(), LEAST()
  • WITH Queries (CTE)
  • Constraints
  • NOT NULL Constraint
  • Unique Constraint
  • Check Constraint
  • Primary Key
  • Foreign Key
  • Sequence
  • Serial Type
  • Identity Columns
  • Generated Columns
  • Data Types
  • Boolean Type
  • Character Type
  • Integer Type
  • Numeric Type
  • Date Type
  • Time Type
  • TimeStamp Type
  • Interval Type
  • Array Type
  • Json Type
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

PostgreSQL WHERE Clause: Filter Data

The WHERE clause is used to filter the rows in the result. It can be used with SELECT, UPDATE, and DELETE statements.

Syntax:
SELECT <column_list>
FROM <table_name>
[ WHERE <conditions>]
[ GROUP BY ]
[ HAVING condition ]
[ ORDER BY ]

The WHERE clause is specified just after the FROM clause and before the GROUP BY, HAVING and ORDER BY clauses. After the WHERE keyword, you can specify one or more conditions.

The WHERE conditions must evaluate to be true or false to filter the rows. Any row that does not satisfy this condition will be eliminated from the result.

In PostgreSQL, the FROM clause is evaluated first, then the WHERE clause, and then the SELECT clause.

The WHERE condition must use the following one or more comparison and Logical operators.

OperatorDescription
=Equal
!= or <>Not Equal
>Greater than
>=Greater than or equal
<Less than
<=Less than or equal
BETWEENBetween some range
LIKESearch for pattern
INSpecify multiple possible values for a column
OperatorDescription
ANDLogical operator AND
ORLogical operator OR
NOTNegate the result of the operator

Let's see how to use the WHERE clause in the SELECT statement to fetch the data from the following employee table.

The following example filters data using the WHERE clause with the comparison operator equal to =.

Example: WHERE Clause with EqualTo Operator
SELECT *
FROM employee 
WHERE first_name = 'Charlton';

The above SELECT statement retrieves employees whose first_name is Charlton. It used the logical operator = with the WHERE clause WHERE first_name = 'Charlton'. The following shows the result of the above query in pgAdmin.

The following example uses the greater than operator > to retrieve rows where the value in the salary column is greater than 50000.

Example: WHERE Clause with GreaterThan Operator
SELECT * FROM employee
WHERE salary &gt; 50000;

The following shows the result in pgAdmin.

You can use the WHERE clause with the IN operator. The IN operator is used to define a list of values to search for. The following query will return the list of employees whose emp_id = 1 OR emp_id = 6.

Example: WHERE Clause with IN Operator
SELECT * FROM employee
WHERE emp_id IN (1,6);

Multiple conditions

Use the AND and OR operators to specify multiple conditions with the WHERE clause.

The following query will get the list of employees with Gender = F AND Salary > 50000. It will retrieve rows that satisfies both conditions.

Example: Multiple Conditions using AND
SELECT * FROM employee
WHERE gender = 'F' AND salary &gt; 50000;

The following query uses the OR operator to find a list of employees whose gender is Female or salary is greater than 50000. It will retrieve rows that satisfies at least one of the conditions.

Example: Multiple Conditions using OR
SELECT * FROM employee
WHERE gender = 'F' OR salary &gt; 50000;

Thus, you can use the WHERE clause to filter the data.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.