You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/SQL/SQL-Where-Clause.md
+69-63Lines changed: 69 additions & 63 deletions
Original file line number
Diff line number
Diff line change
@@ -3,78 +3,84 @@ id: sql-where-clause
3
3
title: Where Clause in SQL
4
4
sidebar_label: Where Clause
5
5
sidebar_position: 3
6
-
tags: [sql, database, clause]
7
-
description: In this tutorial, you will learn how to build query with condition to get desired output.
6
+
tags: [sql, database, clause]
7
+
description: In this tutorial, you will learn how to build queries with conditions to get the desired output.
8
8
---
9
9
10
-
11
10
The WHERE clause in SQL is used to filter records from a result set. It specifies the conditions that must be met for the rows to be included in the result. The WHERE clause is often used in SELECT, UPDATE, DELETE, and other SQL statements to narrow down the data returned or affected.
12
11
13
-
# Syntax
14
-
`SELECT column1, column2, ...
12
+
## Syntax
13
+
```sql
14
+
SELECT column1, column2, ...
15
15
FROM table_name
16
-
WHERE condition;`
17
-
18
-
# Operators Used in the Where Clause
19
-
1.`= : Equal`.
20
-
2.`> : Greater than`.
21
-
3.`< : Less than`.
22
-
4.`>= : Greater than or equal`.
23
-
5.`<= : Less than or equal`.
24
-
6.`<> : Not equal. Note: In some versions of SQL this operator may be written as !=`.
25
-
7.`BETWEEN : Between a certain range`.
26
-
8.`LIKE : Search for a pattern`.
27
-
9.`IN : To specify multiple possible values for a column`.
28
-
# Advantage of SQL WHERE Clause
29
-
30
-
**1. Filtering Rows:** The WHERE clause evaluates each row in the table to determine if it meets the specified condition(s). Only rows that satisfy the condition are included in the result set.
31
-
32
-
**2. Conditions:** Conditions in the WHERE clause can use comparison operators like =, <> (not equal), >, <, >=, <=.
33
-
Logical operators such as AND, OR, and NOT can be used to combine multiple conditions.
34
-
**3. Pattern Matching:** The LIKE operator can be used for pattern matching. For example, LIKE 'A%' matches any string that starts with the letter 'A'.
35
-
**4. Range Checks:** The BETWEEN operator checks if a value is within a range of values. For example, BETWEEN 10 AND 20.
36
-
**5. Null Values:** The IS NULL and IS NOT NULL operators are used to filter records with null values.
37
-
38
-
# Example of Where Clause in SQL
39
-
40
-
Syntax: SELECT * from <Table_Name>;
41
-
Example : SELECT * from Students where marks >50;
42
-
43
-

44
-
45
-
**Where Clause use in Update Statement**
46
-
`UPDATE employees SET salary = salary * 1.10
47
-
WHERE performance_rating = 'Excellent';`
48
-
49
-
**Where Clause use in Delete Statement**
50
-
`DELETE FROM employees
51
-
WHERE last_login < '2023-01-01';`
52
-
53
-
54
-
**Where Clause use in Like Statement**
55
-
`SELECT * FROM customers
56
-
WHERE name LIKE 'J%';`
57
-
58
-
59
-
**Where Clause use in Between Statement**
60
-
`SELECT * FROM orders
61
-
WHERE order_date BETWEEN '2023-01-01' AND '2023-12-31';`
62
-
63
-
64
-
**Where Clause use in IS NULL Statement**
65
-
66
-
`SELECT * FROM employees
67
-
WHERE manager_id IS NULL;`
68
-
69
-
# Conclusion
16
+
WHERE condition;
17
+
```
18
+
19
+
## Operators Used in the WHERE Clause
20
+
1.`=` : Equal
21
+
2.`>` : Greater than
22
+
3.`<` : Less than
23
+
4.`>=` : Greater than or equal
24
+
5.`<=` : Less than or equal
25
+
6.`<>` : Not equal (Note: In some versions of SQL, this operator may be written as `!=`)
26
+
7.`BETWEEN` : Between a certain range
27
+
8.`LIKE` : Search for a pattern
28
+
9.`IN` : To specify multiple possible values for a column
29
+
30
+
## Advantages of SQL WHERE Clause
31
+
32
+
1.**Filtering Rows:** The WHERE clause evaluates each row in the table to determine if it meets the specified condition(s). Only rows that satisfy the condition are included in the result set.
33
+
2.**Conditions:** Conditions in the WHERE clause can use comparison operators like `=`, `<>` (not equal), `>`, `<`, `>=`, `<=`. Logical operators such as `AND`, `OR`, and `NOT` can be used to combine multiple conditions.
34
+
3.**Pattern Matching:** The LIKE operator can be used for pattern matching. For example, `LIKE 'A%'` matches any string that starts with the letter 'A'.
35
+
4.**Range Checks:** The BETWEEN operator checks if a value is within a range of values. For example, `BETWEEN 10 AND 20`.
36
+
5.**Null Values:** The `IS NULL` and `IS NOT NULL` operators are used to filter records with null values.
37
+
38
+
## Examples of WHERE Clause in SQL
39
+
40
+
### Basic Select Query
41
+
```sql
42
+
SELECT*FROM Students WHERE marks >50;
43
+
```
44
+
45
+
### WHERE Clause in UPDATE Statement
46
+
```sql
47
+
UPDATE employees SET salary = salary *1.10
48
+
WHERE performance_rating ='Excellent';
49
+
```
50
+
51
+
### WHERE Clause in DELETE Statement
52
+
```sql
53
+
DELETEFROM employees
54
+
WHERE last_login <'2023-01-01';
55
+
```
56
+
57
+
### WHERE Clause with LIKE Statement
58
+
```sql
59
+
SELECT*FROM customers
60
+
WHERE name LIKE'J%';
61
+
```
62
+
63
+
### WHERE Clause with BETWEEN Statement
64
+
```sql
65
+
SELECT*FROM orders
66
+
WHERE order_date BETWEEN '2023-01-01'AND'2023-12-31';
67
+
```
68
+
69
+
### WHERE Clause with IS NULL Statement
70
+
```sql
71
+
SELECT*FROM employees
72
+
WHERE manager_id IS NULL;
73
+
```
74
+
75
+
## Conclusion
70
76
The WHERE clause in SQL is a powerful tool for filtering data in various SQL statements. It allows you to specify conditions that rows must meet to be included in the result set, thereby enabling precise data retrieval and manipulation. By using comparison operators, logical operators, pattern matching, range checks, and handling null values, you can create complex queries tailored to your specific data requirements. Mastering the WHERE clause is essential for efficient database management and analysis, providing the ability to focus on relevant data and perform targeted updates and deletions.
0 commit comments