WHERE clause

Description

The WHERE clause is used to limit the results of the FROM clause of a query or a subquery based on the specified condition.

Syntax

  1. WHERE boolean_expression

Parameters

  • boolean_expression

    Specifies any expression that evaluates to a result type boolean. Two or more expressions may be combined together using the logical operators ( AND, OR ).

Examples

  1. CREATE TABLE person (id INT, name STRING, age INT);
  2. INSERT INTO person VALUES
  3. (100, 'John', 30),
  4. (200, 'Mary', NULL),
  5. (300, 'Mike', 80),
  6. (400, 'Dan', 50);
  7. -- Comparison operator in `WHERE` clause.
  8. SELECT * FROM person WHERE id > 200 ORDER BY id;
  9. +---+----+---+
  10. | id|name|age|
  11. +---+----+---+
  12. |300|Mike| 80|
  13. |400| Dan| 50|
  14. +---+----+---+
  15. -- Comparison and logical operators in `WHERE` clause.
  16. SELECT * FROM person WHERE id = 200 OR id = 300 ORDER BY id;
  17. +---+----+----+
  18. | id|name| age|
  19. +---+----+----+
  20. |200|Mary|null|
  21. |300|Mike| 80|
  22. +---+----+----+
  23. -- IS NULL expression in `WHERE` clause.
  24. SELECT * FROM person WHERE id > 300 OR age IS NULL ORDER BY id;
  25. +---+----+----+
  26. | id|name| age|
  27. +---+----+----+
  28. |200|Mary|null|
  29. |400| Dan| 50|
  30. +---+----+----+
  31. -- Function expression in `WHERE` clause.
  32. SELECT * FROM person WHERE length(name) > 3 ORDER BY id;
  33. +---+----+----+
  34. | id|name| age|
  35. +---+----+----+
  36. |100|John| 30|
  37. |200|Mary|null|
  38. |300|Mike| 80|
  39. +---+----+----+
  40. -- `BETWEEN` expression in `WHERE` clause.
  41. SELECT * FROM person WHERE id BETWEEN 200 AND 300 ORDER BY id;
  42. +---+----+----+
  43. | id|name| age|
  44. +---+----+----+
  45. |200|Mary|null|
  46. |300|Mike| 80|
  47. +---+----+----+
  48. -- Scalar Subquery in `WHERE` clause.
  49. SELECT * FROM person WHERE age > (SELECT avg(age) FROM person);
  50. +---+----+---+
  51. | id|name|age|
  52. +---+----+---+
  53. |300|Mike| 80|
  54. +---+----+---+
  55. -- Correlated Subquery in `WHERE` clause.
  56. SELECT * FROM person AS parent
  57. WHERE EXISTS (
  58. SELECT 1 FROM person AS child
  59. WHERE parent.id = child.id AND child.age IS NULL
  60. );
  61. +---+----+----+
  62. |id |name|age |
  63. +---+----+----+
  64. |200|Mary|null|
  65. +---+----+----+