For these operators and keywords, we will take this given table as an example.
id | title | post |
1 | What is a computer? | Electronic machine |
2 | An input device | Keyboard |
3 | WWW stands for | World wide web |
LIMIT
LIMIT specifies how many records will fetch in a query.
Display only one record after record that has id 2.
SELECT * FROM posts LIMIT 2, 1;
WHERE the first number defines the skip and the second number defines how many records will display.
id | title | post |
3 | WWW stands for | World wide web |
LIKE
Like operator is used to search any pattern in records.
There are wildcards used with LIKE keywords.
- % - Represents zero, one, and multiple matched characters
- _ - underscore represents one character.
Find all those posts whose title start with "w"
SELECT * FROM posts WHERE title LIKE 'w%'
id | title | post |
1 | What is a computer? | Electronic machine |
3 | WWW stands for | World wide web |
Find all those posts whose title end with "r"
SELECT * FROM posts WHERE title LIKE '%r';
id | title | post |
3 | WWW stands for | World wide web |
Find all those posts whose titles contain "i"
SELECT * FROM posts WHERE title LIKE '%i%';
id | title | post |
1 | What is a computer? | Electronic machine |
2 | An input device | Keyboard |
Find all those posts whose title start with 'a' and contain 2 characters
SELECT * FROM posts WHERE title LIKE 'a_%';
id | title | post |
2 | An input device | Keyboard |
For IN and BETWEEN operators, the given table will be taken as an example.
id | product_name | price | qty |
1 | UPS | 3000 | 2 |
2 | PEN | 100 | 2 |
3 | MOUSE | 400 | 1 |
4 | SPEAKER | 5000 | 2 |
In Operator
It is a shorthand for the OR operator, it allows to use of multiple values in the WHERE clause.
Fetch only those records which have id 1,2,4.
SELECT * FROM products WHERE id IN(1, 2, 4);
id | product_name | price | qty |
1 | UPS | 3000 | 2 |
2 | PEN | 100 | 2 |
4 | SPEAKER | 5000 | 2 |
BETWEEN Operator
When we want to fetch all those records whose price between in a given range.
Fetch all those records whose prices are between 1000 to 5000.
SELECT * FROM products WHERE price BETWEEN 1000 AND 5000;
id | product_name | price | qty |
1 | UPS | 3000 | 2 |
4 | SPEAKER | 5000 | 2 |