SQL Operations using Arithmetic, Comparison, Logical Operators.
SQL Operators
+ Add
- Subtract
* Multiply
/ Divide
% Modulo
= Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
<> Not equal to
Arithmetic Operators
--Addition
SELECT (1000 +45000)
FROM DUAL;
--Subtraction
SELECT (1000 -45000)
FROM DUAL;
--Multiplication
SELECT (1000*45000)
FROM DUAL;
--Division
SELECT (1000 % 45000)
FROM DUAL;
--Comparison Operators Equal
SELECT *
FROM Student_details
WHERE S_name = 'indu';
--NOT Equal
SELECT *
FROM Student_details
WHERE S_name != 'indu';
--Greater than
SELECT *
FROM Student_details
WHERE S_FEES > 78900;
--Less than
SELECT *
FROM Student_details
WHERE S_FEES < 78900;
--Greater than equal TO
SELECT *
FROM Student_details
WHERE S_FEES >= 78900;
--Less than equal TO
SELECT *
FROM Student_details
WHERE S_FEES <= 78900;
--NOT equal TO
SELECT *
FROM Student_details
WHERE S_FEES <> 78900;
--Logical operators ALL
SELECT first_name,
last_name,
age,
location
FROM users
WHERE age > ALL (SELECT age FROM users WHERE location = 'London');
--ANY
SELECT product_name
FROM products
WHERE product_id > ANY (SELECT product_id FROM orders);
--AND
SELECT *
FROM users
WHERE age = 20
AND location = 'London';
--BETWEEN Checking for a Range of Values
SELECT *
FROM users
WHERE age BETWEEN 20 AND 30;
--EXISTS
SELECT name
FROM customers
WHERE EXISTS (SELECT ORDER FROM ORDERS WHERE customer_id = 1);
--IN A keyword used in a WHERE clause to specify a list of values to be matched using an OR comparison.
SELECT *
FROM users
WHERE first_name IN ('Bob','Fred','Harry');
--LIKE
SELECT *
FROM Student_details
WHERE S_name LIKE 'K%';
SELECT *
FROM Student_details
WHERE S_name LIKE '%U';
SELECT *
FROM Student_details
WHERE S_EMAILID LIKE '%456%';
--NOT IN
SELECT *
FROM users
WHERE first_name NOT IN ('Bob','Fred','Harry');
--OR
SELECT *
FROM users
WHERE age = 20
OR location = 'London';
--NULL No value, as opposed to a field containing 0, or an empty string, or just spaces.
SELECT *
FROM users
WHERE age IS NULL;
When to Use Quotes
If you look closely at the conditions used in the above WHERE clauses, you will notice that some values are enclosed within single
quotes, and others are not. The single quotes are used to delimit a string.
If you are comparing a value against a column that is a string data type, the delimiting quotes are required.
Quotes are not used to delimit values used with numeric columns.
Filtering the data using Where condition
Combining WHERE Clauses
A special keyword used to join or change clauses within a WHERE clause. Also known as logical operators.
SELECT prod_id, prod_price, prod_name FROM Products WHERE vend_id = 'DLL01' AND prod_price <= 4;
AND
A keyword used in a WHERE clause to specify that only rows matching all the specified conditions should be retrieved
OR
A keyword used in a WHERE clause to specify that any rows matching either of the specified conditions should be retrieved.
Understanding Order of Evaluation
SELECT prod_name, prod_price FROM Products WHERE (vend_id = 'DLL01' OR vend_id = 'BRS01') AND prod_price >= 10;
NOT
A keyword used in a WHERE clause to negate a condition.
Lesson 6. Using Wildcard Filtering
Wildcards
Special characters used to match parts of a value.
Search pattern
A search condition made up of literal text, wildcard characters, or any combination of the above.
The Percent Sign (%) Wildcard
SELECT prod_id, prod_name FROM Products WHERE prod_name LIKE 'Fish%';
Searching for Partial Email Addresses
There is one situation in which wildcards may indeed be useful in the middle of a search pattern, and that is looking for e-mail
addresses based on a partial address, such as WHERE email LIKE b%@forta.com.