Movatterモバイル変換


[0]ホーム

URL:


You don’t need a vector database - just use Postgres for everything. Read the case study on switching from Pinecone to Neon
PostgreSQL Tutorial
PostgreSQL Tutorial

PostgreSQL OR Operator

Summary: in this tutorial, you will learn about the PostgreSQLOR logical operator and how to use it to combine multiple boolean expressions.

Introduction to the PostgreSQL OR operator

In PostgreSQL, aboolean value can have one of three values:true,false, andnull.

PostgreSQL usestrue,'t','true','y','yes','1' to representtrue andfalse,'f','false','n','no', and'0' to representfalse.

A boolean expression is an expression that evaluates to a boolean value. For example, the expression1<>1 is a boolean expression that evaluates tofalse:

SELECT 1 <> 1 AS result;

Output:

result-------- f(1 row)

The letterf in the output indicatesfalse.

TheOR operator is a logical operator that combines multiple boolean expressions. Here’s the basic syntax of theOR operator:

expression1OR expression2

In this syntax,expression1 andexpression2 are boolean expressions that evaluate totrue,false, ornull.

TheOR operator returnstrue only if any of the expressions istrue. It returnsfalse if both expressions are false. Otherwise, it returns null.

The following table shows the results of theOR operator when combiningtrue,false, andnull.

ORTrueFalseNull
TrueTrueTrueTrue
FalseTrueFalseNull
NullTrueNullNull

In practice, you usually use theOR operator in aWHERE clause to ensure that either of the specified expressions must be true for a row to be included in the result set.

PostgreSQL OR operator

Let’s explore some examples of using theOR operator.

1) Basic PostgreSQL OR operator examples

The following example uses theOR operator to combinetrue withtrue, which returnstrue:

SELECT trueOR trueAS result;

Output:

result-------- t(1 row)

The following statement uses theOR operator to combinetrue withfalse, which returns true:

SELECT trueOR falseAS result;

Output:

result-------- t(1 row)

The following example uses theOR operator to combinetrue withnull, which returnstrue:

SELECT trueOR null AS result;

Output:

result-------- t(1 row)

The following example uses theOR operator to combinefalse withfalse, which returnsfalse:

SELECT falseOR falseAS result;

Output:

result-------- f(1 row)

The following example uses theOR operator to combinefalse withnull, which returnsnull:

SELECT falseOR null AS result;

Output:

result-------- null(1 row)

The following example uses theOR operator to combinenull withnull, which returnsnull:

SELECT null OR null AS result;

Output:

result-------- null(1 row)

2) Using the OR operator in the WHERE clause

We’ll use thefilm table from thesample database for the demonstration:

The following example uses theOR operator in theWHERE clause to find the films that have a rental rate is0.99 or2.99:

SELECT  title,  rental_rateFROM  filmWHERE  rental_rate = 0.99 OR  rental_rate = 2.99;

Output:

title            | rental_rate-----------------------------+------------- Academy Dinosaur            |        0.99 Adaptation Holes            |        2.99 Affair Prejudice            |        2.99 African Egg                 |        2.99...

Summary

  • Use theOR operator to combine multiple boolean expressions.

Last updated on

Was this page helpful?
Thank you for your feedback!

[8]ページ先頭

©2009-2025 Movatter.jp