SQL

Table of Contents

1. SELECT

The fundamental command to query and retrieve data from a database.

SELECT column1, column2 FROM table_name WHERE condition;

2. INSERT

Insert one or more new rows into a specified table.

INSERT INTO table_name (column1, column2) VALUES (value1, value2);

3. UPDATE

Update the values of existing records in a specified table.

UPDATE table_name SET column1 = value1 WHERE condition;

4. DELETE

Delete one or more rows from a table based on a specified condition.

DELETE FROM table_name WHERE condition;

5. CREATE TABLE

Create a new table with specified columns, data types, and constraints.

CREATE TABLE table_name (
  column1 datatype,
  column2 datatype,
  ...
);

6. ALTER TABLE

Add, modify, or delete columns in an existing table.

ALTER TABLE table_name
ADD column_name datatype;

7. DROP TABLE

Permanently remove a table and its data from the database.

DROP TABLE table_name;

8. CREATE INDEX

Improve the speed of data retrieval by creating an index on one or more columns.

CREATE INDEX index_name ON table_name (column1, column2, ...);

9. SELECT DISTINCT

Retrieve only unique values from a specified column or set of columns.

SELECT DISTINCT column1 FROM table_name;

10. WHERE

Specify conditions to filter records in a SELECT, UPDATE, or DELETE statement.

SELECT * FROM table_name WHERE condition;

11. GROUP BY

Group rows based on common values in one or more columns.

SELECT column1, COUNT(*) FROM table_name GROUP BY column1;

12. HAVING

Apply a condition to filter the results of a GROUP BY clause.

SELECT column1, COUNT(*) FROM table_name GROUP BY column1 HAVING COUNT(*) > 1;

13. ORDER BY

Arrange the output of a SELECT statement in ascending or descending order.

SELECT * FROM table_name ORDER BY column1 ASC;

14. JOIN

Merge data from multiple tables based on a specified condition.

SELECT * FROM table1 JOIN table2 ON table1.column_name = table2.column_name;

15. COUNT

Calculate the number of rows that satisfy a given condition.

SELECT COUNT(*) FROM table_name WHERE condition;