Condition (SQL): Difference between revisions

Content deleted Content added
Dna-webmaster (talk | contribs)
categorizing this article
m Removing link(s) Wikipedia:Articles for deletion/Where (SQL) closed as delete (XFDcloser)
 
(28 intermediate revisions by 23 users not shown)
Line 1:
{{Refimprove|date=December 2009}}
A [[relational database management system]] uses [[SQL]] '''conditions''' in '''<tt>WHERE</tt>''' clauses to '''<tt>[[select (SQL)|SELECT]]</tt>''' subsets of data.
 
A [[relational database management system]] uses [[SQL]] '''conditions''' or [[Expression (programming)|expressions]] in '''<tt>{{mono|WHERE</tt>}}''' clauses and in '''[[Having (SQL)|{{mono|HAVING}}]]''' clauses to '''<tt>{{mono|[[select (SQL)|SELECT]]</tt>}}''' subsets of data.
== Examples ==
 
== Types of condition ==
To '''<tt>SELECT</tt>''' one row of data from a table ''tab'' with primary key column ''pk'' set to 100 &mdash; use the condition ''pk = 100'':
{{Expand section|date=July 2010}}
 
* Many conditions compare values for (for example) equality, inequality or similarity.
SELECT * FROM tab WHERE pk = 100
* The EXISTS condition uses the [[SQL:2003|SQL standard]] keyword <code>EXISTS</code><ref>
{{cite book
|last= Fehily
|first= Chris
|title= SQL: Visual Quickstart Guide
|edition= 2
|year= 2005
|publisher= Peachpit Press
|isbn= 978-0-321-33417-6
|pages= [https://archive.org/details/sql0000fehi/page/439 439–440, 480]
|quote= SQL Keywords [...] The appendix lists the [[SQL:2003]] standard's reserved and non-reserved keywords. [...] EXISTS [...]
|url= https://archive.org/details/sql0000fehi/page/439
}}
</ref> to determine whether rows exist in a [[subquery]] result.<ref>
{{cite book
|last= Fehily
|first= Chris
|title= SQL: Visual Quickstart Guide
|edition= 2
|year= 2005
|publisher= Peachpit Press
|isbn= 978-0-321-33417-6
|page= [https://archive.org/details/sql0000fehi/page/278 278]
|quote= EXISTS and NOT EXISTS [...] look for the existence or nonexistence of rows in a subquery result.
|url= https://archive.org/details/sql0000fehi/page/278
}}
</ref>
 
== Examples ==
To '''<tt>{{mono|SELECT</tt>}}''' one row of data from a table called ''tab'' with a primary key column (''pk'') set to 100 &mdash; use the condition ''pk = 100'':
<syntaxhighlight lang="sql">SELECT * FROM tab WHERE pk = 100</syntaxhighlight>
 
To identify whether a table ''tab'' has rows of data with a duplicated column ''dk'' &mdash; use the condition ''having count(*) > 1'':
<syntaxhighlight lang="sql">SELECT dk FROM tab GROUP BY dk HAVING count(*) > 1</syntaxhighlight>
 
== Advanced conditional logic in SQL ==
In addition to basic equality and inequality conditions, SQL allows for more complex conditional logic through constructs such as <code>CASE</code>, <code>COALESCE</code>, and <code>NULLIF</code>. The <code>CASE</code> expression, for example, enables SQL to perform conditional branching within queries, providing a mechanism to return different values based on evaluated conditions. This logic can be particularly useful for data transformation during retrieval, especially in SELECT statements. Meanwhile, <code>COALESCE</code> simplifies the process of handling NULL values by returning the first non-NULL value in a given list of expressions, which is especially useful in scenarios where data might be incomplete or missing. Furthermore, SQL's support for three-valued logic (True, False, Unknown) introduces nuances when handling NULL values in conditions, making it essential to carefully structure queries to account for the "Unknown" state that arises in certain comparisons with NULL values. Proper use of these advanced conditions enhances the flexibility and robustness of SQL queries, particularly in complex data retrieval and reporting environments.
 
{{SQL}}
 
== References ==
{{Reflist}}
 
{{DEFAULTSORT:Condition (Sql)}}
[[Category:SQL]]
[[Category:Articles with example SQL code]]