My SQL Cheat Sheet
My SQL Cheat Sheet
Introduction
This document provides an overview of basic SQL commands that can be executed in a
command-line interface (CMD) using popular databases such as MySQL, PostgreSQL, or
SQLite. Each section includes SQL command syntax followed by an example to help
you understand how to use the commands effectively.
Database Commands
Table Commands
4. Drop a Table: sql DROP TABLE tablename; Example: DROP TABLE students;
1. Insert Data into Table: sql INSERT INTO tablename (column1, column2,
...) VALUES (value1, value2, ...); Example: INSERT INTO students
(id, name, age, grade) VALUES (1, 'John Doe', 18, 'A');
2. Select Data from Table: sql SELECT column1, column2, ... FROM
tablename; Example: SELECT name, age FROM students;
3. Update Data in Table: sql UPDATE tablename SET column1 = value1 WHERE
condition; Example: UPDATE students SET age = 19 WHERE id = 1;
4. Delete Data from Table: sql DELETE FROM tablename WHERE condition;
Example: DELETE FROM students WHERE id = 1;
Join Commands
1. Inner Join: sql SELECT columns FROM table1 INNER JOIN table2 ON
table1.column = table2.column; Example: SELECT students.name,
classes.subject FROM students INNER JOIN classes ON students.id =
classes.student_id;
2. Left Join: sql SELECT columns FROM table1 LEFT JOIN table2 ON
table1.column = table2.column; Example: SELECT students.name,
classes.subject FROM students LEFT JOIN classes ON students.id =
classes.student_id;
3. Right Join: sql SELECT columns FROM table1 RIGHT JOIN table2 ON
table1.column = table2.column; Example: SELECT students.name,
classes.subject FROM students RIGHT JOIN classes ON students.id =
classes.student_id;
Constraints
1. Add Primary Key: sql ALTER TABLE tablename ADD PRIMARY KEY
(columnname); Example: ALTER TABLE students ADD PRIMARY KEY (id);
2. Add Foreign Key: sql ALTER TABLE tablename ADD FOREIGN KEY
(columnname) REFERENCES othertable(columnname); Example: ALTER TABLE
classes ADD FOREIGN KEY (student_id) REFERENCES students(id);
Where Clause
Explanation: Selects all records from the 'employees' table where the department is
'HR'.
Order By Clause
The ORDER BY clause sorts the result set in ascending or descending order.
Group By Clause
GROUP BY groups rows with the same values into summary rows.
Having Clause
Inner Join
Explanation: Retrieves employee names and their departments where IDs match.
Left Join
LEFT JOIN returns all records from the left table and matched records from the right.
Explanation: Retrieves all employees and departments, even those without matches.
Right Join
RIGHT JOIN returns all records from the right table and matched records from the left.
FULL JOIN returns all records when there is a match in either table.
Explanation: Adds a primary key to the 'id' column of the employees table.
Create Index
Drop Index
Begin Transaction
Command: BEGIN;
Example: BEGIN;
Commit Transaction
Example: COMMIT;
Rollback Transaction
Command: ROLLBACK;
Example: ROLLBACK;
Explanation: Rolls back changes, restoring the database to its previous state.
Create User
Grant Privileges
Revoke Privileges
Explanation: Revokes all privileges on the 'dbname' database from the user.
Show Users
Drop User