Database Engineering
  • Need of Database
  • Database and Database Management System
  • What is Relational Database Model ?
  • Installing Postgresql
    • Installing PostgreSQL on Linux
    • Installing PostgreSQL on Windows
    • Installing PostgreSQL on Mac
    • Postgres in Docker
  • What happens during the initial installation ?
    • Roles, Users & Groups
    • More examples on Roles
  • Sample Databases
    • DVD Rental Database
  • Querying Data
    • SELECT Query
    • Column Aliases
    • Order By
    • SELECT DISTINCT
    • DB-TASK-001
  • Filtering Data
    • WHERE ?
    • AND Operator
    • OR Operator
    • LIMIT
    • FETCH
    • IN
    • BETWEEN
    • LIKE
    • SIMILAR TO
    • IS NULL
    • ESCAPE
    • DB-TASK-002
  • Entity Relationship Diagram
    • What is an ER Diagram ?
    • Entity
    • Identifiers
    • Relationship
    • Attributes
    • Cardinality
    • Entity Relationship Diagram Tutorial
    • DB-TASK-003
  • Joins
    • Joins
    • Joins With Employee and Departments
  • Joins on E-Commerce
Powered by GitBook
On this page
  • Basic PostgreSQL AND operator examples
  • Using the AND operator in the WHERE clause
  1. Filtering Data

AND Operator

In PostgreSQL, a boolean value can have one of three values: true, false, and null.

Result

true

true

t

true

'true'

true

'y'

true

'yes'

true

'1'

true

false

false

f

false

'false'

false

'n'

false

'no'

false

'0'

false

A boolean expression is an expression that evaluates to a boolean value. For example, the expression 1=1 is a boolean expression that evaluates to true

SELECT 1 = 1 AS result;
 result 
--------
 t
(1 row)

The letter t in the output indicates the value of true. The AND operator is a logical operator that combines two boolean expressions.

Basic PostgreSQL AND operator examples

  1. The following example uses the AND operator to combine two true values, which returns true

SELECT true AND true AS result;
result
--------
 t
(1 row)
  1. The following statement uses the AND operator to combine true with false, which returns false

SELECT true AND false AS result;
result
--------
 f
(1 row)
  1. The following example uses the AND operator to combine true with null, which returns null

SELECT true AND null AS result;
result
--------
 null
(1 row)
  1. The following example uses the AND operator to combine false with false, which returns false

SELECT false AND false AS result;
result
--------
 f
(1 row)
  1. The following example uses the AND operator to combine false with null, which returns false

SELECT false AND null AS result;
result
--------
 f
(1 row)

Using the AND operator in the WHERE clause

The following example uses the AND operator in the WHERE clause to find the films that have a length greater than 180 and a rental rate less than 1.

SELECT
  title,
  length,
  rental_rate
FROM
  film
WHERE
  length > 180
  AND rental_rate < 1;
title        | length | rental_rate
--------------------+--------+-------------
 Catch Amistad      |    183 |        0.99
 Haunting Pianist   |    181 |        0.99
 Intrigue Worst     |    181 |        0.99
 Love Suicides      |    181 |        0.99
 Runaway Tenenbaums |    181 |        0.99
 Smoochy Control    |    184 |        0.99
 Sorority Queen     |    184 |        0.99
 Theory Mermaid     |    184 |        0.99
 Wild Apollo        |    181 |        0.99
 Young Language     |    183 |        0.99
(10 rows)
PreviousWHERE ?NextOR Operator

Last updated 5 months ago