Back to Cheat Sheets
SQL Commands
Common SQL commands for querying and managing relational databases.
| Action | Command | Details |
|---|---|---|
| Select all | SELECT * FROM table; |
Retrieve all records from a table. |
| Select specific columns | SELECT column1, column2 FROM table; |
Retrieve specified columns from a table. |
| Where clause | SELECT * FROM table WHERE condition; |
Filter rows that satisfy a condition. |
| Insert row | INSERT INTO table (col1, col2) VALUES (val1, val2); |
Add a new row to a table. |
| Update row | UPDATE table SET col = val WHERE condition; |
Modify existing records. |
| Delete row | DELETE FROM table WHERE condition; |
Remove records matching condition. |
| Create table | CREATE TABLE table_name (id INT PRIMARY KEY, col VARCHAR(100)); |
Define a new table. |
| Drop table | DROP TABLE table_name; |
Delete a table and all its data. |
| Join tables | SELECT * FROM a JOIN b ON a.id = b.a_id; |
Combine rows from two tables based on related columns. |
| Group by | SELECT col, COUNT(*) FROM table GROUP BY col; |
Aggregate rows by column. |
| Order by | SELECT * FROM table ORDER BY col DESC; |
Sort query results. |
| Limit results | SELECT * FROM table LIMIT 10; |
Return limited number of rows. |
| Create index | CREATE INDEX idx_name ON table (col); |
Improve query performance. |
| Alter table add column | ALTER TABLE table ADD col INT; |
Add a new column. |
| Transaction start | BEGIN TRANSACTION; |
Start a database transaction. |
| Transaction commit | COMMIT; |
Save all changes made in the transaction. |
| Transaction rollback | ROLLBACK; |
Undo all changes in the transaction. |