2 Top SQL Queries Usage of AND and OR

Here’re SQL queries that explain how to use AND and OR and both conditions in the WHERE clause.

SQL WHERE the AND and OR

The purpose of AND is for all conditions to satisfy. On the other hand, the OR is to get rows that have any one condition satisfied.

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.

SQL AND condition

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 OR condition

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

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.