SQL Cheat Sheet-WPS Office
SQL Cheat Sheet-WPS Office
We may earn a commission if you make a purchase through the links on our website.
Hitesh J
Oracle SQL is the world's most widely used database management system. It is used to store and
retrieve information. Oracle database is designed for enterprise grid computing.
PostgreSQL is an open-source, powerful and advanced version of SQL that supports different functions
of SQL including, foreign keys, subqueries, functions, and user-defined types.
In this quick reference cheat sheet, we will show Oracle SQL and PostgreSQL commands with examples.
A cheat sheet is a set of notes used for quick reference. In this Oracle Cheat Sheet, I will show you all
basic to advanced Oracle SQL commands with examples.
Basic Commands
test_log1a and test_log1b are the redo log groups and each contains 500 KB members.
export ORACLE_SID=test
To check the size of the database including, its Data files, Temporary files, Redo logs and Control files,
run the following command:
SELECT ROUND( SUM(Q1."Data Files" + Q2."Temp Files" + Q3."Redo Logs" + Q4."Control Files"
)/1024/1024/1024, 2) AS "Total Size (GB)" FROM (SELECT SUM(bytes) "Data Files" from
DBA_DATA_FILES) Q1, (SELECT SUM(bytes) "Temp Files" from DBA_TEMP_FILES) Q2, (SELECT
SUM(bytes) "Redo Logs" from V_$LOG) Q3, (SELECT SUM(BLOCK_SIZE * FILE_SIZE_BLKS)"Control Files"
FROM V$CONTROLFILE) Q4;
To view all privileges granted to a user on other users table, run the following command:
CREATE TABLE student ( id integer not null, name varchar2(20), CONSTRAINT student_id_constraint
UNIQUE (id) );
To add an extra column name Jayesh to student table, run the following command:
To modify the data type of a column in a table, run the following command:
To query all rows and columns from a table student, run the following command:
To query data in columns column1, column2 from a table student, run the following command:
To query data in columns column1, column2 from a table student and sort the result in ascending or
descending order, run the following command:
To query data in columns column1, column2 from multiple tables, run the following command:
To create an index named student_idx on a table named student, run the following command:
ON student (student_name);
RENAME TO teacher_idx;
AS
FROM student;
AS
FROM student;
AS
FROM student;
Advanced Commands
CURRENT_TIMESTAMP
SYSDATE
Remove all contents from recycle bin, run the following command:
PURGE RECYCLEBIN;
List all tables in the current schema, run the following command:
Create a backup table from the student table, run the following command:
Restore a backup table into the original table, run the following command:
In this section, we will show you basic and advanced PostgreSQL commands with examples.
\l
\c dbname;
To grant a user the ability to create a new database, run the following command:
\du
CREATE TABLE tablename( pk SERIAL PRIMARY KEY, c1 type(size) NOT NULL, c2 type(size) NULL, );
\d
SELECT column_list
FROM table;
FROM tablename;
SELECT select_list FROM table ORDER BY column ASC [DESC], column2 ASC [DESC],...;
To delete all row of a table, run the following command:
query;
AS
query
To create a new index on the specified table, run the following command:
ON tablename (column,...);
pg_dumpall -f alldb_backup.sql
su - postgres
su - postgres
psql -f alldb_backup.sql
Conclusion
In the above guide, you learned basic and advanced SQL and PostgreSQL commands with examples. I
hope this will help you in your day-to-day database operations.
To retrieve data from a database using SQL, use the SELECT command along with the desired columns
and conditions. For example, the following SQL command retrieves all columns from the "customers"
table where the "city" column is equal to "London":
To insert data into a database using SQL, use the INSERT INTO command along with the desired columns
and values. For example, the following SQL command inserts a new customer record into the
"customers" table:
INSERT INTO customers (first_name, last_name, city) VALUES ('John', 'Doe', 'London')
How do I update data in a database using SQL?
To update data in a database using SQL, use the UPDATE command along with the desired columns and
conditions. For example, the following SQL command updates the "city" column for all customers in the
"customers" table where the "first_name" column is equal to "John":
To delete data from a database using SQL, use the DELETE command along with the desired conditions.
For example, the following SQL command deletes all customers from the "customers" table where the
"city" column is equal to "London":
To create a table in a database using SQL, use the CREATE TABLE command along with the desired
columns and data types. For example, the following SQL command creates a "customers" table with
three columns: "first_name", "last_name", and "city":