rewriting
All checks were successful
learn org at code.softwareshinobi.com/databases.softwareshinobi.com/pipeline/head This commit looks good
All checks were successful
learn org at code.softwareshinobi.com/databases.softwareshinobi.com/pipeline/head This commit looks good
This commit is contained in:
179
landing/docs/SQL-101/006-where.md
Normal file
179
landing/docs/SQL-101/006-where.md
Normal file
@@ -0,0 +1,179 @@
|
||||
# WHERE Clause
|
||||
|
||||
The `WHERE` clause filters query results. It restricts the rows returned based on specified conditions. Position this clause directly after `FROM`.
|
||||
|
||||
```sql
|
||||
SELECT column_name FROM table_name WHERE condition;
|
||||
```
|
||||
|
||||
The `condition` is an expression evaluating to true, false, or unknown. Only rows where the condition evaluates to true are included in the result set.
|
||||
|
||||
## WHERE Clause Example
|
||||
|
||||
Consider a `users` table. To retrieve only active users:
|
||||
|
||||
```sql
|
||||
SELECT DISTINCT username, email, active FROM users WHERE active=true;
|
||||
```
|
||||
|
||||
Output demonstrates results filtered by the `active= true` condition.
|
||||
|
||||
| username | email | active |
|
||||
|----------|-------------------|--------|
|
||||
| shinobi | b@javateamsix.com | 1 |
|
||||
| tony | t@javateamsix.com | 1 |
|
||||
|
||||
Conversely, retrieve inactive users by setting `active=false`:
|
||||
|
||||
```sql
|
||||
SELECT DISTINCT username, email, active FROM users WHERE active=false;
|
||||
```
|
||||
|
||||
Output reflects rows where `active ` is false (0).
|
||||
|
||||
| username | email | active |
|
||||
|-------------|-------------------|--------|
|
||||
| Java Team Six | d@javateamsix.com | 0 |
|
||||
|
||||
To select users with a specific username, like 'shinobi':
|
||||
|
||||
```sql
|
||||
SELECT username, email, active FROM users WHERE username='shinobi';
|
||||
```
|
||||
|
||||
Multiple entries match the username 'shinobi'.
|
||||
|
||||
| username | email | active |
|
||||
|----------|-------------------|--------|
|
||||
| shinobi | b@javateamsix.com | 1 |
|
||||
| shinobi | b@javateamsix.com | 1 |
|
||||
|
||||
## Operators
|
||||
|
||||
Comparison operators define conditions within the `WHERE` clause.
|
||||
|
||||
* `= ` : Equal to
|
||||
* `!=` : Not equal to
|
||||
* `>` : Greater than
|
||||
* `>=` : Greater than or equal to
|
||||
* `<` : Less than
|
||||
* `<=` : Less than or equal to
|
||||
|
||||
## AND Keyword
|
||||
|
||||
Combine multiple conditions with the `AND` keyword. The `AND` operator returns rows where *all* conditions are true.
|
||||
|
||||
Example: Select active users with username 'shinobi'.
|
||||
|
||||
```sql
|
||||
SELECT * FROM users WHERE username='shinobi' AND active=true;
|
||||
```
|
||||
|
||||
Output shows rows matching both the username and active status.
|
||||
|
||||
| id | username | about | birthday | active | email |
|
||||
|----|---------- |-------|----------|--------|-------------------|
|
||||
| 2 | shinobi | NULL | NULL | 1 | b@javateamsix.com |
|
||||
| 5 | shinobi | NULL | NULL | 1 | b@javateamsix.com |
|
||||
|
||||
If no rows satisfy *all* conditions, the result is empty.
|
||||
|
||||
```sql
|
||||
SELECT * FROM users WHERE username='shinobi' AND active=false;
|
||||
```
|
||||
|
||||
-- Output:
|
||||
Empty set.
|
||||
|
||||
## OR Keyword
|
||||
|
||||
Combine conditions with the `OR` keyword. The `OR` operator returns rows where *any* condition is true. Use `OR` to fetch data matching one criterion or another.
|
||||
|
||||
Example: Select users with username 'shinobi' OR where active is false.
|
||||
|
||||
```sql
|
||||
SELECT * FROM users WHERE username='shinobi' OR active=false;
|
||||
```
|
||||
|
||||
Output includes rows where either condition is met.
|
||||
|
||||
| id | username | about | birthday | active | email |
|
||||
|----|-------------|-------|----------|--------|-------------------|
|
||||
| 2 | shinobi | NULL | NULL | 1 | b@javate amsix.com |
|
||||
| 3 | Java Team Six | NULL | NULL | 0 | d@javateamsix.com |
|
||||
| 5 | shinobi | NULL | NULL | 1 | b@javateamsix.com |
|
||||
| 6 | Java Team Six | NULL | NULL | 0 | d@javateamsix.com |
|
||||
|
||||
## LIKE Operator
|
||||
|
||||
The `LIKE` operator performs pattern matching. It is typically used with wildcard characters (`%` for any sequence of characters, `_` for any single character).
|
||||
|
||||
Example: Select users where the username contains the letter 'y'.
|
||||
|
||||
```sql
|
||||
SELECT * FROM users WHERE username LIKE '%y%';
|
||||
```
|
||||
|
||||
Output includes usernames containing 'y'.
|
||||
|
||||
| id | username | about | birthday | active | email |
|
||||
|----|----------|-------|----------|--------|-------------------|
|
||||
| 2 | shinobi | NULL | NULL | 1 | b@javateamsix.com |
|
||||
| 4 | tony | NULL | NULL | 1 | t@javateamsix.com |
|
||||
|
||||
## IN Operator
|
||||
|
||||
The `IN` operator checks if a column's value matches any value in a specified list. This simplifies conditions that would otherwise require multiple `OR` statements.
|
||||
|
||||
Example: Select users with username 'shinobi' or ' Java Team Six'.
|
||||
|
||||
```sql
|
||||
SELECT * FROM users WHERE username IN ('shinobi', 'Java Team Six');
|
||||
```
|
||||
|
||||
Output shows all rows where the username is in the provided list.
|
||||
|
||||
| id | username | about | birthday | active | email |
|
||||
|----|-------------|-------|----------|--------|-------------------|
|
||||
| 2 | shinobi | NULL | NULL | 1 | b@javateamsix.com |
|
||||
| 3 | Java Team Six | NULL | NULL | 0 | d@javateamsix.com |
|
||||
| 5 | shinobi | NULL | NULL | 1 | b@javateamsix .com |
|
||||
| 6 | Java Team Six | NULL | NULL | 0 | d@javateamsix.com |
|
||||
|
||||
## IS Operator
|
||||
|
||||
Use the `IS` operator specifically for checking `NULL` values . The `=` operator cannot be used for this purpose.
|
||||
|
||||
To find rows where `about` is `NULL`:
|
||||
|
||||
```sql
|
||||
SELECT * FROM users WHERE about IS NULL;
|
||||
```
|
||||
|
||||
To find rows where `about` is not `NULL`:
|
||||
|
||||
```sql
|
||||
SELECT * FROM users WHERE about IS NOT NULL;
|
||||
```
|
||||
|
||||
## BETWEEN Operator
|
||||
|
||||
The `BETWEEN` operator selects values within a defined range. This range is inclusive of the start and end values. Applicable to numbers, text, and dates.
|
||||
|
||||
Example: Select users with an ID between 3 and 6 (inclusive).
|
||||
|
||||
```sql
|
||||
SELECT * FROM users WHERE id BETWEEN 3 AND 6;
|
||||
```
|
||||
|
||||
Output displays rows where `id` is within the specified range.
|
||||
|
||||
| id | username | about | birthday | active | email |
|
||||
|----|-------------|-------|----------|--------|-------------------|
|
||||
| 3 | Java Team Six | NULL | NULL | 0 | d@javateamsix.com |
|
||||
| 5 | shinobi | NULL | NULL | 1 | b@javateamsix.com |
|
||||
| 6 | Java Team Six | NULL | NULL | 0 | d@javateamsix.com |
|
||||
|
||||
## Summary
|
||||
|
||||
The `WHERE` clause and its associated operators are fundamental tools for precisely selecting data subsets, essential for efficient database interaction from Java applications.
|
||||
Reference in New Issue
Block a user