SQL Cheat sheet

SQL Cheat sheet:

After following this SQL Cheat sheet we will be in a position to understand quickly and use SQL statement as per requirements. 
Below are the SQL commands Cheat sheet mentioned which perform different kinds of operations.
Various SQL types: DRL, DDL, DML, TCL, DCL 

DESCRIPTION select syntax:
DRL: DATA RETRIEVAL LANGUAGE COMMANDS
To select entire data from the given table<table_name> Select *
from <table_name>;
To select two columns from a table Select col1, col2
from <table_name>;
To select data from a table based on a condition Select col1, col2
from <table_name>

WHERE condition;
To select distinct records from a table Select distinct col
from <table_name>;
To select distinct records from a table based on a condition to filter the data Select distinct col
from <table_name>

WHERE condition
;
To select data from a table and sort the records in ascending order Select col1, col2
from <table_name>
ORDER BY col2
ASC;
To select data from a table and sort the records in descending order Select col1, col2
from <table_name>
ORDER BY col2
DESC;
To select data from table to aggregate the data based on a column. Select aggregate_func(col1), col2
from <table_name>
GROUP BY col2;
To select data from table to aggregate the data based on a column and a condition. Select aggregate_func(col1), col2
from <table_name>
GROUP BY col2
HAVING condition
;
To select data from multiple tables and inner join the result based on a condition and display a single result Select col1, col2
from <table1_name>
INNER JOIN <table2_name>ON condition;
To select data from multiple tables and left join the result based on a condition and display as a single result Select col1, col2
from <table1_name>
LEFT JOIN <table2_name> ON condition;
To select data from multiple tables and right join the result based on a condition and display as a single result Select col1, col2
from <table1_name>
RIGHT JOIN <table2_name> ON condition;
To select data from multiple tables and full outer join the result based on a condition and display as a single result Select col1, col2
from <table1_name>
FULL OUTER JOIN <table2_name> ON condition;
To select data from multiple tables and cross join the result to obtain a Cartesian product of the records and display as a single result Select col1, col2
from <table1_name>
CROSS JOIN <table2_name>;
To select data from multiple tables and combine the results of records from two tables. Select col1, col2
from <table1_name>
UNION
select col1, col2 from <table2_name>;
DDL: DATA DEFINITION LANGUAGE COMMANDS
To create a table with new columns and all the column definition can be mentioned by a comma. CREATE TABLE <table_name>
(col_name DATA_TYPE CONSTRAINT);
To add a new column to the table. ALTER TABLE <table_name>;ALTER TABLE <table_name> ADD(new_col_name DATA_TYPE);
To delete the entire table from the database. DROP TABLE <table_name>;
To rename the table to a new name. ALTER TABLE <table_name> RENAME TO <new_table_name>;
To truncate the table(remove all tuples permanently at one go) TRUNCATE TABLE <table_name>;
DML: DATA MANIPULATION LANGUAGE COMMANDS
To insert data into a table for one record. INSERT INTO <table_name>(list of columns)VALUES(list of values);
To update a cell value in a table based on column name UPDATE <table_name> SET col1=updated_value;
To update multiple cell values in a table based on column names and condition UPDATE <table_name> SET col1=updated_value1, col2=updated_value2 WHERE condition;
To delete the complete data in a table. DELETE FROM <table_name>;
To delete the complete data in a table based on a condition. DELETE FROM <table_name>
WHERE condition
;
TCL: TRANSACTION CONTROL LANGUAGE COMMANDS
to save the data change till current time. COMMIT;
to undo the changes till a particular savepoint mentioned. ROLLBACK;
to mention a pointer to the instance so that later we can roll back the changes till that particular save point. SAVEPOINT <save_point_name> ;
DCL: DATA CONTROL LANGUAGE COMMANDS
To grant access to a particular user based on the grant option and access requirement. GRANT <privileges> ON <obj_name> TO <user>;
To revoke access to a particular user from a particular object REVOKE <privileges> ON <obj_name> FROM <user>;

In this cheat sheet, we may need to refer various DATA_TYPE usage

Leave a Comment

Your email address will not be published. Required fields are marked *

Twitter
YouTube
Pinterest
LinkedIn