SQL Query Command Syntaxes
1. CREATE TABLE
CREATE TABLE table_name (
column1 datatype constraints,
column2 datatype constraints,
...
);
2. ALTER TABLE
- Add a new column:
ALTER TABLE table_name ADD column_name datatype;
- Modify a column:
ALTER TABLE table_name MODIFY column_name new_datatype;
- Drop a column:
ALTER TABLE table_name DROP COLUMN column_name;
3. DROP TABLE
DROP TABLE table_name;
4. INSERT INTO
- Insert values into all columns:
INSERT INTO table_name VALUES (value1, value2, ...);
- Insert values into specific columns:
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
5. UPDATE
UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;
6. DELETE
DELETE FROM table_name WHERE condition;
7. SELECT
- Select all columns:
SELECT * FROM table_name;
- Select specific columns:
SELECT column1, column2 FROM table_name;
- With WHERE condition:
SELECT * FROM table_name WHERE condition;
8. CREATE INDEX
CREATE INDEX index_name ON table_name (column_name);
9. DROP INDEX
DROP INDEX index_name;
10. CREATE VIEW
CREATE VIEW view_name AS SELECT column1, column2 FROM table_name WHERE condition;
11. DROP VIEW
DROP VIEW view_name;
12. JOINS
- INNER JOIN:
SELECT columns FROM table1 INNER JOIN table2 ON table1.column = table2.column;
- LEFT JOIN:
SELECT columns FROM table1 LEFT JOIN table2 ON table1.column = table2.column;
- RIGHT JOIN:
SELECT columns FROM table1 RIGHT JOIN table2 ON table1.column = table2.column;
- FULL JOIN:
SELECT columns FROM table1 FULL JOIN table2 ON table1.column = table2.column;
13. GROUP BY & HAVING
SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name HAVING COUNT(*) > value;
14. ORDER BY
SELECT * FROM table_name ORDER BY column_name ASC/DESC;
15. LIMIT (For MySQL, PostgreSQL)
SELECT * FROM table_name LIMIT number;
16. FETCH (For Oracle)
SELECT * FROM table_name FETCH FIRST number ROWS ONLY;
17. CASE Statement
SELECT column_name,
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
ELSE default_result
END AS alias_name
FROM table_name;
18. UNION & UNION ALL
SELECT column_names FROM table1 UNION SELECT column_names FROM table2;
SELECT column_names FROM table1 UNION ALL SELECT column_names FROM table2;