OR: the entire condition is true, if either one of the conditions is true
AND: the entire condition is true, only if both conditions are true
NOT: opposite of a condition - true becomes false; false becomes true
List products where the vendor code is ‘21344’ or ‘24288’
SELECT P_Description, P_Indate, P_Price, V_Code FROM PRODUCT WHERE V_Code = 21344 OR V_Code = 24288;
List products where either ‘the product indate is after July 15, 2015 and the product price is less than 50.00’ or the vendor code is 24288
SELECT P_Description, P_Indate, P_Price, V_Code FROM PRODUCT WHERE (P_Price < 50 AND P_Indate > TO_DATE('1999-09-15', 'YYYY-MM-DD')) OR V_Code = 24288;
IS NULL: check whether an attribute value is null.
IS NOT NULL: check whether an attribute value is NOT null.
BETWEEN: define range limits.
LIKE: check for similar character strings.
IN: check whether an attribute value matches a value contains within a subset of listed values.
List the products with prices between 50 and 100
SELECT * FROM PRODUCT WHERE P_Price BETWEEN 50.00 AND 100.00;
SELECT * FROM PRODUCT WHERE P_Price >= 50.00 AND P_Price <= 100.00;
List the details of all vendors whose last name begins with “Smith”:
SELECT V_Name, V_Contact, V_AreaCode, V_Phone FROM VENDOR WHERE V_Contact LIKE 'Smith%';--% is wild card
List the contents of the product table where the product price is exactly $50 or $100:
SELECT * FROM PRODUCT WHERE P_Price IN (50.00, 100.00);
List the details of products with existing (not- NULL) vendor codes:
SELECT * FROM PRODUCT WHERE V_Code IS NOT NULL;
sort by a column or a combination of columns
List the details of product table listed by product price in ascending order (default)
SELECT * FROM PRODUCT ORDER BY P_Price;
List the details of products with an in-date before 15 September 1999 and a price less than A$50. Put the results in ascending (default) order of vendor code and descending order of price.
SELECT P_Description, P_Indate, P_Price, V_Code FROM PRODUCT WHERE P_Indate < TO_DATE('1999-09-15', 'YYYY-MM-DD') AND P_Price < 50 ORDER BY V_Code, P_Price DESC;
Get the current system date (today's date)
SELECT SYSDATE AS current_date FROM DUAL;
Convert a string into a DATE using TO_DATE, for comparison
SELECT employee_id, first_name, last_name, hire_date FROM employees where hire_date > TO_DATE('15-04-2008', 'DD-MM-YYYY');
Calculate the number of months between today's date and hire_date
SELECT employee_id, hire_date, round(MONTHS_BETWEEN(SYSDATE, hire_date)) AS months_worked FROM employees;
SYSDATE → Today’s current date, as per your computer system
TO_DATE → convert a character string to DATE type by a certain format
MONTHS_BETWEEN → Months difference between two dates
Find employees hired in the last 12 months
SELECT employee_id, hire_date FROM employees WHERE hire_date >= ADD_MONTHS(SYSDATE, -12);
Find the next Monday after the hire_date for HR meeting
SELECT employee_id, hire_date, NEXT_DAY(hire_date, 'Monday') AS next_monday_after_hire FROM employees;
ADD_MONTHS → Add a set number of months to a date field
NEXT_DAY → The next given weekday after the given date
We can use the system table ‘DUAL’ (automatically created in Oracle) to do miscellaneous calculations.
Find the round of 0.4556
SELECT ROUND(0.4556, 2) FROM DUAL;
Truncate 0.4556 to 2 decimal places, not rounding
SELECT TRUNC (0.4556, 2) FROM DUAL;
Find the remainder of 4 divided by 3
SELECT MOD(4, 3) FROM DUAL;
Calculate 2^3
SELECT POWER(2, 3) FROM DUAL;
ROUND → Round numeric fields to the stated precision
TRUNC → Delete numeric points to the stated precision
MOD → Remainder of a division operator
ABS → Absolute value of a field
POWER → Statistical power
You can think about actual use case of these functions in the real-world datasets
If sales quantity is above 2, it is popular
If exactly 2, it is good;
otherwise, fair
SELECT ISBN, QUANTITY, CASE WHEN QUANTITY > 2 THEN 'popular' WHEN QUANTITY = 2 THEN 'good' ELSE 'fair' END as "Comment based on sales quantity" FROM ZJLB_ORDERITEMS;
CASE → different operations depending on conditions
Convert PHONE_NUMBER (with all '.' removed) to a numeric format
SELECT EMPLOYEE_ID, TO_NUMBER(replace(PHONE_NUMBER,'.','')) AS PHONE_AS_NUMBER FROM employees WHERE PHONE_NUMBER IS NOT NULL;
Format HIRE_DATE as 'YYYY-mm-dd'
SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME, HIRE_DATE, TO_CHAR(HIRE_DATE, 'YYYY-mm-dd') AS HIRE_DATE_FORMATTED FROM employees;
Format SALARY with commas and currency symbol
SELECT EMPLOYEE_ID, FIRST_NAME, SALARY, TO_CHAR(SALARY, '$999,999.00') AS SALARY_FORMATTED FROM employees;
TO_NUMBER → Convert a string to a number field (useful for arithmetic calculations)
TO_CHAR → Use to format values (e.g., convert dates and numbers)
LOWER → returns a string with all characters converted to lowercase.
UPPER → returns a string with all characters converted to uppercase.
INITCAP → converts the first letter of every word to uppercase, and every other letter to lowercase.
Run query on oracle database with given mock data
SELECT employee_id, upper(last_name), initcap(first_name),lower(email) FROM employees;
find all phone numbers with 44 as regional code
SELECT * FROM employees WHERE substr(phone_number, 5, 2) = '44';
find all phone numbers with 44 starting from position 5 (count starts from 1)
SELECT * FROM employees WHERE INSTR(phone_number, '44') = 5;
find all employees with very long last names
SELECT * FROM employees WHERE length(last_name) > 9;
SUBSTR → Extract a portion of the string
INSTR → Return the numeric position of a substring within the string
LENGTH → no. of characters in the character string
```latex
SELECT department_id from DEPARTMENTS;
```
take note that some are 2 char
some have 3 chars
Make all department codes 5 characters padded with 0, for invoices to look neat
SELECT LPAD(department_id, 5, '0') from DEPARTMENTS;
LPAD → Left-pad a string with another string, to a certain length
RPAD → Right-pad a string with another string, to a certain length
in case employees type in email with spaces at both ends
select trim(email) from employees;
trim spaces only on the left side of email
select ltrim(email) from employees;
remove all '0' and '.' from left side
This stops at the first char that is not '0' or '.'
select ltrim(phone_number,'.0') from employees;
TRIM → Removes leading & trailing spaces from both ends
LTRIM
RTRIM → Same as LTRIM, but from the right-hand side
replace ‘IT’ in department names with Tech: rebranding
SELECT department_name, replace(department_name, 'IT', 'Tech’) from DEPARTMENTS;
Find employees whose last name sounds like "smyth"
SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME FROM employees WHERE SOUNDEX(LAST_NAME) = SOUNDEX('smyth’);
REPLACE → Substitute a string with another string
SOUNDEX → Use to find values based on their “sound”
Replace NULL COMMISSION_PCT values with default 5%
SELECT EMPLOYEE_ID, FIRST_NAME, NVL(COMMISSION_PCT, 0.05) AS DEFAULT_COMMISSION FROM employees;
Display different messages based on whether COMMISSION_PCT is NULL
SELECT EMPLOYEE_ID, FIRST_NAME, NVL2(COMMISSION_PCT, 'Has Commission', 'No Commission’) AS COMMISSION_STATUS FROM employees;
NVL → Substitute a value for any NULLS; valid values stay as they are
NVL2 → Substitute a value for any NULLS, and a value for any fields that are not null