SQL Quick Reference
| SQL statements | Syntax | Description |
|---|---|---|
| AND / OR | SELECT column_name(s) |
FROM table_name
WHERE condition
AND\|OR condition | AND:表示逻辑与
OR:表示逻辑或 |
| ALTER TABLE | ALTER TABLE table_name ADD column_name datatype ALTER TABLE table_name DROP COLUMN column_name | Used to modify the structure of an existing table, adding or removing columns. | | AS (alias) | SELECT column_name AS column_alias FROM table_name SELECT column_name FROM table_name AS table_alias | Used to assign an alias to 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 its columns and data types. | | CREATE INDEX | CREATE INDEX index_name ON table_name (column_name) CREATE UNIQUE INDEX index_name ON table_name (column_name) | Used to create an index on a table's columns 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 store the results of a complex query. | | DELETE | DELETE FROM table_name WHERE some_column=some_value DELETE FROM table_name DELETE * FROM table_name | Used to delete records from a table,DELETE FROM table_name and DELETE * FROM table_name deletes all records. | | DROP DATABASE | DROP DATABASE database_name | Used to delete a database. | | DROP INDEX | 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) | Used to drop 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 a grouped result set. | | IN | SELECT column_name(s) FROM table_name WHERE column_name IN (value1, value2, ...) | Used to filter records that match a value in a set. | | INSERT INTO | INSERT INTO table_name VALUES (value1, value2, ...) INSERT INTO table_name (column1, column2, ...) 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 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 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, regardless of whether they match. | | LIKE | SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern | Used to filter records that match a specific pattern. | | ORDER BY | SELECT column_name(s)
FROM table_name
ORDER BY column_name [ASC|DESC] | Used to sort a 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 from a table. | | SELECT DISTINCT | SELECT DISTINCT column_name(s) FROM table_name | Used to return only distinct values. | | SELECT INTO | SELECT * INTO new_table_name [IN externaldatabase] FROM old_table_name SELECT column_name(s) 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 the first specified number of records from a table, which can be an absolute number or a percentage. | | TRUNCATE TABLE | TRUNCATE TABLE table_name | Used to delete all data from 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, without 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.