SQL: A Quick Reference Guide

This page provides a concise overview of common SQL commands and their usage. Remember that specific syntax might vary slightly depending on your database system (MySQL, PostgreSQL, SQL Server, Oracle, etc.). Always refer to your database system's documentation for precise details.



Basic SELECT Statements

Simple SELECT

Syntax

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

(Returns the specified columns or all columns from the table.)
      

SELECT DISTINCT

Syntax

SELECT DISTINCT column_name(s) FROM table_name;
      
Output

(Returns only unique rows, eliminating duplicates.)
      

WHERE Clause

Syntax

SELECT column_name(s) FROM table_name WHERE condition;
      
Output

(Returns rows that match the specified condition.)
      

AND, OR, NOT Operators

Syntax

SELECT column_name(s) FROM table_name WHERE condition1 AND|OR|NOT condition2;
      
Output

(Combines or negates conditions. AND requires both conditions to be true; OR requires at least one; NOT reverses the condition.)
      

ORDER BY Clause

Syntax

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

(Sorts the result set in ascending (ASC) or descending (DESC) order.)
      

Aggregate Functions: MIN(), MAX(), COUNT(), AVG(), SUM()

Syntax

SELECT MIN(column), MAX(column), COUNT(column), AVG(column), SUM(column) FROM table_name;
      
Output

(Returns the minimum, maximum, count, average, and sum of values in a column, respectively.)
      

LIKE Operator

Syntax

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

(Selects rows based on a pattern match using wildcards %, _, [], [!].)
      

IN Operator

Syntax

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

(Selects rows where the column value is in a given list.)
      

BETWEEN Operator

Syntax

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

(Selects rows where the column value is within a range, inclusive.)
      

AS (Alias)

Syntax

SELECT column_name AS alias FROM table_name;
      
Output

(Gives a column a temporary name (alias) within a query.)
      

JOINs (INNER, LEFT, RIGHT, FULL OUTER)

Syntax

SELECT column_name(s) FROM table1 JOIN|LEFT JOIN|RIGHT JOIN|FULL OUTER JOIN table2 ON condition;
      
Output

(Combines rows from two or more tables based on a related column.)
      

UNION and UNION ALL

Syntax

SELECT column_name(s) FROM table1 UNION|UNION ALL SELECT column_name(s) FROM table2;
      
Output

(Combines result sets from multiple SELECT statements. UNION removes duplicates; UNION ALL keeps them.)
      

GROUP BY Clause

Syntax

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

(Groups rows with the same values in specified columns.)
      

HAVING Clause

Syntax

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

(Filters grouped rows based on aggregate function results.)
      

EXISTS Keyword

Syntax

SELECT column_names FROM table1 WHERE EXISTS (SELECT 1 FROM table2 WHERE condition);
      
Output

(Checks for the existence of any rows in a subquery.)
      

ANY and ALL Keywords

Syntax

SELECT column_names FROM table1 WHERE column_name operator ANY|ALL (subquery);
      
Output

(Compares a value to multiple values returned by a subquery. ANY: at least one match; ALL: all values match.)
      

CASE Expression

Syntax

CASE WHEN condition THEN result ELSE result END;
      
Output

(Performs conditional logic within a query.)
      

Comments

Syntax

-- Single-line comment
/* Multi-line
comment */
      
Output

(Adds comments to your SQL code.)
      

Database and Table Statements

CREATE DATABASE

Syntax

CREATE DATABASE database_name;
      
Output

(Creates a new database.)
      

DROP DATABASE

Syntax

DROP DATABASE database_name;
      
Output

(Deletes a database.)
      

BACKUP DATABASE

Syntax

BACKUP DATABASE database_name TO DISK = 'filepath';
      
Output

(Creates a backup of a database.)
      

CREATE TABLE

Syntax

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

(Creates a new table.)
      

DROP TABLE

Syntax

DROP TABLE table_name;
      
Output

(Deletes a table.)
      

ALTER TABLE

Syntax

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

(Adds or deletes columns in a table.)
      

CONSTRAINTS (NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, DEFAULT)

Syntax

ALTER TABLE table_name ADD CONSTRAINT constraint_name constraint_type (column);
      
Output

(Adds constraints to a table to ensure data integrity.)
      

CREATE INDEX

Syntax

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

(Creates an index on a table to speed up data retrieval.  UNIQUE prevents duplicates.)
      

AUTO INCREMENT

Syntax

AUTO_INCREMENT
      
Output

(Automatically increments a numeric value for each new row.)
      

Dates

Syntax

Date functions vary by database system.
      
Output

(Functions for working with dates and times.)
      

VIEWS

Syntax

CREATE [OR REPLACE] VIEW viewname AS SELECT column FROM table WHERE condition;
      
Output

(Creates a virtual table based on a SELECT statement.)
      

Stored Procedures

Syntax

CREATE PROCEDURE procedure_name AS sql_statement;
      
Output

(Creates a reusable block of SQL code.)
      

EXEC (Execute Stored Procedure)

Syntax

EXEC procedure_name;
      
Output

(Executes a stored procedure.)