SQL Quick Reference
| SQL Statements | Syntax | Description |
|---|---|---|
| AND / OR | SELECT column_name(s) FROM table_name WHERE condition AND|OR condition |
AND: Represents logical AND OR: Represents logical OR |
| ALTER TABLE | ALTER TABLE table_name ADD column_name datatype |
Used to modify the structure of an existing table, adding or removing columns. |
| AS (alias) | SELECT column_name AS column_alias FROM table_name |
Used to specify an alias for a column or table. |
| BETWEEN | SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2 |
Used to filter records within a specified range. |
| CREATE DATABASE | CREATE DATABASE database_name |
Used to create a new database. |
| CREATE TABLE | CREATE TABLE table_name (column_name1 data_type, column_name2 data_type, ...) |
Used to create a new table, defining the columns and data types of the table. |
| CREATE INDEX | CREATE INDEX index_name ON table_name (column_name) |
Used to create an index on a table's column(s) to speed up queries. |
| CREATE VIEW | CREATE VIEW view_name AS SELECT column_name(s) FROM table_name WHERE condition |
Used to create a view to save the results of complex queries. |
| DELETE | DELETE FROM table_name WHERE some_column=some_value |
Used to delete records from a table,DELETE FROM table_nameandDELETE * FROM table_namewill delete all records. |
| DROP DATABASE | DROP DATABASE database_name |
Used to delete a database. |
| DROP INDEX | DROP INDEX table_name.index_name (SQL Server) |
Used to delete an index on a table. |
| DROP TABLE | DROP TABLE table_name |
Used to delete a table and all its data. |
| GROUP BY | SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name |
Used to group a result set by one or more columns. |
| HAVING | SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name HAVING aggregate_function(column_name) operator value |
Used to filter the grouped result set. |
| IN | SELECT column_name(s) FROM table_name WHERE column_name IN (value1, value2, ...) |
Used to filter records matching a value in a set. |
| INSERT INTO | INSERT INTO table_name VALUES (value1, value2, ...) |
Used to insert new records into a table. |
| INNER JOIN | SELECT column_name(s) FROM table_name1 INNER JOIN table_name2 ON table_name1.column_name=table_name2.column_name |
Used to return matching records from two tables. |
| LEFT JOIN | SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2 ON table_name1.column_name=table_name2.column_name |
Used to return all records from the left table and the matching records from the right table. |
| RIGHT JOIN | SELECT column_name(s) FROM table_name1 RIGHT JOIN table_name2 ON table_name1.column_name=table_name2.column_name |
Used to return all records from the right table and the matching records from the left table. |
| FULL JOIN | SELECT column_name(s) FROM table_name1 FULL JOIN table_name2 ON table_name1.column_name=table_name2.column_name |
Used to return all records from both tables, whether they match or not. |
| LIKE | SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern |
Used to filter records matching a specific pattern. |
| ORDER BY | SELECT column_name(s) FROM table_name ORDER BY column_name [ASC|DESC] |
Used to sort the result set. ASC means ascending order (default), DESC means descending order. |
| SELECT | SELECT column_name(s) FROM table_name |
Used to select data from a table. |
| SELECT | SELECT * FROM table_name |
Used to select all columns in a table. |
| SELECT DISTINCT | SELECT DISTINCT column_name(s) FROM table_name |
Used to return unique distinct values. |
| SELECT INTO | SELECT * INTO new_table_name [IN externaldatabase] FROM old_table_name |
Used to select data from one table and insert it into a new table. |
| SELECT TOP | SELECT TOP number|percent column_name(s) FROM table_name ORDER BY column_name [ASC|DESC] |
Returns a specified number of top records from a table, either an absolute number or a percentage. |
| TRUNCATE TABLE | TRUNCATE TABLE table_name |
Used to delete all data in a table without deleting the table structure. |
| UNION | SELECT column_name(s) FROM table_name1 UNION SELECT column_name(s) FROM table_name2 |
Used to combine the result sets of two or more SELECT statements, excluding duplicate records. |
| UNION ALL | SELECT column_name(s) FROM table_name1 UNION ALL SELECT column_name(s) FROM table_name2 |
Used to combine the result sets of two or more SELECT statements, including duplicate records. |
| UPDATE | UPDATE table_name SET column1=value, column2=value, ... WHERE some_column=some_value |
Used to modify existing records in a table. |
| WHERE | SELECT column_name(s) FROM table_name WHERE column_name operator value |
Used to filter records and specify query conditions. |