Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

SQL Cheatsheet

Basic Queries & Operators JOINs Order, Group, Aggregate

SELECT column1, column2 FROM table; SELECT column1, column2 SELECT column1, column2 FROM table
Query data in columns column1, column2 from a table. FROM table1 ORDER BY column1 [ASC][DESC];
SELECT * FROM table; INNER JOIN table2 ON condition; Sorts the results in ascending or descending order.
Query all rows and columns from a table Inner join table1 and table2.
SELECT column1, aggregate_function_name(column2)
SELECT column1, column2 FROM table SELECT column1, column2 FROM table
WHERE condition; FROM table1 GROUP BY column1;
Query data and filter rows using a boolean condition: =, <, LEFT JOIN table2 ON condition; Groups rows using an aggregate function.
<=, >, >=, <>. Left join table1 and table2. SELECT column1, aggregate_function_name(column2)
SELECT column1, column2 FROM table1 FROM table
WHERE column1[NOT] LIKE pattern; GROUP BY column1;
SELECT column1, column2
Query rows using pattern matching.  Use with % or _ HAVING condition;
FROM table1
Filter groups using HAVING operator.
RIGHT JOIN table2 ON condition;
SELECT column1, column2 FROM table
Right join table1 and table2
WHERE column1 [NOT] IN value_list;
AGGREGATE FUNCTIONS
Filters rows with values equals to those in the value_list.
SELECT column1, column2 AVG ⇒ Returns the average of a list.
FROM table1 COUNT ⇒ Returns the number of elements of a list.
SELECT column1, column2 FROM table
FULL OUTER JOIN table2 ON condition; SUM ⇒ Returns the total of a list.
WHERE column1 BETWEEN limit1 AND limit2;
Full outer join table1 and table2 MAX ⇒ Returns the maximum value in a list.
Filters rows with values between the two limits.
SELECT column1, column2 MIN ⇒ Returns the minimum value in a list.
SELECT column1, column2 FROM table FROM table1
WHERE column1 IS [NOT] NULL; CROSS JOIN table2;
Filters NULL values. Cross join table1 and table2.
SELECT DISTINCT column1 FROM table Results also called as ⇒ E
C. ARTESIAN PRODUCTF
WHERE condition;
Returns distinct rows from a table SELECT column1, column2
SELECT column1, column2 FROM table FROM table1 A
WHERE rownum<n; INNER JOIN table1 B ON condition;
Returns the first n rows. Join table1 to itself using INNER JOIN.Also called as⇒SELF JOIN

Oracle Master Training | oraclemaster@outlook.com | All Rights Reserved.


SQL Cheatsheet
DML - Data Manipulation
DDL - Data Definition Language Constraints, Views, Triggers
Language
CREATE TABLE table_name( INSERT INTO table_name(column_list) CONSTRAINTS DEFINITION
id NUMBER PRIMARY KEY, VALUES (value_list);
CREATE TABLE table1(
column_name1 VARCHAR2 NOT NULL, Inserts one record into a table.
     col1 NUMBER PRIMARY KEY, -- primary key constraint
column_name2 DATE
    col2 NUMBER NOT NULL, -- NOT NULL constraint
); INSERT INTO table1(column_list)
     FOREIGN KEY (col2) REFERENCES table2(col2),-- Foreign Key
Creates a new table with three columns. SELECT column_list
     col3 NUMBER,
FROM table2;
     UNIQUE(col3), -- UNIQUE constraint
DROP TABLE table_name; Inserts rows from table table2 into table table1.
     CHECK (col3> 0 AND col3 >= col2) -- CHECK constraint
Deletes table from the database Columns types must match!
);
VIEWS
ALTER TABLE table_name ADD column_name; UPDATE table
Adds a new column to the table. SET column1 = new_value, CREATE [TEMPORARY] VIEW view_name(col1,col2)
column2 = new_value AS
ALTER TABLE table_name1 RENAME /*column3, column4, ... */; SELECT col1, col2
column_name1 TO column_name2; Updates values in the column column1 FROM table;
Renames column column_name1(old name) to and column2 for all rows. Creates a new view that consists of two columns from table t.
column_name2(new name).
DROP VIEW view_name;
UPDATE table Deletes the view.
ALTER TABLE table_name DROP COLUMN column_name;
SET column1 = new_value,
column2 = new_value TRIGGERS
Removes column column_name from the table.
WHERE condition; CREATE [OR ALTER] TRIGGER trigger_name
Updates values in the column column1, column2 that match BEFORE [OR AFTER] EVENT
ALTER TABLE old_table_name RENAME
the condition. ON table_name FOR EACH ROW [OR STATEMENT]
TO new_table_name; BEGIN
Renames a table from old_table_name to new_table_name. DELETE FROM table_name; ...
Deletes all data in a table. END;
TRUNCATE TABLE table_name; Create or modify a trigger.
Removes all data in a table. DELETE FROM table_name EVENT values: INSERT, UPDATE, DELETE
WHERE condition;
Deletes rows that match the condition. DROP TRIGGER trigger_name;
Deletes trigger.

Oracle Master Training | oraclemaster@outlook.com | All Rights Reserved.

You might also like