How to use the AND Condition with the OR Condition in SQL
Aug 13, 2023
How to use the AND Condition with the OR Condition in SQL
Learn how to use the AND Condition with the OR Condition in SQL. This tutorial demonstrates how to write a query combining the AND condition with the OR condition in a single SQL query. We also explain how to let the database know which order to evaluate your conditions so that your query returns the correct results. SQL Combining the AND and OR Conditions (and sample data):https://www.techonthenet.com/sql/and _… SQL AND Condition:https://www.techonthenet.com/sql/and.php SQL OR Condition:https://www.techonthenet.com/sql/or.php SQL Tutorials:https://www.techonthenet.com/sql/inde … Don’t forget to subscribe to our channel for more great SQL tutorials! Visit us at: https://www.techonthenet.com or follow us on: Facebook: https://www.facebook.com/techonthenetcom Twitter: https://twitter.com/tech_on_the_net Pinterest: https://www.pinterest.com/techonthene …
Content
0.04 -> The AND condition and the OR condition
can be used together in a single SQL query
6.62 -> These conditions can be used in a
SELECT, INSERT, UPDATE or DELETE statement
12.68 -> When combining the AND condition with the OR condition, it is important to use parentheses
18.08 -> so the database knows what order
to evaluate each condition
22.12 -> Just like when you were learning
the order of operations in math class
26.06 -> The best way to understand this concept
is to look at an example
29.54 -> So let's get started
31.34 -> Here we have a table called suppliers
containing the following data
36.14 -> We'll start our query by entering
SELECT * FROM suppliers
40.74 -> This tells the database to select all columns from
the suppliers table and return a result set
46.94 -> Next we will enter the WHERE keyword
to start our WHERE clause
51.06 -> In this example, we will test to see
if the state is equal to California
54.76 -> and the supplier_id is
not equal to 900
58.28 -> Since we use the AND keyword,
both of these conditions must be TRUE
62.8 -> for the record to be included
in the result set
65.66 -> Notice that we enclose these
conditions in parentheses
69.04 -> This tells the database to evaluate
the two conditions together
72.34 -> before applying any other condition
in our WHERE clause
76.08 -> Next, we will add the OR keyword
and enter a second condition to test for
80.84 -> Now we will test to see if
the supplier_id is equal to 100
85.22 -> This condition is enclosed in parentheses
so that the database knows to evaluate it first
90.78 -> before applying any other conditions
93.34 -> The inclusion of parentheses
in our query lets the database know
97.18 -> that there would be two main
conditions in the query
100.48 -> Here is the first condition
102.7 -> and here is the second condition
105.32 -> If either of these conditions is TRUE,
the record will be included in the result set
110.76 -> Now let's run our query
and see what results we get
114.02 -> Our query returns all columns
from the suppliers table
117.82 -> where California is the state
and supplier_id is not equal to 900
123.02 -> Our query also returns all suppliers
where the supplier_id is equal to 100
129.58 -> As you can see, the parentheses determine
the order that the conditions are evaluated
134.5 -> allowing you to create complex
conditions in your queries
138.5 -> This covers a simple example of how to use the
AND condition with the OR condition in an SQL query
145.58 -> If you would like to see
more examples
148.42 -> or would like to download
the sample data we used for this tutorial
151.58 -> please visit our website at
TechOnTheNet.com
154.64 -> You can also try the examples
in our SQL editor on our website
158.64 -> Look for the "Try It" button
next to each example
162.1 -> If you found our tutorial helpful,
please leave a like on this video
166.3 -> and don't forget to subscribe
to our YouTube channel for more great SQL tutorials
Source: https://www.youtube.com/watch?v=l3Ky5btytSY