SQL Statements Reference

SELECT

Retrieves data from one or more tables. This is one of the most fundamental statements in SQL.

SELECT [ALL | DISTINCT] column1, column2, ...
FROM table_name
[WHERE condition]
[GROUP BY column1, column2, ...]
[HAVING condition]
[ORDER BY column1 [ASC | DESC], ...]
SELECT CustomerName, City
FROM Customers
WHERE Country = 'USA'
ORDER BY City;

INSERT

Adds new rows of data into a table.

INSERT INTO table_name [(column1, column2, ...)]
VALUES (value1, value2, ...);
INSERT INTO Products (ProductName, Price)
VALUES ('Laptop', 1200.50);

UPDATE

Modifies existing records in a table.

UPDATE table_name
SET column1 = value1, column2 = value2, ...
[WHERE condition];
UPDATE Employees
SET Salary = Salary * 1.10
WHERE Department = 'Sales';

DELETE

Removes rows from a table.

DELETE FROM table_name
[WHERE condition];
DELETE FROM Orders
WHERE OrderDate < '2023-01-01';

CREATE TABLE

Creates a new table in the database.

CREATE TABLE table_name (
column1 datatype [constraints],
column2 datatype [constraints],
...
);
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(255) NOT NULL,
ContactName VARCHAR(255),
Email VARCHAR(255) UNIQUE
);

ALTER TABLE

Modifies an existing table structure, such as adding, deleting, or modifying columns.

ALTER TABLE table_name
[ADD column_name datatype [constraints]] |
[DROP COLUMN column_name] |
[MODIFY COLUMN column_name datatype [constraints]];
ALTER TABLE Products
ADD Description VARCHAR(500);

DROP TABLE

Deletes an entire table from the database.

DROP TABLE table_name;
DROP TABLE OldCustomers;

CREATE INDEX

Creates an index on a table to speed up data retrieval.

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

CREATE DATABASE

Creates a new database.

CREATE DATABASE database_name;
CREATE DATABASE MyCompanyDB;