While learning or studying about SQL, it is always hard to remember everything as a human being. So, we have comprised all important commands, keywords, etc to be remembered and in short, we provide SQL Cheatsheet here in this article.
What is SQL?
SQL stands for Structured Query Language. SQL is a database computer language designed for storing, manipulating and retrieving data in databases.
What are SQL statements?
A statement is any text that the database engine recognizes as a valid command. There are 5 types of SQL Statements and they are:
- DDL – Data Definition Language
- DQL – Data Query Language
- DML – Data Manipulation Language
- DCL – Data Control Language
- TCL – Transaction Control Language
To be Remembered – SQL Cheatsheet
Commands for SQL Statements
- DDL – CREATE | DROP | ALTER | TRUNCATE | RENAME | COMMENT
- DQL – SELECT
- DML – INSERT | UPDATE | DELETE | LOCK CALL | EXPLAIN PLAN
- DCL – GRANT | REVOKE
- TCL – COMMIT | ROLLBACK | SAVEPOINT | SET TRANSACTION
Important Keywords
- WHERE | DISTINCT | LIMIT | ORDER BY | DESC | ASC | AS
Database Objects
- TABLE | VIEW | SYNONYM | SEQUENCE | INDEX | TRIGGER
Constraints
- NOT NULL | UNIQUE | PRIMARY KEY | FOREIGN KEY | CHECK | DEFAULT
Aggregation Functions
- AVG | COUNT | MAX | MIN | SUM
Aggregation Keywords
- GROUP BY | HAVING
Operators
Arithmetic + – * / % Bitwise & ^ | Comparison = < > <= >= !< !> <> != Compound += -= *= /= %= &= != ^= Logical AND | OR | NOT | ANY | SOME | ALL | BETWEEN | IN | EXISTS | LIKE | IS NULL | UNIQUE
Joins
Inner Join Left [Outer] Join Right [Outer] Join Full Outer Join
Set Operations
UNION | UNION ALL INTERSECT EXCEPT | MINUS
SQL Query Examples:
DDL Examples:
Create a Table:
CREATE TABLE Students ( rollno int PRIMARY KEY, fname varchar(255) NOT NULL, lname varchar(255) );
Adding a new column to the Table:
ALTER TABLE Students ADD email varchar(255);
Modifying the data type of existing column:
ALTER TABLE Students ALTER COLUMN lname varchar(512);
Removing a existing column from the Table:
ALTER TABLE Students DROP COLUMN email
Truncate (remove all data) a Table:
TRUNCATE TABLE Students;
Drop a Table:
DROP TABLE Students;
DQL Examples:
Fetch all data from a Table:
SELECT * FROM Students;
Fetch count of records:
SELECT count(*) FROM Students;
Filter data from a Table:
SELECT * FROM Students WHERE rollno=1234; SELECT * FROM Students WHERE rollno>1234 AND age < 15;
Fetch selected columns:
SELECT fname,lname FROM Students WHERE rollno>1234 AND age < 15;
Fetch maximum 10 rows:
SELECT fname,lname FROM Students WHERE rollno>1234 AND age < 15 LIMIT 10;
Fetch Maximum Age:
SELECT max(age) FROM Students;
Fetch Minimum Age:
SELECT min(age) FROM Students;
Fetch Sum of Age:
SELECT sum(age) FROM Students;
Fetch Average Age:
SELECT avg(age) FROM Students;
Fetch Average Age for each Gender:
SELECT avg(age) FROM Students GROUP BY gender;
Sort (Order) Fetched Records:
SELECT fname,lname FROM Students WHERE rollno>1234 AND age < 15 ORDER BY gender;
Sort in Descending Order:
SELECT fname,lname FROM Students WHERE rollno>1234 AND age < 15 ORDER BY gender DESC;
Fetch from 2 Tables:
SELECT fname,classteacher FROM Students INNER JOIN Section ON Students.section=Section.id;
Aggregate and Filter:
SELECT section,count(*) AS studentcount FROM Students GROUP BY section HAVING count(*) > 20;
Full Outer Join:
SELECT fname,classteacher FROM Students FULL JOIN Section ON Students.section=Section.id;
DML Examples:
Insert data (rows) into a Table:
INSERT INTO Students (rollno, fname, lname) VALUES (12345,'Saranya','Karthik');
Update data (value of Column) of a Table:
UPDATE Students SET lname='Kumar' WHERE rollno=12346;
Delete data (rows) from a Table:
DELETE FROM Students WHERE rollno=1234
For more detailed Tutorial, Please refer