Below are SQL examples that demonstrate how to use AND and OR in the WHERE clause.

SQL WHERE the AND and OR

How to use AND and OR in WHERE clause

The AND is to get records that satisfy all conditions. On the other hand, the OR is to get rows matching either condition.

So limiting rows in SQL [asktom.oracle.com/] output can do it using the where clause with AND and OR. The AND and OR conditions examples here show you how to achieve it.

In the below query, you will get rows, from the INVOICES table, where the invoice date is > 12th JAN 2022 and the Total is less than $5[developer.salesforce.com]

Invoices table

Invoices table
InvoicesTable
SELECT
    InvoiceDate,
    BillingAddress,
    BillingCity,
    Total
FROM
    invoices
WHERE
    DATE(InvoiceDate) > '2022-01-12' AND Total < 5
ORDER BY
    Total
32 Complex SQL Queries

SQL Query: How to use OR operator

The OR operator in the WHERE allows you to get rows that match any criteria. The below query pulls the rows BillingCity’s first letter of ‘p’ or ‘d.’

SELECT
    InvoiceDate,
    BillingAddress,
    BillingCity,
    Total
FROM
    invoices
WHERE
    BillingCity LIKE 'p%' OR BillingCity LIKE 'd%'
ORDER BY
    Total

SQL AND and OR conditions

This query shows you to use both AND and OR. So that you can limit the rows in the output.

SELECT
    InvoiceDate,
    BillingAddress,
    BillingCity,
    Total
FROM
    invoices
WHERE
    Total > 1.98 AND BillingCity LIKE 'p%' OR
    BillingCity LIKE 'd%'
ORDER BY
    Total

Related