SQL WHERE: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(3 intermediate revisions by the same user not shown)
Line 29: Line 29:
* Arithmetic operators: <code>+</code>, <code>-</code>, <code>/</code>, <code>*</code>
* Arithmetic operators: <code>+</code>, <code>-</code>, <code>/</code>, <code>*</code>
==Equality Conditions==
==Equality Conditions==
<font color=darkkhaki>TO PROCESS: https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604/ch04.html#:-:text=Equality%20Conditions</font>
==Inequality Conditions==
==Inequality Conditions==
<font color=darkkhaki>TO PROCESS: https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604/ch04.html#:-:text=Inequality%20conditions,an%20inequality%20condition%3A</font>
==Range Conditions==
==Range Conditions==
<font color=darkkhaki>TO PROCESS: https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604/ch04.html#:-:text=Range%20Conditions</font>
==The <tt>BETWEEN</TT> Operator==
==The <tt>BETWEEN</TT> Operator==
<font color=darkkhaki>TO PROCESS: https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604/ch04.html#:-:text=The%20between%20operator</font>
==String Ranges==
==String Ranges==
<font color=darkkhaki>TO PROCESS: https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604/ch04.html#:-:text=String%20ranges</font>
==Membership Conditions==
==Membership Conditions==
<font color=darkkhaki>TO PROCESS: https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604/ch04.html#:-:text=Membership%20Conditions</font>
==Using Subqueries==
==Using Subqueries==
<font color=darkkhaki>TO PROCESS: https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604/ch04.html#:-:text=Using%20subqueries</font>
==Matching Conditions==
==Matching Conditions==
<font color=darkkhaki>TO PROCESS: https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604/ch04.html#:-:text=Matching%20Conditions</font>
==Using Regular Expressions==
==Using Regular Expressions==
<font color=darkkhaki>TO PROCESS: https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604/ch04.html#:-:text=Using%20regular%20expressions</font>
==<tt>NULL</tt> in Conditions==
==<tt>NULL</tt> in Conditions==
<font color=darkkhaki>TO PROCESS: https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604/ch04.html#:-:text=Null%3A%20That%20Four-Letter%20Word</font>
<font color=darkkhaki>TO PROCESS: https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604/ch04.html#:-:text=Null%3A%20That%20Four-Letter%20Word</font>
Line 43: Line 55:
=Using Parentheses=
=Using Parentheses=
If the <code>WHERE</code> clause includes three or more conditions combined with <code>AND</code>, <code>OR</code> or <code>NOT</code>, you should use parentheses to make your intent clear.
If the <code>WHERE</code> clause includes three or more conditions combined with <code>AND</code>, <code>OR</code> or <code>NOT</code>, you should use parentheses to make your intent clear.
<font color=darkkhaki>TO PROCESS: https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604/ch04.html#:-:text=Using%20Parentheses</font>
=SQL Conditional Logic=
=SQL Conditional Logic=
<font color=darkkhaki>TO PROCESS: https://www.geeksforgeeks.org/sql-conditional-expressions/</font>


==<tt>CASE</tt>==
==<tt>CASE</tt>==

Revision as of 00:13, 25 May 2024

Internal

TODO

TO PROCESS: https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604/ch04.html

Overview

The WHERE clause is the mechanism for filtering out unwanted data from the result set. The WHERE clause can be used with SELECT, UPDATE and DELETE, but not with INSERT. The WHERE clause may contain an arbitrary number of filter conditions separated by AND, OR and NOT operators, and optionally grouped together with parentheses.

  FROM <filter_condition> AND|OR <filter_condition> ...

SELECT * FROM person WHERE person.name = 'Alice' AND (person.eye_color = 'blue' OR person.eye_color = 'black');

Filter Conditions

A condition is made up of one or more expressions, combined with one or more operators. An expression can be any of the following:

  • A number
  • A column in a table or a view
  • A string literal
  • A built-in function such as CONCAT()
  • A subquery
  • A list of expressions such as ('A', 'B', 'C')

The operators used within conditions include:

  • Comparison operators: =, !=, <, >, <>, LIKE, IN, BETWEEN.
  • Arithmetic operators: +, -, /, *

Equality Conditions

TO PROCESS: https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604/ch04.html#:-:text=Equality%20Conditions

Inequality Conditions

TO PROCESS: https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604/ch04.html#:-:text=Inequality%20conditions,an%20inequality%20condition%3A

Range Conditions

TO PROCESS: https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604/ch04.html#:-:text=Range%20Conditions

The BETWEEN Operator

TO PROCESS: https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604/ch04.html#:-:text=The%20between%20operator

String Ranges

TO PROCESS: https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604/ch04.html#:-:text=String%20ranges

Membership Conditions

TO PROCESS: https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604/ch04.html#:-:text=Membership%20Conditions

Using Subqueries

TO PROCESS: https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604/ch04.html#:-:text=Using%20subqueries

Matching Conditions

TO PROCESS: https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604/ch04.html#:-:text=Matching%20Conditions

Using Regular Expressions

TO PROCESS: https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604/ch04.html#:-:text=Using%20regular%20expressions

NULL in Conditions

TO PROCESS: https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604/ch04.html#:-:text=Null%3A%20That%20Four-Letter%20Word

Also see:

NULL

Using Parentheses

If the WHERE clause includes three or more conditions combined with AND, OR or NOT, you should use parentheses to make your intent clear.

TO PROCESS: https://learning.oreilly.com/library/view/learning-sql-3rd/9781492057604/ch04.html#:-:text=Using%20Parentheses

SQL Conditional Logic

TO PROCESS: https://www.geeksforgeeks.org/sql-conditional-expressions/

CASE

CASE provides an IF/ELSE conditional logic that can be used in WHERE clauses.

CASE <expression>
    WHEN <result_1> THEN <expression>
    WHEN <result_1> THEN <expression>
    ...
    ELSE <expression>
END

A simple IF/ELSE:

CASE <expression_that_evaluates_to_true_or_false> WHEN TRUE THEN <if_expression>
ELSE <else_expression>
END


Usage example:

CASE array_length(person.skills, 1) > 0 WHEN TRUE THEN ('skill_we_search_for' = ANY(person.skills)) 
ELSE 'key_we_search_for' = ANY(general.skills)
END