Text of docs.data.world › documentation › sql › concepts › basic › AND_OR_NOT.html as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

AND, OR, and NOT | SQL Tutorial Documentation on data.world

Concepts Basic Learn SQL Basics The SELECT and FROM Clauses The SELECT * EXCLUDE and SELECT * RENAME Clauses LIMIT WHERE and Comparison Operators ORDER BY AND, OR, and NOT Arithmetic Operations IS NULL and IS NOT NULL LIKE and NOT LIKE BETWEEN OFFSET Intermediate Working With Joins, Aggregations, and Built-In Functions Joins CAST Aggregations GROUP BY and FILTER HAVING UNION, INTERSECT, and MINUS POSITION CASE IN and NOT IN Working With Strings Working With Dates Working With Arrays Advanced WITH Inline Subqueries Value blocks Special Uses of Inline Subqueries Advanced Aggregate Functions Windowed Aggregations ROLLUP and CUBE PIVOT UNPIVOT TABLESAMPLE Metadata Tables Lexical Issues data.world Specific Multi-Tables Federated Queries Parameterized Queries Special Columns Query past instances of data Text formatting for query outputs Reference Boolean Aggregations Regression Aggregations Standard Aggregations Statistical Aggregations Other Aggregations PRAGMA Statements All Functions Array Functions Date Functions Color Functions Formatting Functions General Functions Hashing and Related Functions Linked Data Functions Math Functions Security Functions String Functions URL Extraction Functions Aggregations APPROX_DISTINCT APPROX_PERCENTILE APPROX_MEDIAN ARRAY_AGG ARBITRARY AVG BOOL_OR BOOL_AND CHECKSUM CORRELATION COUNT COUNT_IF COVAR_POP COVAR_SAMP CUME_DIST DENSE_RANK FIRST_VALUE GROUP_CONCAT GROUPING KURTOSIS LAST_VALUE LAG LEAD MAX MAX_BY MIN NTH_VALUE NTILE MIN_BY PERCENT_RANK RANK REGR_AVGX REGR_AVGY REGR_COUNT REGR_R2 REGR_INTERCEPT REGR_SLOPE REGR_SXY REGR_SXX REGR_SYY ROW_NUMBER SKEWNESS STD_POP STDEV SUM VAR_POP VAR_SAMP VARIANCE STD_SAMP Functions ABS ARRAY ARRAY_CONCAT ARRAY_JOIN ARRAY_LENGTH ARRAY_CONTAINS ARRAY_APPEND ARRAY_LOWER ARRAY_PREPEND ASIN AT_TIME_ZONE ARRAY_UPPER ATAN ATAN2 ATTR_OF BASE64_TO_HEX BOLD CEILING CHAR COALESCE COLOR CONCAT COS COSH CURRENT_USER_IN_GROUP CURRENT_USER DATE_ADD DATE_DIFF DATE_PARSE DATE_COMPARE DATE_FORMAT DATE_PART DATE_SUB ELEMENT_AT DAY DEGREES DATE_TRUNC ENDSWITH EXP ACOS EXP10 FLOOR HEX_TO_BASE64 GREATEST HOURS GENERATE_SERIES IF IRI_TO_RESOURCE_HASH IRI_OF ITALIC LABEL_OF JARO_WINKLER_DISTANCE LEAST LEFT LENGTH LEVENSHTEIN_DISTANCE LOG LOWER LOG10 LPAD MD5 LTRIM MINUTES MOD MONTH MID NOW NOW_UTC PI POSITION POW RADIANS RAND RANDOM REGEX REGEXP_EXTRACT REPLACE REVERSE RIGHT RGB ROUND RTRIM SECONDS SHA1 RPAD SHA384 SHA512 SHA256 SIGN SIN SOUNDEX SINH SQRT SOUNDEX_DISTANCE STARTSWITH STR_AFTER STATUS STR_BEFORE STR_CONTAINS STRING_SPLIT SUBSTRING TAN TANH TRIM TRUNC UPPER URL_EXTRACT_FRAGMENT URL_EXTRACT_HOST URL_EXTRACT_PARAMETER URL_EXTRACT_PORT URL_EXTRACT_PATH URL_EXTRACT_QUERY WITH_BACKGROUND_COLOR URL_EXTRACT_PROTOCOL WITH_COLOR WITH_LINK YEAR WITH_STATUS While there are certainly queries that you’d like to run with only one condition restricting the data returned, there are likely many more that require combining conditions to return exactly the results you want.

AND , OR , and NOT are the logical operators SQL uses to combine conditions in WHERE clauses in order to refine the data returned by a query. AND The first of the logical operators is AND . AND is used in the WHERE clause of a query to return all the rows which meet all of the conditions specified that are joined with AND . If we wanted to see all of the cats who were returned to their owners, we could write a query like this: SELECT monthyear, name , age_upon_outcome FROM austin_animal_center_outcomes WHERE animal_type = "Cat" AND outcome_type = "Return to Owner" ORDER BY name DESC siyeh/austin-animal-center-statistics-oct-1-2013-to-dec-7-2017 Run query Copy code Notice that we did not choose to include the fields used with the AND operator in the WHERE clause.

As the query only returns rows for “Cat” and “Return to owner”, the data in those columns would be redundant because it would all be the same. Also note that because we included an ORDER BY clause, the data returned was sorted by the cats’ names in descending order. monthyear name age_upon_outcome 2014-12 Zulma 4 years 2017-08 Zorro 2 years 2016-07 Zoey 7 months 2016-11 Zoey 12 years 2014-11 Zoe 3 years 2016-03 Zoe 6 months 2017-09 “Zippy #Z16” 1 year 2016-09 Zion 6 years 2014-02 Zillah 15 years 2014-03 Zillah 15 years 2016-05 Ziggy 14 years 2017-06 Ziggy 5 years 2014-03 Zena 11 months 2016-01 Zelda 8 months 2015-04 Zeke 13 years OR Another way to restrict the data returned form a query is to use the logical operation OR .

OR returns all the rows in a dataset that meet the first condition specified before the OR as well as all data that meets the second condition specified after the OR . Let’s say we wanted to see all of the cats and dogs who were either strays or owner surrenders. We could write the query like this: SELECT animal_type, intake_type, Intake_condition, age_upon_intake FROM austin_animal_center_intakes WHERE (animal_type = "Cat" OR animal_type = "DOG" ) AND (intake_type = "Stray" OR intake_type = "Owner Surrender" ) siyeh/austin-animal-center-statistics-oct-1-2013-to-dec-7-2017 Run query Copy code The first several rows in the table returned would be: animal_type intake_type Intake_condition age_upon_intake Cat Stray Normal 16 years Cat Stray Normal 1 month Cat Owner Surrender Normal 10 years Cat Owner Surrender Normal 9 months Cat Stray Normal 10 months Cat Owner Surrender Sick 15 years Cat Stray Normal 7 years NOT The last logical operation used in the WHERE clause is NOT which is most commonly used with other keywords such as BETWEEN , LIKE or IN .

However it can also be used to negate an entire section in the WHERE clause. Let’s say we already had a query that returned all the rows for cats and dogs who were taken in who were sick. That query might look like this: SELECT monthyear, animal_type, intake_type, intake_condition FROM austin_animal_center_intakes WHERE ((animal_type = "Cat" ) OR (animal_type = "Dog" )) AND (intake_condition = "Sick" ) siyeh/austin-animal-center-statistics-oct-1-2013-to-dec-7-2017 Run query Copy code And the first several rows of the data returned would look like this: monthyear animal_type intake_type Intake_condition 2014-02 Cat Owner Surrender Sick 2014-02 Cat Owner Surrender Sick 2014-02 Cat Stray Sick 2014-02 Cat Stray Sick 2014-02 Cat Stray Sick 2014-03 Cat Stray Sick 2014-03 Cat Stray Sick 2014-03 Cat Owner Surrender Sick If for some reason you wanted to see all of the data for animals that were neither sick cats nor sick dogs, you could add NOT to the WHERE clause: SELECT monthyear, animal_type, intake_type, Intake_condition FROM austin_animal_center_intakes WHERE NOT (((animal_type = "Cat" ) OR (animal_type = "Dog" )) AND (intake_condition = "Sick" )) siyeh/austin-animal-center-statistics-oct-1-2013-to-dec-7-2017 Run query Copy code The table returned from this query would begin like: monthyear animal_type intake_type Intake_condition 2017-12 Dog Stray Normal 2014-12 Dog Public Assist Normal 2015-11 Cat Stray Normal 2015-03 Dog Public Assist Normal 2016-03 Dog Public Assist Normal 2014-02 Dog Owner Surrender Normal 2014-02 Dog Public Assist Normal The example above is cumbersome and there are other ways to write the query which would be easier for the eye to parse.

NOT is not a commonly used logical operator unless combined with LIKE , BETWEEN , etc. Exercises (Continued from previous section) There are two ways to do these exercises. The first is to use the “Try query” links to test your queries without saving them. The second is to create a data.world project and save your queries to it. If you are reading this documentation and completing the exercises as a tutorial, you will need to create your own project to save your work.

Details and instructions are in the SQL tutorial which has instructions for setting up your project and links to all the current exercises. Exercise 9 Using the patient, description, and reasondescription columns in the medications table, write a query that returns all the r

The rest of this page is on the live site.