Skip to content

Commit

Permalink
Add like
Browse files Browse the repository at this point in the history
  • Loading branch information
peymanslh committed Feb 5, 2024
1 parent 4bbcbe2 commit bd63d92
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 9 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# peyman.blog
# SQL by example

## Setup
Install extended version of hugo.
Run server: `hugo server`.
Create a new post: `hugo new --kind post posts/<post-name>.md`
Install extended version of hugo.
- Run development server: `hugo server`.
- Create a new post: `hugo new --kind post posts/<post-name>.md`

## TODO
- [x] SELECT
Expand All @@ -14,17 +14,17 @@ Create a new post: `hugo new --kind post posts/<post-name>.md`
- [x] ORDER BY
- [x] GROUP BY
- [x] AND, OR, NOT
- [ ] Operators
- [x] AS (Alias)
- [x] LIMIT
- [x] Comments
- [ ] SELECT DISTINCT
- [ ] IN
- [ ] LIKE
- [x] LIKE
- [x] MIN, MAX
- [x] COUNT
- [x] SUM
- [x] AVG
- [ ] SELECT DISTINCT
- [ ] IN
- [ ] Operators
- [ ] BETWEEN
- [ ] ROUND
- [ ] JOIN
Expand All @@ -43,6 +43,7 @@ Create a new post: `hugo new --kind post posts/<post-name>.md`
- [ ] NULL
- [ ] ALTER TABLE
- [ ] TRUNCATE
- [ ] SIMILAR TO https://www.postgresql.org/docs/16/functions-matching.html

- [ ] Constraints
- [ ] Unique
Expand Down
2 changes: 1 addition & 1 deletion content/posts/comment.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: "Comment"
date: 2024-02-03T15:11:15+03:30
tags:
- intro
- query
- postgresql
---
When you writing multiline SQL queries you can use `--` to comment a single line and
Expand Down
94 changes: 94 additions & 0 deletions content/posts/like.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
---
title: "LIKE"
date: 2024-02-05T11:17:10+03:30
tags:
- query
- postgresql
---
`LIKE` is use in `WHERE` condition to find data based on a pattern.
There are two wildcards that we can use with `LIKE`:
- `%`: represents zero, one, or multiple characters
- `_`: represents one, single character.
```sql
-- find all users with first_name of John
SELECT
id,
first_name,
last_name
FROM users
WHERE first_name LIKE 'John';
-- +-----+------------+-----------+
-- | id | first_name | last_name |
-- |-----+------------+-----------|
-- | 14 | John | Combs |
-- | 51 | John | Fritz |
-- | 285 | John | Sanders |
-- | 445 | John | Wise |
-- | 485 | John | Peterson |
-- | 501 | John | Moreno |
-- | 502 | John | Moreno |
-- +-----+------------+-----------+

-- find all users that their first_name starts with A
SELECT
id,
first_name,
last_name
FROM users
WHERE first_name LIKE 'A%';
-- +-----+------------+-----------+
-- | id | first_name | last_name |
-- |-----+------------+-----------|
-- | 13 | Andrew | Mercado |
-- | 42 | Adrian | Ewing |
-- | 74 | Audrey | Edwards |

-- find all users that their first_name ends with n
SELECT
id,
first_name,
last_name
FROM users
WHERE first_name LIKE '%n';
-- +-----+------------+-----------+
-- | id | first_name | last_name |
-- |-----+------------+-----------|
-- | 1 | Carmen | Malone |
-- | 3 | Brandon | Jenkins |
-- | 7 | Jocelyn | Williams |
-- | 10 | Steven | Perez |
-- ...

-- find all users that their first_name contains n
SELECT
id,
first_name,
last_name
FROM users
WHERE first_name LIKE '%n%';
-- +-----+------------+-----------+
-- | id | first_name | last_name |
-- |-----+------------+-----------|
-- | 1 | Carmen | Malone |
-- | 2 | Stephanie | Wallace |
-- | 3 | Brandon | Jenkins |
-- | 5 | Jennifer | Smith |
-- ...

-- find all users that their first_name is 4 character and ends with arl
SELECT
id,
first_name,
last_name
FROM users
WHERE first_name LIKE '_arl';
-- +-----+------------+------------+
-- | id | first_name | last_name |
-- |-----+------------+------------|
-- | 59 | Earl | Livingston |
-- | 210 | Carl | Gonzalez |
-- | 382 | Karl | Pierce |
-- | 412 | Earl | Owens |
-- +-----+------------+------------+
```
[PostgreSQL docs](https://www.postgresql.org/docs/16/functions-matching.html#FUNCTIONS-LIKE)

0 comments on commit bd63d92

Please sign in to comment.