SQL Quick Reference

This is a quick reference guide to common SQL commands and their syntax. Note that specific syntax might vary slightly depending on the database system (MySQL, SQL Server, PostgreSQL, etc.).



Basic SQL Statements

SELECT

Syntax

SELECT column_name(s) FROM table_name;
SELECT * FROM table_name; -- Selects all columns
SELECT DISTINCT column_name(s) FROM table_name; -- Selects unique values
      
Output

(Returns selected columns or all columns from the specified table. DISTINCT removes duplicate rows.)
      

WHERE

Syntax

SELECT column_name(s)
FROM table_name
WHERE condition AND|OR condition;
      
Output

(Returns rows that satisfy the specified conditions.)
      

ORDER BY

Syntax

SELECT column_name(s)
FROM table_name
ORDER BY column_name [ASC|DESC];
      
Output

(Returns rows sorted by the specified column in ascending (ASC) or descending (DESC) order.)
      

GROUP BY

Syntax

SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE condition
GROUP BY column_name;
      
Output

(Groups rows with the same values in the specified column and applies aggregate functions.)
      

HAVING

Syntax

SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE condition
GROUP BY column_name
HAVING aggregate_function(column_name) operator value;
      
Output

(Filters grouped rows based on aggregate function results.)
      

BETWEEN

Syntax

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
      
Output

(Selects rows where a value falls within a specified range, inclusive.)
      

IN

Syntax

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
      
Output

(Selects rows where a value matches any value in a list.)
      

LIKE

Syntax

SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;
      
Output

(Selects rows based on a pattern match.)
      

AS (alias)

Syntax

SELECT column_name AS column_alias
FROM table_name;

SELECT column_name
FROM table_name AS table_alias;
      
Output

(Creates aliases for columns or tables.)
      

Database Statements

CREATE DATABASE

Syntax

CREATE DATABASE database_name;
      
Output

(Creates a new database.)
      

CREATE TABLE

Syntax

CREATE TABLE table_name (
column_name1 data_type,
column_name2 data_type,
column_name3 data_type,
...
);
      
Output

(Creates a new table.)
      

CREATE INDEX

Syntax

CREATE [UNIQUE] INDEX index_name
ON table_name (column_name);
      
Output

(Creates a new index. UNIQUE prevents duplicate values.)
      

CREATE VIEW

Syntax

CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition;
      
Output

(Creates a new view based on a SELECT statement.)
      

DROP DATABASE

Syntax

DROP DATABASE database_name;
      
Output

(Deletes a database.)
      

DROP TABLE

Syntax

DROP TABLE table_name;
      
Output

(Deletes a table.)
      

DROP INDEX

Syntax

DROP INDEX table_name.index_name; --SQL Server
DROP INDEX index_name ON table_name; --MS Access
DROP INDEX index_name; --DB2/Oracle
ALTER TABLE table_name DROP INDEX index_name; --MySQL
      
Output

(Deletes an index.)
      

Data Manipulation Statements

INSERT INTO

Syntax

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

(Inserts new rows into a table.)
      

UPDATE

Syntax

UPDATE table_name SET column1=value, column2=value,... WHERE condition;
      
Output

(Updates existing rows in a table.)
      

DELETE

Syntax

DELETE FROM table_name WHERE condition;
      
Output

(Deletes rows from a table.)
      

TRUNCATE TABLE

Syntax

TRUNCATE TABLE table_name;
      
Output

(Deletes all rows from a table, faster than DELETE.)
      

ALTER TABLE

Syntax

ALTER TABLE table_name
ADD column_name datatype
OR
ALTER TABLE table_name
DROP COLUMN column_name;
      
Output

(Adds or removes columns from a table.)
      

Join Statements

INNER JOIN

Syntax

SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name;
      
Output

(Returns rows only when there is a match in both tables.)
      

LEFT JOIN

Syntax

SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name;
      
Output

(Returns all rows from the left table, even if there is no match in the right table.)
      

RIGHT JOIN

Syntax

SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON table_name1.column_name=table_name2.column_name;
      
Output

(Returns all rows from the right table, even if there is no match in the left table.)
      

FULL OUTER JOIN

Syntax

SELECT column_name(s)
FROM table_name1
FULL OUTER JOIN table_name2
ON table_name1.column_name=table_name2.column_name;
      
Output

(Returns all rows when there is a match in either left or right table.)
      

Set Operators

UNION

Syntax

SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2;
      
Output

(Combines the result sets of two SELECT statements, removing duplicate rows.)
      

UNION ALL

Syntax

SELECT column_name(s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2;
      
Output

(Combines the result sets of two SELECT statements, including duplicate rows.)
      

Other Statements

EXISTS

Syntax

IF EXISTS (SELECT * FROM table_name WHERE condition)
BEGIN
--do what needs to be done if exists
END
ELSE
BEGIN
--do what needs to be done if not
END;
      
Output

(Checks if a record exists and executes different code blocks based on the result.)
      

SELECT INTO

Syntax

SELECT * INTO new_table_name FROM old_table_name;
      
Output

(Creates a new table and populates it with data from another table.)
      

SELECT TOP

Syntax

SELECT TOP number|percent column_name(s) FROM table_name;
      
Output

(Selects a specified number or percentage of rows from a table.)