Condition (SQL)


title: "Condition (SQL)" type: doc version: 1 created: 2026-02-28 author: "Wikipedia contributors" status: active scope: public tags: ["sql", "articles-with-example-sql-code"] topic_path: "technology/databases" source: "https://en.wikipedia.org/wiki/Condition_(SQL)" license: "CC BY-SA 4.0" wikipedia_page_id: 0 wikipedia_revision_id: 0

A relational database management system uses SQL conditions or expressions in **** clauses and in clauses to **** subsets of data.

Types of condition

  • Many conditions compare values for (for example) equality, inequality or similarity.
  • The EXISTS condition uses the SQL standard keyword EXISTS |last= Fehily |first= Chris |title= SQL: Visual Quickstart Guide |edition= 2 |year= 2005 |publisher= Peachpit Press |isbn= 978-0-321-33417-6 |pages= 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 to determine whether rows exist in a subquery result. |last= Fehily |first= Chris |title= SQL: Visual Quickstart Guide |edition= 2 |year= 2005 |publisher= Peachpit Press |isbn= 978-0-321-33417-6 |page= 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

Examples

To **** one row of data from a table called tab with a primary key column (pk) set to 100 — use the condition pk = 100: ::code[lang=sql] SELECT * FROM tab WHERE pk = 100

To identify whether a table ''tab'' has rows of data with a duplicated column ''dk'' — use the condition ''having count() > 1'': SELECT dk FROM tab GROUP BY dk HAVING count() > 1 ::

Advanced conditional logic in SQL

In addition to basic equality and inequality conditions, SQL allows for more complex conditional logic through constructs such as CASE, COALESCE, and NULLIF. The CASE 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, COALESCE 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.

References

::callout[type=info title="Wikipedia Source"] This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page. ::

sqlarticles-with-example-sql-code