- Data Retrieval
- Data Manipulation
- Data Definition
- Constraints
- Transactions
- User Management
- Sequences
- Views
- String Functions
- Mathematical Functions
- Date Functions
- Miscellaneous
Description: Used to retrieve data from one or more tables. Example:
SELECT empno, ename, job, sal
FROM emp
WHERE sal > 2999;
Description: Creates a virtual table based on a SELECT query. Example:
CREATE OR REPLACE VIEW employee AS
SELECT empno, ename, job, deptno FROM emp;
Description: Groups rows sharing a property and applies aggregate functions. Example:
SELECT deptno, MAX(sal), MIN(sal), AVG(sal), SUM(sal)
FROM emp
GROUP BY deptno;
Description: Filters groups based on a condition. Example:
SELECT deptno, MAX(sal), MIN(sal), AVG(sal), SUM(sal)
FROM emp
GROUP BY deptno
HAVING MIN(sal) > 900;
Description: Adds new rows to a table. Example:
INSERT INTO dept(loc, deptno, dname)
VALUES ('MONTREAL', 50, 'Marketing');
Description: Modifies existing rows in a table. Example:
UPDATE dept
SET dname = 'Computer', loc = 'Toronto'
WHERE deptno = 52;
Description: Removes rows from a table. Example:
DELETE FROM dept
WHERE deptno = 52;
Description: Creates a new table in the database. Example:
CREATE TABLE student (sid NUMBER, sname VARCHAR2(50), birthdate DATE);
Description: Modifies the structure of an existing table. Examples:
- Add a column:
ALTER TABLE member ADD (gender CHAR(1));
- Drop a column:
ALTER TABLE member DROP COLUMN phone;
Description: Deletes a table and its data permanently. Example:
DROP TABLE student;
Description: Ensures that a column (or combination of columns) has unique values and cannot be NULL. Example:
ALTER TABLE member ADD CONSTRAINT member_mid_PK PRIMARY KEY (mid);
Description: Establishes a relationship between two tables. Example:
ALTER TABLE rental ADD CONSTRAINT rental_mid_FK FOREIGN KEY(mid) REFERENCES member(mid);
Description: Ensures all values in a column are unique. Example:
ALTER TABLE member ADD CONSTRAINT member_email_UK UNIQUE(email);
Description: Ensures that all values in a column satisfy a specific condition. Example:
ALTER TABLE video ADD CONSTRAINT video_price_CC CHECK (price >= 0);
Description: Ensures that a column cannot have NULL values. Example:
ALTER TABLE video MODIFY (title CONSTRAINT video_title_NN NOT NULL);
Description: Creates a point in a transaction to which you can roll back. Example:
SAVEPOINT savepoint_name;
Description: Undoes changes made in the current transaction. Example:
ROLLBACK TO savepoint_name;
Description: Saves all changes made in the current transaction. Example:
COMMIT;
Description: Creates a new database user. Example:
CREATE USER c##sofia IDENTIFIED BY 1234;
Description: Grants privileges to a user. Example:
GRANT connect, resource TO c##sofia;
Description: Deletes a user and all their objects. Example:
DROP USER c##sofia CASCADE;
Description: Modifies a user's attributes. Example:
ALTER USER c##sofia QUOTA 100M ON users;
Description: Creates a sequence to generate unique values. Example:
CREATE SEQUENCE dept_sequence START WITH 52 INCREMENT BY 2;
Description: Retrieves the next or current value of a sequence. Examples:
- Get the next value:
SELECT dept_sequence.NEXTVAL FROM dual;
- Get the current value:
SELECT dept_sequence.CURRVAL FROM dual;
Description: Modifies an existing sequence. Example:
ALTER SEQUENCE dept_sequence INCREMENT BY 5;
Description: Creates or updates a view. Example:
CREATE OR REPLACE VIEW employee AS
SELECT empno, ename, job, deptno FROM emp;
Description: Restricts a view to be read-only. Example:
CREATE OR REPLACE VIEW employee_read AS
SELECT empno, ename, job, deptno FROM emp
WITH READ ONLY;
Description: Ensures that data modifications through a view adhere to the view's WHERE clause. Example:
CREATE OR REPLACE VIEW employee_30 AS
SELECT empno, ename, job, sal, deptno
FROM emp
WHERE deptno = 30
WITH CHECK OPTION;
Description: Creates a view even if the base tables do not exist. Example:
CREATE OR REPLACE FORCE VIEW child_detail AS
SELECT mid, mname, cid, cname, mom_id
FROM mother, child
WHERE mother.mid = child.mom_id;
Description: Extracts a substring from a string. Example:
SELECT term_desc, SUBSTR(term_desc, 1, 4)
FROM term;
Description: Converts strings to uppercase, lowercase, or capitalizes the first letter. Examples:
- Uppercase:
SELECT UPPER(s_last) FROM student;
- Lowercase:
SELECT LOWER(s_last) FROM student;
- Capitalize:
SELECT INITCAP(s_last) FROM student;
Description: Replaces occurrences of a substring within a string. Example:
SELECT REPLACE(term_desc, '200', '202') FROM term;
Description: Pads a string with characters on the left or right. Example:
SELECT LPAD(credits, 3, '0') FROM course;
Description: Removes characters from the left or right of a string. Example:
SELECT LTRIM(call_id, 'MIS ') FROM course;
Description: Returns the length of a string. Example:
SELECT LENGTH('example') FROM dual;
Description: Returns the remainder of a division operation. Example:
SELECT MOD(10, 3)
FROM dual;
Description: Raises a number to the power of another number. Example:
SELECT POWER(2, 3)
FROM dual;
Description: Performs mathematical operations on numbers. Examples:
- Absolute value:
SELECT ABS(-5) FROM dual;
- Round up:
SELECT CEIL(259.01) FROM dual;
- Round down:
SELECT FLOOR(259.99) FROM dual;
Description: Rounds a number to a specified precision. Example:
SELECT ROUND(123.456, 2)
FROM dual;
Description: Adds a specified number of months to a date. Example:
SELECT ADD_MONTHS(sysdate, 11)
FROM dual;
Description: Returns the last day of the month for a given date. Example:
SELECT LAST_DAY(TO_DATE('19 02 2024', 'DD MM YYYY'))
FROM dual;
Description: Calculates the number of months between two dates. Example:
SELECT MONTHS_BETWEEN(sysdate, TO_DATE('25 03 1997', 'DD MM YYYY')) FROM dual;
Description: Represents a period of time. Examples:
- Year to month:
SELECT TO_YMINTERVAL('0-11') FROM dual;
- Day to second:
SELECT TO_DSINTERVAL('0 23:59:30') FROM dual;
Description: Converts a date or number to a string. Example:
SELECT TO_CHAR(sysdate, 'DD Month YYYY Day Year HH:MI:SS Am')
FROM dual;
Description: Checks for NULL or non-NULL values. Examples:
- IS NOT NULL:
SELECT empno, ename, job, sal, comm
FROM emp
WHERE comm IS NOT NULL;
- IS NULL:
SELECT empno, ename, job, sal, comm
FROM emp
WHERE comm IS NULL;
Description: Replaces NULL with a specified value. Example:
SELECT empno, ename, sal, NVL(comm, 0), sal * 12 + NVL(comm, 0) "Annual Salary"
FROM emp;
Description: Returns different values based on whether a column is NULL or NOT NULL. Example:
SELECT NVL2(comm, 2000, 500) FROM emp;
Description: Writes the output of SQL commands to a file. Example:
SPOOL c:\DB1\output.txt;
Description: Returns the number of rows matching a condition. Example:
SELECT COUNT(*)
FROM emp;
Description: Aggregate functions to calculate maximum, minimum, average, and sum. Example:
SELECT MAX(sal), MIN(sal), AVG(sal), SUM(sal)
FROM emp;
Description: Sorts the result set in ascending or descending order. Example:
SELECT ename, sal
FROM emp
ORDER BY sal DESC;
Description: Searches for a specified pattern in a column. Examples:
- Starts with 'M':
SELECT empno, ename, job, sal
FROM emp
WHERE ename LIKE 'M%';
- Ends with 'N':
SELECT empno, ename, job, sal
FROM emp
WHERE ename LIKE '%N';
Description: Combines rows from two or more tables based on a related column. Examples:
- Inner Join:
SELECT empno, ename, job, emp.deptno, dept.dname
FROM emp, dept
WHERE emp.deptno = dept.deptno;
- Outer Join:
SELECT empno, ename, job, emp.deptno, dept.dname
FROM emp, dept
WHERE emp.deptno = dept.deptno(+);