SQL Keywords Reference

Welcome to The Coding College! This comprehensive reference guide covers SQL keywords, the building blocks of Structured Query Language. Understanding these keywords is essential for efficient database interaction.

What Are SQL Keywords?

SQL keywords are predefined, reserved words used to perform specific operations in a database. They form the syntax and structure of SQL commands. Each keyword serves a distinct purpose and cannot be used as an identifier (like table or column names).

Commonly Used SQL Keywords

Here’s a categorized reference of SQL keywords used across major database systems like MySQL, SQL Server, and PostgreSQL:

1. Data Definition Language (DDL) Keywords

These keywords define or modify the structure of database objects such as tables, schemas, and indexes.

KeywordDescription
CREATECreates a new database, table, or object.
ALTERModifies an existing database or table structure.
DROPDeletes an existing database or table.
TRUNCATEDeletes all records from a table, resetting it.

2. Data Manipulation Language (DML) Keywords

These keywords are used to retrieve and manipulate data stored in the database.

KeywordDescription
SELECTRetrieves data from one or more tables.
INSERTAdds new rows of data into a table.
UPDATEModifies existing data in a table.
DELETERemoves records from a table.

3. Data Query Language (DQL) Keywords

Primarily focused on querying data.

KeywordDescription
WHEREFilters records based on specific conditions.
ORDER BYSorts results in ascending or descending order.
GROUP BYGroups rows that share the same values in specified columns.
HAVINGFilters grouped records using conditions.

4. Transaction Control Language (TCL) Keywords

These keywords control transactions within a database.

KeywordDescription
COMMITSaves all changes made in a transaction.
ROLLBACKReverts changes made in a transaction.
SAVEPOINTCreates a point within a transaction to roll back to.

5. Control of Access Keywords

These manage database security and permissions.

KeywordDescription
GRANTGives specific privileges to users or roles.
REVOKERemoves specific privileges from users or roles.

6. Join and Set Operation Keywords

These keywords manage relationships between tables or combine results of queries.

KeywordDescription
JOINCombines rows from two or more tables based on a related column.
INNER JOINReturns rows with matching values in both tables.
LEFT JOINReturns all rows from the left table and matching rows from the right.
RIGHT JOINReturns all rows from the right table and matching rows from the left.
FULL OUTER JOINReturns rows when there is a match in either table.
UNIONCombines the results of two queries, eliminating duplicates.
INTERSECTReturns common rows from two queries.
EXCEPTReturns rows from the first query not present in the second.

7. Function and Expression Keywords

Used in functions and expressions to manipulate or analyze data.

KeywordDescription
ASAssigns an alias to a table or column.
DISTINCTEnsures unique results by removing duplicates.
NULLRepresents missing or undefined values.
CASEImplements conditional logic in queries.

8. Miscellaneous Keywords

KeywordDescription
EXISTSTests for the existence of a record in a subquery.
LIMITRestricts the number of rows returned by a query.
OFFSETSpecifies the starting point of the rows to return.
LIKEPerforms pattern matching with wildcard characters.
INChecks if a value matches any value in a list.
BETWEENFilters values within a specified range.

SQL Keywords by Database

KeywordMySQLSQL ServerMS Access
AUTO_INCREMENTYesNo (use IDENTITY)No
IDENTITYNoYesNo
LIMITYesNo (use TOP)No

Best Practices for Using SQL Keywords

  1. Capitalization: Write SQL keywords in uppercase to improve readability.
  2. Avoid Reserved Words: Do not use keywords as identifiers for columns or tables.
  3. Use Aliases Wisely: Use AS for column or table aliases to make queries easier to understand.
  4. Combine Conditions Thoughtfully: Use AND, OR, and NOT for complex filtering.

Conclusion

SQL keywords are fundamental to writing effective queries and managing databases. Familiarizing yourself with these keywords ensures you can perform a variety of operations efficiently. Bookmark this guide for quick reference!

Leave a Comment