SQL Commands: From Basics to Advanced
SQL Commands Guide
1. Basic SQL Commands
1.1. Database Operations
- CREATE DATABASE database_name;
- USE database_name;
- SHOW DATABASES;
- DROP DATABASE database_name;
1.2. Table Operations
- CREATE TABLE table_name (column1 datatype, column2 datatype, ...);
- SHOW TABLES;
- DESCRIBE table_name;
- DROP TABLE table_name;
- RENAME TABLE old_table_name TO new_table_name;
1.3. Insert, Update, Delete
- INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
- UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;
- DELETE FROM table_name WHERE condition;
- TRUNCATE TABLE table_name;
2. Intermediate SQL Commands
2.1. Select Queries
- SELECT column1, column2 FROM table_name;
- SELECT * FROM table_name;
- SELECT DISTINCT column_name FROM table_name;
- SELECT * FROM table_name WHERE condition;
- SELECT * FROM table_name WHERE condition1 AND condition2;
- SELECT * FROM table_name WHERE condition1 OR condition2;
Page 1
SQL Commands: From Basics to Advanced
- SELECT * FROM table_name WHERE NOT condition;
2.2. Sorting and Limiting
- SELECT * FROM table_name ORDER BY column_name ASC;
- SELECT * FROM table_name ORDER BY column_name DESC;
- SELECT * FROM table_name LIMIT 10;
- SELECT * FROM table_name LIMIT 10 OFFSET 5;
2.3. Functions
- SELECT COUNT(column_name) FROM table_name;
- SELECT AVG(column_name) FROM table_name;
- SELECT SUM(column_name) FROM table_name;
- SELECT MIN(column_name) FROM table_name;
- SELECT MAX(column_name) FROM table_name;
- SELECT UPPER(column_name) FROM table_name;
- SELECT LOWER(column_name) FROM table_name;
- SELECT LENGTH(column_name) FROM table_name;
- SELECT NOW();
- SELECT CURDATE();
- SELECT YEAR(column_name) FROM table_name;
- SELECT MONTH(column_name) FROM table_name;
3. Joins
3.1. Types of Joins
- SELECT columns FROM table1 INNER JOIN table2 ON table1.column = table2.column;
- SELECT columns FROM table1 LEFT JOIN table2 ON table1.column = table2.column;
- SELECT columns FROM table1 RIGHT JOIN table2 ON table1.column = table2.column;
- SELECT columns FROM table1 FULL OUTER JOIN table2 ON table1.column = table2.column;
3.2. Self Join
- SELECT a.column1, b.column2 FROM table_name a, table_name b WHERE a.common_column =
b.common_column;
Page 2
SQL Commands: From Basics to Advanced
4. Advanced SQL Commands
4.1. Subqueries
- SELECT column1, (SELECT COUNT(*) FROM another_table) AS count FROM table_name;
- SELECT column1 FROM table_name WHERE column2 = (SELECT MAX(column2) FROM another_table);
4.2. Views
- CREATE VIEW view_name AS SELECT column1, column2 FROM table_name WHERE condition;
- SELECT * FROM view_name;
- DROP VIEW view_name;
4.3. Indexes
- CREATE INDEX index_name ON table_name (column_name);
- DROP INDEX index_name ON table_name;
4.4. Transactions
- START TRANSACTION;
- COMMIT;
- ROLLBACK;
4.5. Stored Procedures
- DELIMITER //
- CREATE PROCEDURE procedure_name (parameters) BEGIN SQL_statements; END //
- DELIMITER ;
- CALL procedure_name(parameters);
- DROP PROCEDURE procedure_name;
4.6. Triggers
- CREATE TRIGGER trigger_name BEFORE|AFTER INSERT|UPDATE|DELETE ON table_name FOR
EACH ROW BEGIN SQL_statements; END;
- DROP TRIGGER trigger_name;
Page 3